PHP-FPM and Nginx: 502 Bad Gateway

后端 未结 15 1798
闹比i
闹比i 2021-01-29 18:35

Configuration

  • Ubuntu Server 11.10 64 bit
  • Amazon AWS, Ec2, hosted on the cloud
  • t1.micro instance

Before I write an

15条回答
  •  庸人自扰
    2021-01-29 18:52

    If anyone finds this page by encountering the same problem I had, I found the answer here.

    For those of you who can't be bothered to click and work it out for themselves... ;)

    The Condition:

    Ubuntu or Debian server with NGINX and PHP 5.3 works fine but upgrading PHP to 5.4 gives 502 Bad Gateway errors. Looking for services running on port 9000 (typically running netstat -lp or similar) returns nothing.

    The fix:

    Open /etc/php5/fpm/pool.d/www.conf and make a note of the 'listen' parameter (in my case /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
    

    and replace the fastcgi_pass variable in your vhost with the location you just noted.

    So this sample symfony2 configuration (taken from 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;
      }
    

    becomes this:

      # 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;
      }
    

    Then restart nginx:

    sudo /etc/init.d/nginx restart
    

    Note: replace ~ ^/(app|app_dev)\.php(/|$) { with ~ ^/index\.php(/|$) { if you're not on SF2**

    Hope this saves someone a little bit of time :)

    Edit

    Of course, you could change the listen = /var/run/php5-fpm.sock to listen = 127.0.0.1:9000 in /etc/php5/fpm/pool.d/www.conf then restart php5-fpm (which would save you from having to change your vhosts), but you have to assume they changed php5-fpm to run through a socket rather than listening on port 9000 for a reason.

    Edit2

    If you're still experiencing 502 error see this answer.

提交回复
热议问题