nginx 502 Bad Gateway: causes and how to fix it
Fix nginx 502 Bad Gateway with Node, PHP-FPM or any proxy: read the error log, check the app is running and the port or socket matches.
A 502 Bad Gateway means nginx is working but the program behind it did not answer properly. The error log tells you exactly why, so start there.
What to do, step by step
- Open the nginx error log and read the last lines: sudo tail -n 30 /var/log/nginx/error.log.
- If it says connect() failed (111: Connection refused), your app is not running or not on that address and port.
- Check the app is up and listening: sudo ss -tlnp and look for the port from your proxy_pass line.
- For PHP, check PHP-FPM runs and that fastcgi_pass matches its socket or port.
- Fix the cause, then reload nginx: sudo nginx -t && sudo systemctl reload nginx.
Commands you can copy
sudo tail -n 30 /var/log/nginx/error.logsudo ss -tlnpsudo nginx -t && sudo systemctl reload nginxOther causes to check
- (13: Permission denied) while connecting to upstream
- nginx cannot open the socket or port. For a socket file, check its owner and mode (listen.owner and listen.group in the PHP-FPM pool). On SELinux systems run sudo setsebool -P httpd_can_network_connect 1.
- (110: Connection timed out) or upstream timed out
- The app answers too slowly. Find out why it is slow, or raise proxy_read_timeout (or fastcgi_read_timeout for PHP).
- Works for a while, then 502
- The app crashed or ran out of memory. Check its own logs and use a process manager such as systemd or PM2 to restart it.
- upstream sent too big header
- Large cookies or headers. Raise proxy_buffer_size and proxy_buffers, or send smaller headers.
- PHP: connect() to unix:/run/php/php8.3-fpm.sock failed (2: No such file or directory)
- The socket path is wrong for your PHP version. List the sockets with ls /run/php/ and use the real file name.
Questions about nginx 502 Bad Gateway
- What is the difference between 502 and 504?
- A 502 means nginx got no usable answer from your app. A 504 means the app took too long. The error log says which one happened and why.
- Is a 502 an nginx bug?
- Almost never. It points to the app or the connection to it. nginx only reports that it could not get an answer.
- Where is the nginx error log?
- Usually /var/log/nginx/error.log. Your server block may set a different path with the error_log line.
Free tools that help
- Common ports listSearch a list of common TCP and UDP port numbers: SSH 22, HTTP 80, HTTPS 443, MySQL 3306, PostgreSQL 5432 and more, with dev server ports.
Config generators for this
- nginx reverse proxy configSend visitors from port 80 to an app running on port 3000.
- nginx PHP-FPM configRun a PHP site (WordPress, Laravel or plain PHP) on nginx with PHP-FPM.
- nginx HTTPS server blockServe your site over HTTPS with nginx: certificate paths, HTTP to HTTPS redirect, HSTS and a proxy or static site.
- nginx load balancer configSplit traffic across two or three copies of your app with an nginx upstream block.
- php.ini settingsSet the PHP values most sites need: upload size, memory, run time, errors, time zone and OPcache.
- nginx rate limit configSlow down abusive visitors with limit_req: a request rate per IP, a burst, and a 429 answer.