PHP-FPM 和 Nginx : 502 Bad Gateway

标签 php nginx amazon-ec2 fastcgi fpm

配置

  • Ubuntu 服务器 11.10 64 位
  • Amazon AWS,Ec2,托管在云端
  • t1.micro 实例

在我写任何其他内容之前,我想声明我已经检查了 nginx 502 bad gatewayNginx + PHP-FPM 502 Bad Gateway线程,不幸的是在这方面对我没有帮助。

这个问题似乎很常见:nginx 或 php-fpm 的错误配置可能导致 502 Bad Gateway 错误,这是我无法摆脱的。请注意,即使我转到我的域根目录,它也会出现,而无需指定任何特定目录。

我正在运行一个 Amazon EC2 网络服务器,启用了端口 9000,打开了端口 80,等等。

特别的问题是,我怎样才能摆脱这个讨厌的错误?或者,更好的是,我怎样才能让 php5-fpm 实际工作

到目前为止我所做的尝试

配置文件的大部分一致编辑,特别是 php-fpm.confnginx.conf

i. php-fpm.conf

我添加了以下内容,但并没有太大帮助:

;;;;;;;;;;;;;
; Fpm Start ;
;;;;;;;;;;;;;

;pm.start_servers = 20
;pm.min_spare_servers = 5
;pm.max_spare_servers = 35

现在,后来我尝试包含我的配置文件:

