nginx禁止和坏网关错误

nginx禁止和坏网关错误,nginx,amazon-ec2,php,Nginx,Amazon Ec2,Php,我将nginx配置为从服务器提供两个virtua主机:一个主主机和一个子域主机。主主机是一个rails应用程序,为乘客提供服务。它按预期工作 子域主机是一个小型PHP应用程序。对该子域执行浏览器请求时,返回403(禁止)错误。当对特定文件执行浏览器请求时,它返回502(坏网关)错误 以下是nginx.conf文件: #user nobody; worker_processes 3; events { worker_connections 19000; } worker_rlimit

我将nginx配置为从服务器提供两个virtua主机:一个主主机和一个子域主机。主主机是一个rails应用程序,为乘客提供服务。它按预期工作

子域主机是一个小型PHP应用程序。对该子域执行浏览器请求时,返回403(禁止)错误。当对特定文件执行浏览器请求时,它返回502(坏网关)错误

以下是nginx.conf文件:

#user  nobody;
worker_processes  3;

events {
  worker_connections  19000;
}

worker_rlimit_nofile  20000;


http {
  include       mime.types;
  default_type  application/octet-stream;

  passenger_root /usr/local/lib/ruby/gems/1.9.1/gems/passenger-3.0.18;
  passenger_ruby /usr/local/bin/ruby;

  sendfile        on;

  gzip  on;
  gzip_http_version   1.1;
  gzip_disable        "msie6";
  gzip_vary           on;
  gzip_comp_level     9;
  gzip_static         on;

  passenger_max_pool_size 6;
  passenger_min_instances 1;
  passenger_pool_idle_time 10;

  # Rails app
  server {
    listen       80;
    server_name  .domain.com;
    passenger_enabled on;

    root /home/ubuntu/rails_app/public;

    location ~ ^/assets/ {
      expires           max;
      add_header        Cache-Control public;
      #add_header        Last-Modified "";
      #add_header        ETag "";
      open_file_cache          max=1000 inactive=500s;
      open_file_cache_valid    600s;
      open_file_cache_errors   on;
      break;
    }

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
      root   html;
    }
  }


  # PHP app
  server {
      listen       80;
      server_name  sub.domain.com;

      root   /home/ubuntu/rails_app/sendy;
      index  index.html index.htm index.php;

      if (!-d $uri) { 
          set $rule_0 1$rule_0;
      }

      if (!-f $uri) {
          set $rule_0 2$rule_0;
      }

      if ($rule_0 = "21") {
          rewrite ^/([a-zA-Z0-9-]+)$ /$1.php last;
      }

      location / {
          try_files $uri $uri/ /index.php;
      }

      location ~ \.php$ {
          #root           html;
          fastcgi_pass   unix:/var/run/php-fpm/php-fpm.sock;
          #fastcgi_index  index.php;
          fastcgi_param  SCRIPT_NAME        $fastcgi_script_name;
          fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;
          include        fastcgi_params;
      }

      location /l {
          rewrite ^/l/([a-zA-Z0-9/]+)$ /l.php?i=$1 last;
      }

      location /t {
          rewrite ^/t/([a-zA-Z0-9/]+)$ /t.php?i=$1 last;
      }

      location /w {
          rewrite ^/w/([a-zA-Z0-9/]+)$ /w.php?i=$1 last;
      }

      location /unsubscribe {
          rewrite ^/unsubscribe/(.*)$ /unsubscribe.php?i=$1 last;
      }

      location /subscribe {
          rewrite ^/subscribe/(.*)$ /subscribe.php?i=$1 break;
      }

      location ~ /\.ht {
          deny  all;
      }
  }
}
我认为这是一个权限问题,但我将它们更改为744755甚至777,仍然会得到相同的错误


有什么想法吗?

您的nginx配置在第一次查看时看起来不错。我有点担心php fpm进程。您应该有一个主进程和至少一个子进程,通常不止一个

您是否配置了php fpm池

我的流程就是这样的

# ps aux | grep "php"
root      1081  0.0  0.3 387316  5404 ?        Ss   14:49   0:00 php-fpm: master process (/etc/php-fpm.conf)
nobody    1082  0.0  0.6 390376 10316 ?        S    14:49   0:03 php-fpm: pool poolname.com
nobody    1083  0.0  0.6 390388 10360 ?        S    14:49   0:03 php-fpm: pool poolname.com
nobody    1084  0.0  0.6 390392 10324 ?        S    14:49   0:02 php-fpm: pool poolname.com
您的
/etc/php fpm.conf
应至少包含以下内容:

