X
X

Select Your Currency

Türk Lirası $ US Dollar
X
X

Select Your Currency

Türk Lirası $ US Dollar

What is Nginx 502 Bad Gateway Error and How to Fix It?

HomepageArticlesTechnical GuidesWhat is Nginx 502 Bad Gateway Error...

What is Nginx 502 Bad Gateway Error?

The 502 Bad Gateway error you encounter when entering a site hosted on the Nginx web server means that Nginx received an invalid response from a backend service (usually PHP-FPM, Apache, or Node.js). This error is one of the most common problems faced by virtual server users and generally indicates a bottleneck on the server-side.

1. Check the PHP-FPM Service

80% of this error corresponds to a crashed or hung PHP-FPM service. First, connect to your server via SSH, check the status of the service, and restart it:

systemctl status php-fpm
systemctl restart php-fpm

If you are using Ubuntu/Debian, you should update the command to systemctl restart php7.4-fpm (depending on the version you use).

2. Read Nginx Logs

The most accurate method to find the exact source of the problem is to look at Nginx error logs (error.log):

tail -f /var/log/nginx/error.log

If you see a phrase like "upstream prematurely closed connection" on the screen, you can understand that the backend service (PHP, etc.) closed before completing the Nginx response.

3. Increase Timeout Values

Especially if a heavy plugin or query is running in systems like WordPress, the PHP process may take a long time to finish, and Nginx may time out and give a 502 error. Try increasing the timeout values in the Nginx configuration file (nginx.conf or the related vhost file):

fastcgi_read_timeout 300;
proxy_read_timeout 300;

After making the settings, test the configuration with nginx -t and restart Nginx (systemctl restart nginx).

If you do not want to worry about these server-side optimizations, you can get support from our expert technical team in our İyibirNet VDS Server services.


Top