PHP-FPM and Nginx: 502 Bad Gateway

后端 未结 15 1765
闹比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:51

    Don't forget that php-fpm is a service. After installing it, make sure you start it:

    # service php-fpm start
    # chkconfig php-fpm on
    
    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2021-01-29 18:52

    If you met the problem after upgrading php-fpm like me, try this: open /etc/php5/fpm/pool.d/www.conf uncomment the following lines:

    listen.owner = www-data
    listen.group = www-data
    listen.mode = 0666
    

    then restart php-fpm.

    0 讨论(0)
  • 2021-01-29 18:53

    Try setting these values, it solves problem in fast-cgi

    fastcgi_buffer_size 16k;
    fastcgi_buffers 4 16k;
    
    0 讨论(0)
  • 2021-01-29 18:53

    The port was changed to 9001 in 5.4, just changing the port from 9000 to 9001 in the nginx conf and in php-fpm configuration worked for me.

    0 讨论(0)
  • 2021-01-29 18:53

    All right after trying every solution on the web I ended up figuare out the issue using very simple method , first I cheked php-fpm err log

    cat /var/log/php5-fpm.log 
    

    and the most repeated error was

    " WARNING: [pool www] server reached pm.max_children setting (5), consider raising it "
    

    I edit PHP-fpm pools setting

    nano /etc/php5/fpm/pool.d/www.conf
    

    I chenged This Line

    pm.max_children = 5
    

    To new Value

    pm.max_children = 10
    

    BTW I'm using low end VPS with 128MB ram As everyone else I was thinkin redusing pm.max_children will make my server run faster consume less memory , but the setting we using were too low tho even start PHP-fpm process . I hope this help others since I found this after 24 hour testing and failing , ever my webhost support were not able to solve the issue .

    0 讨论(0)
提交回复
热议问题