nginx error: how to find the cause and fix 502, 504, 413 and 414
"nginx error" covers several different problems, and the browser page does not say which one you have. Read the last lines of nginx's error.log first. The message there points to the fix: a stopped backend (502), a slow one (504), an upload limit (413) or a header size limit (414/400).
On this page
What it means
When nginx cannot serve a request, it sends an HTTP error to the visitor and writes the real reason to its error log. The browser page only gives a number. The log line gives the cause. A 502, a 504, a 413 and a failed nginx -t look alike when you are stressed, but each has a different fix, so start with the log.
502 is the most common case on reverse-proxy setups. It generally means nginx could not get a valid answer from the application behind it (a Node app, PHP-FPM, a container). The steps below run from read-only checks to config changes. After any config change, test it before you apply it.
Symptoms and causes
You are likely dealing with this problem if:
- The browser shows 502 Bad Gateway, 504 Gateway Time-out, 413 Request Entity Too Large, 414 Request-URI Too Large or 400 Bad Request.
- On a 413 during an upload, the browser may show a connection reset instead of a readable page. The nginx docs note that browsers cannot display this error correctly.
The usual causes, most common first:
- Backend not running or not reachable (502)
- The app or PHP-FPM is stopped, or is not listening on the address or socket named in
proxy_pass,upstreamorfastcgi_pass. - Backend crashes or resets the connection (502)
- The app crashes, is killed for running out of memory, or its workers time out.
- Response headers too large (502)
- Headers from the backend exceed
proxy_buffer_size(default 4k or 8k), so nginx treats the response as invalid. For PHP-FPM the matching directive isfastcgi_buffer_size. - Backend slower than the timeout (504)
proxy_read_timeoutdefaults to 60s.proxy_connect_timeoutandproxy_send_timeoutalso default to 60s. The read and send timeouts apply between two successive reads or writes, not to the whole response.- Request too big (413, 414, 400)
client_max_body_sizedefaults to1m, which blocks larger uploads with 413.large_client_header_buffersdefaults to4 8k: a request line that is too long gives 414, a header field that is too long gives 400.
How to fix it
Work through these in order. They start with the quickest, safest checks and move to deeper repairs; stop as soon as the problem is gone.
1.Read the error log and test the config
The log line tells you which of the causes above applies. These commands only read.
- Find the error log path your build uses. Common locations are
/var/log/nginx/error.logon Debian, Ubuntu and RHEL, or the container's stderr under Docker.nginx -V 2>&1 | sed -n 's/.*--error-log-path=\([^ ]*\).*/\1/p' - Show the latest entries and read the exact message next to the time of your failed request.
tail -n 100 /var/log/nginx/error.log - Check the configuration for syntax errors.
nginx -t
2.Start the backend and confirm the address matches
Connection refused means nothing is listening where nginx is looking.
- Start or restart the application or PHP-FPM using your normal service manager.
- Compare the port or socket in the
proxy_pass,upstreamorfastcgi_passline with what the app really listens on. - From the nginx host, request the backend directly and confirm it answers (replace the address with yours).
curl -i http://127.0.0.1:3000/
3.Raise the upstream header buffer
Fixes upstream sent too big header while reading response header from upstream.
Before you start: Back up the config file first. A bigger buffer hides oversized headers (often huge cookies) rather than removing them.
- In the affected
location, raise the buffer. For PHP-FPM usefastcgi_buffer_sizeinstead.proxy_buffer_size 16k; - Test and reload (see the last solution).
4.Handle 504 timeouts
The backend answered, but too slowly for nginx's default 60 seconds between reads.
Before you start: Back up the config file first. A longer timeout only hides a slow backend, so look for the slow query or job as well.
- Raise the read timeout in the affected
location(use the fastcgi equivalent for PHP-FPM).proxy_read_timeout 120s; - Test and reload (see the last solution).
5.Optionally retry another upstream server
The default proxy_next_upstream error timeout does not retry on 502 or 504. With more than one server in the upstream group you can add retries.
Before you start: Retries can repeat work on the backend. POST, PATCH and LOCK requests are not retried unless non_idempotent is set. Back up the config first.
- Add the status codes and cap the number of tries.
proxy_next_upstream error timeout http_502 http_504; proxy_next_upstream_tries 2; - Test and reload (see the last solution).
6.Fix 413, 414 and 400 size limits
The request is bigger than nginx's default allows.
Before you start: Back up the config file first. Raise limits only as far as you need.
- For 413, set a larger upload limit in
http,serverorlocation. A value of0disables the check.client_max_body_size 10m; - For 414 or 400 on long URLs or headers, raise the header buffers.
large_client_header_buffers 4 16k; - Test and reload (see the last solution).
7.Test the change before applying it
Testing first stops a typo from taking the site down when the new config is loaded.
Before you start: Loading the new config affects a live server. If nginx -t reports an error, fix it and do not reload.
- Run the config test.
nginx -t - Only if it passes, reload nginx using your normal service manager.
8.Turn on debug logging if the log is not enough
Debug output shows what nginx does with a request when normal messages do not explain it.
Before you start: Debug logs are large and slow the server. Limit them to your own IP and switch them off afterwards.
- Check that your binary supports it:
nginx -Vmust list--with-debug. Linux packages often ship a separatenginx-debugbinary.nginx -V 2>&1 | grep -- --with-debug - Set the log level to debug and limit it to your address in the
eventsblock.events { debug_connection 203.0.113.10; } - If you redefine
error_login an inner block without a level, debug is dropped for that block.
Check that it worked
- Repeat the request that failed and confirm you get a normal response instead of the error page.
- Watch
tail -f /var/log/nginx/error.logwhile you retry: no new error lines should appear. nginx -treports that the syntax is ok and the test is successful.
If it still fails
- Copy the exact error.log line (it names the cause) and search for that line, not the HTTP code.
- Check the application's own logs: a 502 or 504 often starts in the backend, not in nginx.
- If you run NGINX Plus, use F5's official support channels.
- If the config test fails, compare with your last working copy of the file.
Questions
Why does a 413 upload show a connection reset in my browser?
The nginx documentation says browsers cannot display this error correctly while sending a request body, so you may see a reset instead of the page. The log still records the real reason.
Where is the nginx error log?
It depends on the OS and build. /var/log/nginx/error.log is usual on Debian, Ubuntu and RHEL, and container setups often send it to stderr. nginx -V shows the compiled path.
Sources
- Module ngx_http_proxy_moduleOfficial · nginx.org
- Module ngx_http_core_moduleOfficial · nginx.org
- A debugging logOfficial · nginx.org
- Configuring LoggingOfficial · F5 NGINX docs
- nginx 502 Bad Gateway: causes and how to fix it · Netdata