include=/etc/php-fpm.d/*.conf

[global]
pid = /var/run/php-fpm/php-fpm.pid
error_log = /var/log/php-fpm.log
daemonize = yes
然后需要在
/etc/php fpm.d/

e、 g.
mydomain.com.conf

[mydomain.com]
; The address on which to accept FastCGI requests.
; Valid syntaxes are:
;   'ip.add.re.ss:port'    - to listen on a TCP socket to a specific address on
;                            a specific port;
;   'port'                 - to listen on a TCP socket to all addresses on a
;                            specific port;
;   '/path/to/unix/socket' - to listen on a unix socket.
; Note: This value is mandatory.

listen        = /var/run/php-fpm/php-fpm.sock

; Set permissions for unix socket, if one is used. In Linux, read/write
; permissions must be set in order to allow connections from a web server. Many
; BSD-derived systems allow connections regardless of permissions..
; Default Values: user and group are set as the running user
; mode is set to 0666

listen.owner  = nginx
listen.group  = nginx
listen.mode   = 0666

; Unix user/group of processes
; Note: The user is mandatory. If the group is not set, the default user's group
; will be used.

user          = nobody
group         = nobody

; Choose how the process manager will control the number of child processes.
; Possible Values:
;   static  - a fixed number (pm.max_children) of child processes;
;   dynamic - the number of child processes are set dynamically based on the
;             following directives:
;             pm.max_children      - the maximum number of children that can
;                                    be alive at the same time.
;             pm.start_servers     - the number of children created on startup.
;             pm.min_spare_servers - the minimum number of children in 'idle'
;                                    state (waiting to process). If the number
;                                    of 'idle' processes is less than this
;                                    number then some children will be created.
;             pm.max_spare_servers - the maximum number of children in 'idle'
;                                    state (waiting to process). If the number
;                                    of 'idle' processes is greater than this
;                                    number then some children will be killed.
; Note: This value is mandatory.

pm = dynamic

; The number of child processes to be created when pm is set to 'static' and the
; maximum number of child processes to be created when pm is set to 'dynamic'.
; This value sets the limit on the number of simultaneous requests that will be
; served. Equivalent to the ApacheMaxClients directive with mpm_prefork.
; Equivalent to the PHP_FCGI_CHILDREN environment variable in the original PHP
; CGI.
; Note: Used when pm is set to either 'static' or 'dynamic'
; Note: This value is mandatory.

pm.max_children = 100

; The number of child processes created on startup.
; Note: Used only when pm is set to 'dynamic'
; Default Value: min_spare_servers + (max_spare_servers - min_spare_servers) / 2

pm.start_servers = 20

; The desired minimum number of idle server processes.
; Note: Used only when pm is set to 'dynamic'
; Note: Mandatory when pm is set to 'dynamic'

pm.min_spare_servers = 5

; The desired maximum number of idle server processes.
; Note: Used only when pm is set to 'dynamic'
; Note: Mandatory when pm is set to 'dynamic'

pm.max_spare_servers = 100

; The number of requests each child process should execute before respawning.
; This can be useful to work around memory leaks in 3rd party libraries. For
; endless request processing specify '0'. Equivalent to PHP_FCGI_MAX_REQUESTS.
; Default Value: 0

pm.max_requests = 250
如果config设置为您完全满意,请重新启动php fpm

/etc/init.d/php-fpm restart
您现在应该看到更多的进程

ps aux | grep "php"
确保套接字文件存在并且由正确的用户拥有(应该是运行nginx的用户)

现在重新启动nginx,确保它读取新的套接字文件

/etc/init.d/nginx restart
希望这有帮助。
如果没有,则出现其他错误,我们需要继续调试
ps aux | grep“php”
的输出是什么?您的php fpm是否正在运行,是否正在侦听您指定的套接字?这是输出,Michel:
ubuntu 1531 0.0 0.1 8104 928 pts/0 S+14:3 0:00 grep--color=auto php root 7256 0.0 0.5 60672 3212?Ss 2012 0:09 php fpm:master process(/etc/php5/fpm/php fpm.conf)
hey我正试图帮助您,但“仍然不起作用”不是我们可以继续的回答。请告诉我你做了什么,如果我建议的检查都是肯定的,我已经完成了你指定的所有步骤。我更改文件所有者,并检查所有内容。并且日志仍然显示
*9 connect()到unix:/var/run/php fpm/php-fpm.sock失败(111:连接被拒绝)
/etc/init.d/nginx restart