PvP Server Kiralama & Oyun Sunucuları
0 Giriş Yap Kayıt Ol

Solution to 502 Bad Gateway and 504 Gateway Timeout Error on Linux Server

Yazdır

Solution to 502 Bad Gateway and 504 Gateway Timeout Error on Linux Server

A step-by-step guide to solving Nginx, Apache, PHP-FPM and service timeout problems.

seen on websites 502 Bad Gateway and 504 Gateway Timeout errors usually occur because the web server is not getting the correct response from the back-end service. This service can be PHP-FPM, Apache backend, Node.js application, game panel API service or another application behind the proxy.

These errors are especially common in hosting, game panel, WHMCS, Pterodactyl, custom administration panel and web-based launcher systems.

Simple explanation: Nginx is like the attendant at the door. If the PHP or application inside does not respond, it will show a 502 or 504 error to the user.

1. Difference Between 502 and 504 Error

  • 502 Bad Gateway: The web server cannot connect to the backend service or receives a garbled response.
  • 504 Gateway Timeout: The backend service seems to be responding, but the response is not returned in time.

So 502 is more of a connection/service crash, and 504 is a slowness or timeout problem.

2. Checking Service Statuses

First, check whether the web server and PHP services are running.

systemctl status nginx
systemctl status apache2
systemctl status php-fpm

On CentOS, AlmaLinux or Rocky Linux systems, the PHP-FPM service may be named:

systemctl status php-fpm
systemctl status httpd

If the service has stopped, restart it:

systemctl restart nginx
systemctl restart php-fpm

3. Reviewing Log Files

It is not correct to proceed with guesswork for 502 and 504 errors. The log file shows the real cause of the error.

tail -n 100 /var/log/nginx/error.log
tail -n 100 /var/log/apache2/error.log

On CentOS-based systems:

tail -n 100 /var/log/httpd/error_log

The following statements can be seen in the logs:

  • connect() failed: Unable to connect to backend service.
  • upstream timed out: Backend service responds late.
  • connection refused: The service is down or there is forwarding to the wrong port.
  • permission denied: There is a socket or file permission problem.

4. PHP-FPM Socket or Port Control

Nginx connects to PHP-FPM via socket or port. The PHP-FPM path in the Nginx config file and the actual PHP-FPM listen value must be the same.

grep -R "fastcgi_pass" /etc/nginx/sites-enabled/
grep -R "listen =" /etc/php/*/fpm/pool.d/www.conf

For example if Nginx uses:

fastcgi_pass unix:/run/php/php8.2-fpm.sock;

The same socket should be listened to on the PHP-FPM side:

listen = /run/php/php8.2-fpm.sock
Important: If the PHP version has changed, Nginx may still be looking at the old PHP-FPM socket path. This is one of the most common causes of 502 errors.

5. Increasing Timeout Settings

504 errors may be received in large processing panels, play markets, APIs or slow-running PHP scripts. Nginx timeout values ​​can be increased.

nano /etc/nginx/nginx.conf

The following values can be added to the http block or the relevant server block:

proxy_connect_timeout 300;
proxy_send_timeout 300;
proxy_read_timeout 300;
fastcgi_read_timeout 300;

Test the settings and restart Nginx:

nginx -t
systemctl restart nginx

6. Checking PHP-FPM Process Limits

When traffic increases, PHP-FPM transaction limits may be insufficient. In this case, requests are queued and a timeout occurs.

nano /etc/php/8.2/fpm/pool.d/www.conf

Example settings:

pm = dynamic
pm.max_children = 30
pm.start_servers = 5
pm.min_spare_servers = 5
pm.max_spare_servers = 10

These values should be adjusted according to the amount of RAM of the server. Setting too high a value increases RAM consumption.

Common Mistakes

  • Changing settings without looking at the log file
  • Not updating the Nginx socket path even though the PHP version has changed
  • Not fixing the actual slow query by increasing the timeout
  • Increasing PHP-FPM limits without calculating RAM
  • Restarting the service without testing Nginx config

FAQ

Is 502 error always caused by Nginx?
No. Nginx is just the one showing the error. The real problem may be in PHP-FPM, Apache backend, Node.js application or another service.

Is increasing timeout the definitive solution for 504 errors?
Not always. Increasing timeout provides temporary relief. The real solution is to find the slow query, heavy processing or performance problem.

Is it safe to restart PHP-FPM?
It is generally safe, but active requests may be affected for a short time. In busy systems, low traffic hours should be preferred.

Performance Recommendations

  • Check the PHP-FPM version and socket path regularly.
  • Examine slow query logs in web panel and API services.
  • If RAM is insufficient, do not increase the PHP-FPM max_children value unconsciously.
  • Reduce backend load by using a cache system.
  • Manage error logs before they grow with logrotate.

This article is specially prepared for PvPServer.

Bu cevap yeterince yardımcı oldu mu?

Oyla

overlay spinner