Nginx 502 is an uncommon error that causes due to issue with your server Nginx and PHP-FastCGI. If you notice any Nginx 502 error it is better to check the Nginx log.
When you are investigating the Nginx error logs, you can see that the “recv() failed (104: Connection reset by peer) while reading response header from upstream,” error. This also results in a “no live upstreams while connecting to upstream” error. When I’m checking my WordPress site logs I saw that several times appeared that error log. This is an issue with the Nginx and PHP-FPM configurations. Can easily fix it in just a few steps.
There are mainly three different reasons for Nginx Connection reset by peer.
- Nginx and PHP-FPM communication error.
- PHP-FPM is timing out quickly.
- PHP-FPM is not running.
In order to fix 104: Connection reset by peer while reading response header from upstream issue, you should have access to modify the PHP and Nginx configuration files. If you are on shared hosting or a managed hosting package, you can ask your hosting partner to correct that configuration file.
How to Fix Connection Reset By Peer While Reading Response Header From Upstream.
If you can see continuous connection reset by peer error your visitors may receive “502 Bad Gateway” errors. To correct this Nginx error you must set Nginx keepalive_requests and PHP-FPM pm.max_requests equal value. If these two values are not equal then the Nginx or PHP-FPM end up closing the connection. This is an issue caused by server configuration mismatch, you can fix it by like our Logrotate issue guide.
Change Nginx keepalive_requests.
- Open the nginx.conf configuration file with sudo privileges.
- Change keepalive_requests as per your need. Don’t use too high keepalive_requests, it can cause excessive memory usage.
- Save the configuration file.
Change PHP-FPM www.conf
- Open the www.conf file with sudo privilege.
- In “pm.max_requests” enter the same value you set on “keepalive_requests” in the Nginx conf file.
- Save the configuration file.
- Restart the Nginx and PHP-FPM by running the following command line.
Check PHP permission for session.save_path.
In some situations, your PHP is not able to write the season file. This will generate Nginx 502 connection reset by peer while reading response header from upstream.
There can be several reasons for PHP is not able to write the season file. It can be bad permissions, bad directory ownership, or even bad user/group.
- First check where your PHP session.save_path is. Some situations it is “/var/lib/php/sessions” or “/var/lib/php/session“
- Open your system php.ini file and search for “session.save_path“.
- Then you can see your system PHP session.save_path.
- Run the following command in your SSH terminal to fix the permission issue. In this example my LEMP stack php session.save_path is “/var/lib/php/session“
- Then restart php.
Change FastCGI Timeouts.
Another solution is to change the Nginx Fast-CGI timeouts. Default FastCGI timeout is the 60s. Your application may require higher than the 60s to run, therefore it is better to change the following timeouts to prevent Nginx 502 bad gateway error.
- Open the nginx configuration file. Generally it is “/etc/nginx/nginx.conf“.
- Change add the following timeout values to your nginx.conf.
- fastcgi_read_timeout 300;
- fastcgi_send_timeout 300;
- fastcgi_connect_timeout 300;
- Save the changes and restart nginx.
Increase opcache memory limit.
If your system PHP uses opcache if the opcache memory gets filled, it will show connection reset by peer while reading response header from upstream. The simple solution is to increase the opcache memory limit.
- Open the php.ini file.
- Search for “opcache.memory_consumption”.
- Change the value to a higher amount like below.
opcache.memory_consumption = 256
- From now, your error should disappear and visitors can access your site without any error.