include=/etc/php5/fpm/*.conf

这只会让我更糟。

完整配置

;;;;;;;;;;;;;;;;;;;;;
; FPM Configuration ;
;;;;;;;;;;;;;;;;;;;;;

; All relative paths in this configuration file are relative to PHP's install
; prefix (/usr). This prefix can be dynamicaly changed by using the
; '-p' argument from the command line.

; Include one or more files. If glob(3) exists, it is used to include a bunch of
; files from a glob(3) pattern. This directive can be used everywhere in the
; file.
; Relative path can also be used. They will be prefixed by:
;  - the global prefix if it's been set (-p arguement)
;  - /usr otherwise
;include=/etc/php5/fpm/*.conf

;;;;;;;;;;;;;;;;;;
; Global Options ;
;;;;;;;;;;;;;;;;;;

[global]
; Pid file
; Note: the default prefix is /var
; Default Value: none
pid = /var/run/php5-fpm.pid

; Error log file
; Note: the default prefix is /var
; Default Value: log/php-fpm.log
error_log = /var/log/php5-fpm.log

; Log level
; Possible Values: alert, error, warning, notice, debug
; Default Value: notice
log_level = notice

; If this number of child processes exit with SIGSEGV or SIGBUS within the time
; interval set by emergency_restart_interval then FPM will restart. A value
; of '0' means 'Off'.
; Default Value: 0
;emergency_restart_threshold = 0

; Interval of time used by emergency_restart_interval to determine when 
; a graceful restart will be initiated.  This can be useful to work around
; accidental corruptions in an accelerator's shared memory.
; Available Units: s(econds), m(inutes), h(ours), or d(ays)
; Default Unit: seconds
; Default Value: 0
emergency_restart_interval = 0

; Time limit for child processes to wait for a reaction on signals from master.
; Available units: s(econds), m(inutes), h(ours), or d(ays)
; Default Unit: seconds
; Default Value: 0
;process_control_timeout = 0

; Send FPM to background. Set to 'no' to keep FPM in foreground for debugging.
; Default Value: yes
daemonize = no

;;;;;;;;;;;;;
; Fpm Start ;
;;;;;;;;;;;;;

;pm.start_servers = 20
;pm.min_spare_servers = 5
;pm.max_spare_servers = 35

;;;;;;;;;;;;;;;;;;;;
; Pool Definitions ; 
;;;;;;;;;;;;;;;;;;;;

; Multiple pools of child processes may be started with different listening
; ports and different management options.  The name of the pool will be
; used in logs and stats. There is no limitation on the number of pools which
; FPM can handle. Your system will tell you anyway :)

; To configure the pools it is recommended to have one .conf file per
; pool in the following directory:
include=/etc/php5/fpm/pool.d/*.conf

iinginx.conf

老实说,这个配置只是我访问过的几个网站的一小部分,但我可以告诉你,在这个 502 Bad Gateway 业务之前,服务器运行良好(没有 PHP 工作。期间。)。

问题主要在于某些事情非常非常错误。现在,当我尝试执行 service php5-fpm restart 时,它卡在我猜是无限循环之类的东西中,我什至无法 CTRL-C出来。

完整配置

user www-data;
worker_processes 1;
pid /var/run/nginx.pid;

events {
    worker_connections 64;
    # multi_accept on;
}

http {

    ##
    # Basic Settings
    ##

    sendfile on;
    tcp_nopush off;
    tcp_nodelay on;
    keepalive_timeout 65;
    types_hash_max_size 2048;
    # server_tokens off;

    # server_names_hash_bucket_size 64;
    # server_name_in_redirect off;

    include /etc/nginx/mime.types;
    default_type application/octet-stream;

    ##
    # Logging Settings
    ##

    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log;

    ##
    # Gzip Settings
    ##

    gzip on;
    gzip_disable "msie6";

    # gzip_vary on;
    # gzip_proxied any;
    # gzip_comp_level 6;
    # gzip_buffers 16 8k;
    # gzip_http_version 1.1;
    # gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;

    ##
    # Virtual Host Configs
    ##

    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;

    server {
        listen 80;
        server_name ec2-xx-xx-xx-xx.compute-x.amazonaws.com;

        location ~ ^(.+\.php)(.*)$ {
            root   /home/wayvac/public;
            fastcgi_pass   unix:/var/run/php5-fpm.pid;  
            #fastcgi_pass   127.0.0.1:9000; #Un-comment this and comment "fastcgi_pass   unix:/var/run/php-fpm/php-fpm.sock;" if you are not using php-fpm.
            fastcgi_index  index.php;
            set $document_root2 $document_root;
            if ($document_root2 ~ "^(.*\\\\).*?[\\\\|\/]\.\.\/(.*)$") { set $document_root2 $1$2; }
            if ($document_root2 ~ "^(.*\\\\).*?[\\\\|\/]\.\.\/(.*)$") { set $document_root2 $1$2; }
            if ($document_root2 ~ "^(.*\\\\).*?[\\\\|\/]\.\.\/(.*)$") { set $document_root2 $1$2; }
            if ($document_root2 ~ "^(.*\\\\).*?[\\\\|\/]\.\.\/(.*)$") { set $document_root2 $1$2; }
            if ($document_root2 ~ "^(.*\\\\).*?[\\\\|\/]\.\.\/(.*)$") { set $document_root2 $1$2; }
            fastcgi_split_path_info ^(.+\.php)(.*)$;
            fastcgi_param   SCRIPT_FILENAME $document_root2$fastcgi_script_name;
            fastcgi_param   PATH_INFO   $fastcgi_path_info;
            fastcgi_param   PATH_TRANSLATED $document_root2$fastcgi_path_info;
            include fastcgi_params;
            fastcgi_param  DOCUMENT_ROOT      $document_root2;
        }       

        access_log /var/log/nginx/access.log;
        error_log /var/log/nginx/error.log;

        location / {
            root /home/wayvac/public;   
            index index.html index.htm index.php;
        }

        location ~* \.(?:ico|css|js|gif|jpe?g|png)$ {
            # Some basic cache-control for static files to be sent to the browser
            expires max;
            add_header Pragma public;
            add_header Cache-Control "public, must-revalidate, proxy-revalidate";
        }

        #include drop.conf;
        #include php.conf;
    }
}

最佳答案

如果有人通过遇到与我相同的问题找到此页面,我找到了答案 here .

对于那些懒得点击并自己解决的人... ;)

条件:

带有 NGINX 和 PHP 5.3 的 Ubuntu 或 Debian 服务器可以正常工作,但将 PHP 升级到 5.4 会出现 502 Bad Gateway 错误。寻找在端口 9000 上运行的服务(通常运行 netstat -lp 或类似的)不会返回任何结果。

修复:

打开 /etc/php5/fpm/pool.d/www.conf 并记下“listen”参数(在我的例子中是 /var/run/php5-fpm .sock):

; 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/php5-fpm.sock

并将 vhost 中的 fastcgi_pass 变量替换为您刚刚记下的位置。

所以这个示例 symfony2 配置(取自 here):

  # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
  location ~ ^/(app|app_dev)\.php(/|$) {
    fastcgi_pass   127.0.0.1:9000;
    fastcgi_split_path_info ^(.+\.php)(/.*)$;
    include fastcgi_params;
    fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;
    fastcgi_param  HTTPS              off;
  }

变成这样:

  # pass the PHP scripts to FastCGI server at /var/run/php5-fpm.sock
  location ~ ^/(app|app_dev)\.php(/|$) {
    fastcgi_pass unix:/var/run/php5-fpm.sock;
    fastcgi_split_path_info ^(.+\.php)(/.*)$;
    include fastcgi_params;
    fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;
    fastcgi_param  HTTPS              off;
  }

然后重启nginx:

sudo /etc/init.d/nginx restart

注意:~ ^/(app|app_dev)\.php(/|$) { 替换为 ~ ^/index\.php(/|$) { 如果你不在 SF2 上**

希望这可以节省一些时间:)

编辑

当然,您可以在 /etc 中将 listen =/var/run/php5-fpm.sock 更改为 listen = 127.0.0.1:9000/php5/fpm/pool.d/www.conf 然后重新启动 php5-fpm(这将使您不必更改虚拟主机),但您必须假设他们更改了 php5-fpm 以通过套接字而不是比监听 9000 端口是有原因的。

编辑2

如果您仍然遇到 502 错误,请参阅 answer .

关于PHP-FPM 和 Nginx : 502 Bad Gateway,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10003978/

相关文章:

linux - Django项目结构及整体位置

node.js - AWS EC2 IAM 角色凭证

amazon-web-services - 使用云形成创建堆栈时加密 EC2 的根卷

php - "Did you mean"php 和 mysql 的功能

php - 我应该为我的 Doctrine 模型使用抽象类还是接口(interface)?

nginx 缺少站点可用目录

amazon-web-services - AWS 的动态 SSL 证书

amazon-ec2 - 端口 22 : Connection refused after sudo reboot in AWS

PHP - 编辑 zip 存档中的文件并在关闭前另存为另一个存档名称

javascript - PHP,使用 AJAX 将数据从 javascript 发送到 PHP