Nginx 502 Bad Gateway: causes and fixes
Short answer
502 means nginx could not reach or understand the backend (PHP-FPM, Node, a Docker container…). Check: is the backend running → do address and port match → permissions/SELinux → oversized response headers.
Most common cause: The backend is not running, crashed, or was OOM-killed.
Try this first: Match the backend's listen address
location / {
proxy_pass http://127.0.0.1:3000; # do not use localhost
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}Last verified: · against the official docs listed below · Sources
Typical log lines
connect() failed (111: Connection refused) while connecting to upstream connect() to unix:/run/php/php8.3-fpm.sock failed (2: No such file or directory) upstream sent too big header while reading response header from upstream no live upstreams while connecting to upstream
Common causes
- The backend is not running, crashed, or was OOM-killed.
- Wrong address, port or socket path in proxy_pass / fastcgi_pass (the socket name changes after PHP upgrades).
- Backend listens on 127.0.0.1 but proxy_pass uses localhost, which resolves to ::1; or a container only listens inside its network.
- SELinux blocks nginx from making network connections (common on CentOS/Rocky/Alma).
- Backend response headers too large (big cookies/JWT) for proxy_buffer_size.
Diagnosis commands
tail -n 50 /var/log/nginx/error.logss -ltnp | grep -E ':3000|php-fpm'curl -sv http://127.0.0.1:3000/ -o /dev/nullls -l /run/php/systemctl status php8.3-fpm --no-pagergetenforce && getsebool httpd_can_network_connectFix configs
Match the backend's listen address
location / {
proxy_pass http://127.0.0.1:3000; # do not use localhost
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}PHP-FPM socket path
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass unix:/run/php/php8.3-fpm.sock; # must match ls /run/php/
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}Oversized response headers
proxy_buffer_size 16k; proxy_buffers 8 16k; # PHP-FPM: fastcgi_buffer_size 32k; fastcgi_buffers 16 16k;
Allow under SELinux (RHEL family)
setsebool -P httpd_can_network_connect 1
Run nginx -t first, then systemctl reload nginx.
Key facts and sources
- proxy_buffer_size defaults to one memory page (4k or 8k); response headers that do not fit cause “upstream sent too big header”. [nginx.org]
- proxy_connect_timeout defaults to 60s and usually cannot exceed 75 seconds. [nginx.org]
- fastcgi_pass accepts a TCP address or a unix: socket path. [nginx.org]
Checked against the linked pages on 2026-11-14; the official docs win if a newer nginx changes them.
FAQ
What is the difference between 502 and 504?
502 means the connection failed or the reply was invalid, usually instantly; 504 means the backend accepted but did not answer in time, usually after tens of seconds.
Does restarting nginx fix a 502?
Rarely. A 502 is almost always the backend; read the connect() error in error.log, then restart or fix the backend.
I get 502 behind Cloudflare. What now?
Check whose error page it is. A Cloudflare-styled 502 means Cloudflare cannot reach your origin (firewall, port); an nginx-styled one is the backend issue described here.
Fix it with a tool
Related errors
VPS / CDN recommendations may appear here later, labelled as affiliate links. There are no referral links today.