PHP-FPM and Nginx: 502 Bad Gateway

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

    In your NGINX vhost file, in location block which processes your PHP files (usually location ~ \.php$ {) through FastCGI, make sure you have next lines:

    proxy_buffer_size          128k;
    proxy_buffers              4 256k;
    proxy_busy_buffers_size    256k;
    fastcgi_buffer_size        16k;
    fastcgi_buffers            4 16k;
    

    After that don't forget to restart fpm and nginx.


    Additional:

    NGINX vhost paths

    • /etc/nginx/sites-enabled/ - Linux
    • '/usr/local/etc/nginx/sites-enabled/' - Mac

    Restart NGINX:

    • sudo service nginx restart - Linux
    • brew service restart nginx - Mac

    Restart FPM:

    Determine fpm process name: - systemctl list-unit-files | grep fpm - Linux - brew services list | grep php - Mac

    and then restart it with:

    • sudo service <service-name> restart - Linux
    • brew services restart <service-name> - Mac
    0 讨论(0)
  • 2021-01-29 19:01

    For anyone else struggling to get to the bottom of this, I tried adjusting timeouts as suggested as I didn't want to stop using Unix sockets...after lots of troubleshooting and not much to go on I found that this issue was being caused by the APC extension that I'd enabled in php-fpm a few months back. Disabling this extension resolved the intermittent 502 errors, easiest way to do this was by commenting out the following line :

    ;extension = apc.so
    

    This did the trick for me!

    0 讨论(0)
  • 2021-01-29 19:02

    Hope this tip will save someone else's life. In my case the problem was that I ran out of memory, but only slightly, was hard to think about it. Wasted 3hrs on that. I recommend running:

    sudo htop
    

    or

    sudo free -m
    

    ...along with running problematic requests on the server to see if your memory doesn't run out. And if it does like in my case, you need to create swap file (unless you already have one).

    I have followed this tutorial to create swap file on Ubuntu Server 14.04 and it worked just fine: http://www.cyberciti.biz/faq/ubuntu-linux-create-add-swap-file/

    0 讨论(0)
  • 2021-01-29 19:02

    I'm very late to this game, but my problem started when I upgraded php on my server. I was able to just remove the .socket file and restart my services. Then, everything worked. Not sure why it made a difference, since the file is size 0 and the ownership and permissions are the same, but it worked.

    0 讨论(0)
  • 2021-01-29 19:03

    Before messing with Nginx config, try to disable ChromePHP first.

    1 - Open app/config/config_dev.yml

    2 - Comment these lines:

    chromephp:
        type:   chromephp
        level:  info
    

    ChromePHP pack the debug info json-encoded in the X-ChromePhp-Data header, which is too big for the default config of nginx with fastcgi.

    0 讨论(0)
  • 2021-01-29 19:05

    I made all this similar tweaks, but from time to time I was getting 501/502 errors (daily).

    This are my settings on /etc/php5/fpm/pool.d/www.conf to avoid 501 and 502 nginx errors… The server has 16Gb RAM. This configuration is for a 8Gb RAM server so…

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

    then set the following values for

    pm.max_children = 70
    pm.start_servers = 20
    pm.min_spare_servers = 20
    pm.max_spare_servers = 35
    pm.max_requests = 500
    

    After this changes restart php-fpm

    sudo service php-fpm restart
    
    0 讨论(0)
提交回复
热议问题