Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

kingcoen's avatar

502 Bad Gateway Timeout Error - Recurring problem.

Hi,

I've a timeout error with Nginx - PHP. After 30 seconds the app return 502 Bad Gateway, always. I've changed fast-cgi params, PHP params, Nginx,etc but the problem still arises.

What can we do about this issue? We have setup the server on DigitalOcean using forge. Use php5.6.

0 likes
6 replies
richard@gorbutt.com's avatar

Look in the log files. I was running a wordpress site (setup by Forge) and I was getting these. Turned out someone(!) was hitting the xml_rpc.php file in the site root 3 times a second and it kept killing the php fpm process. The log files guided me to that. Once I renamed the file it as been rock solid and I didn't need it.

With my limited server knowledge it seems that to take down an nginx site seems a lot easier than apache! :-)

ashwinmram's avatar

I did some digging and here what I found:

You get more details if you look at your nginx logs. The problem is you won't be able to access them unless you first switch to the root user via

sudo su

You can then access the nginx log under and you will see the error I got below as well:

/var/log/nginx/yoursite.com-error.log

2017/12/26 23:15:15 [error] 15705#15705: *179 recv() failed (104: Connection reset by peer) while reading response header from upstream, client: [MY_LOCAL_IP], server: yoursite.com, request: "POST /admin HTTP/2.0", upstream: "fastcgi://unix:/var/run/php/php7.0-fpm.sock:", host: "yoursite.com", referrer: "https://prime7pms.com/admin"

The big clue was Connection reset by peer. In order to fix it I had to do the following:

  1. Click on Edit Nginx Configuration through Forge under the Site Details screen for your site. If you wish to do this via the console it is located here:
 etc/nginx/sites-enabled/yoursite.com

I then used the following settings to increase the time-out:

    location ~ \.php$ {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
        fastcgi_index index.php;
    include fastcgi_params;
        fastcgi_connect_timeout 300;
        fastcgi_send_timeout 300;
        fastcgi_read_timeout 300;
        client_max_body_size 16M;
    }

That is only part of the solution. You will also need to visit /etc/php/7.0/cli/php.ini and change the Resource Limits as follows:

max_execution_time = 300

The final change you will need to make is in /etc/php/7.0/fpm/pool.d/www.conf where you will have to set the following:

request_terminate_timeout=300

After all these changes you will need to bounce PHP and Nginx which you could do through the Forge interface or run the following:

sudo systemctl reload php7.0-fpm.service
sudo systemctl reload nginx.service

Final considerations, I used 300 for my settings which is 5 minutes. I am still using PHP 7.0 on my server, so you may have to update all references to your PHP version.

I hope this helps someone as I was cracking my head for a few hours for a seemingly simple issue!

4 likes
eugenefvdm's avatar

It's incredible how the Laravel guys managed to solve this common problem by actually updating three key parameters!

Please or to participate in this conversation.