How to solve Nginx 502 Bad Gateway error?
For Nginx 502 Bad Gateway error, check the upstream service, host and port, timeout records, Unix socket permissions and Docker network in the correct order.
Nginx 502 Bad GatewayThe error indicates that Nginx cannot receive a valid response from the upstream application to which it connects as a reverse proxy or gateway. Most of the time the problem is not Nginx itself; It is caused by a downed service, incorrect host/port, network access, socket permission, or the application closing the connection prematurely.
For this reason, it is not right to increase the timeout values as a first reflex. Matching the message in the Nginx error log with the status of the upstream service at the same time gives much faster results.
1. Read the real cause in the error log
Take note of the second the error occurred and review the relevant error log:
sudo tail -n 100 /var/log/nginx/error.log
sudo journalctl -u nginx --since "10 minutes ago"
connect() failed (111: Connection refused)It usually means that there is no service listening on the target port or that the wrong address is being navigated.upstream timed outindicates that the connection was established but no response was received within the expected time.upstream prematurely closed connectionindicates that the application closed the connection before completing the response.
2. Verify that the upstream service is actually listening
sudo systemctl status my-app
sudo ss -lntp
curl -v http://127.0.0.1:3000/health
If possible, call the health endpoint from the same server or container network where Nginx is running. App only127.0.0.1It cannot be accessed from another container if it is listening on it. APPLICATION0.0.0.0If it is listening on the device but the firewall is blocking the traffic, a direct curl test will help isolate this.
3. See active proxy_pass setting
sudo nginx -T
sudo nginx -t
nginx -Tshows the active configuration with included files. Do not share publicly as the output may contain passwords or special headers. The request matchesserverAndlocationon your blogproxy_passcheck the target:
location /api/ {
proxy_pass http://127.0.0.1:3000;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
Host, port and protocol must be the same as the application. Going via HTTP to an upstream that expects HTTPS orproxy_passChanging the trailing slash unconsciously can cause different problems.
4. Beware of localhost fallacy in Docker networking
If Nginx is in a separate container127.0.0.1It is the Nginx container itself; It is not an application container. Use the service name and application's container port within the same Compose network:
upstream backend {
server api:3000;
}
location /api/ {
proxy_pass http://backend;
}
From Nginx containercurl http://api:3000/healthTest DNS and network access together by running Do not confuse the port published to the host with the in-container port.
5. Check Unix socket permissions and security layers
If PHP-FPM, Gunicorn or a similar service uses a Unix socket, the Nginx user must be able to access the socket. The socket path must be the same in both configs; The file's owner, group and directory traversal permissions must be appropriate. On SELinux-enabled systems, the policy may block access even if the correct Unix permissions are present. in logPermission deniedDo not open random permissions without seeing it.
6. Change timeout only after measurement
If the application process takes really long and the logupstream timed outif anyproxy_connect_timeoutwithproxy_read_timeoutNote the difference between . Connect timeout limits connection establishment, read timeout limits the wait between two reads. Fix slow query, external service wait or application crash first; Then determine a conservative value if needed.
location /report/ {
proxy_pass http://backend;
proxy_connect_timeout 5s;
proxy_read_timeout 60s;
}
After changenginx -tRun it and if successful, reload the service according to your own distribution method. Then generate the same request again and monitor Nginx and the application log together.
Short guide
Common mistake
Only increase timeout values when the upstream service is down or at the wrong address; The error appears later, but the cause is not resolved.