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).

Applies to:nginxopen source (stable and mainline)NGINX Plus
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.

Flow from browser to nginx to backend, with the failure on the nginx to backend link
IllustrationA 502 or 504 is produced by nginx when the backend behind it fails or is too slow.

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, upstream or fastcgi_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 is fastcgi_buffer_size.
Backend slower than the timeout (504)
proxy_read_timeout defaults to 60s. proxy_connect_timeout and proxy_send_timeout also 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_size defaults to 1m, which blocks larger uploads with 413. large_client_header_buffers defaults to 4 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

Easy

The log line tells you which of the causes above applies. These commands only read.

  1. Find the error log path your build uses. Common locations are /var/log/nginx/error.log on Debian, Ubuntu and RHEL, or the container's stderr under Docker.
    Terminal
    nginx -V 2>&1 | sed -n 's/.*--error-log-path=\([^ ]*\).*/\1/p'
  2. Show the latest entries and read the exact message next to the time of your failed request.
    Terminalneeds sudo / root
    tail -n 100 /var/log/nginx/error.log
  3. Check the configuration for syntax errors.
    Terminalneeds sudo / root
    nginx -t

2.Start the backend and confirm the address matches

Easy

Connection refused means nothing is listening where nginx is looking.

  1. Start or restart the application or PHP-FPM using your normal service manager.
  2. Compare the port or socket in the proxy_pass, upstream or fastcgi_pass line with what the app really listens on.
  3. From the nginx host, request the backend directly and confirm it answers (replace the address with yours).
    Terminal
    curl -i http://127.0.0.1:3000/

3.Raise the upstream header buffer

ModerateChanges settings

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.

  1. In the affected location, raise the buffer. For PHP-FPM use fastcgi_buffer_size instead.
    nginx.conf
    proxy_buffer_size 16k;
  2. Test and reload (see the last solution).

4.Handle 504 timeouts

ModerateChanges settings

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.

  1. Raise the read timeout in the affected location (use the fastcgi equivalent for PHP-FPM).
    nginx.conf
    proxy_read_timeout 120s;
  2. Test and reload (see the last solution).

5.Optionally retry another upstream server

AdvancedChanges settings

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.

  1. Add the status codes and cap the number of tries.
    nginx.conf
    proxy_next_upstream error timeout http_502 http_504;
    proxy_next_upstream_tries 2;
  2. Test and reload (see the last solution).

6.Fix 413, 414 and 400 size limits

ModerateChanges settings

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.

  1. For 413, set a larger upload limit in http, server or location. A value of 0 disables the check.
    nginx.conf
    client_max_body_size 10m;
  2. For 414 or 400 on long URLs or headers, raise the header buffers.
    nginx.conf
    large_client_header_buffers 4 16k;
  3. Test and reload (see the last solution).

7.Test the change before applying it

EasyChanges settings

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.

  1. Run the config test.
    Terminalneeds sudo / root
    nginx -t
  2. Only if it passes, reload nginx using your normal service manager.

8.Turn on debug logging if the log is not enough

AdvancedChanges settings

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.

  1. Check that your binary supports it: nginx -V must list --with-debug. Linux packages often ship a separate nginx-debug binary.
    Terminal
    nginx -V 2>&1 | grep -- --with-debug
  2. Set the log level to debug and limit it to your address in the events block.
    nginx.conf
    events {
        debug_connection 203.0.113.10;
    }
  3. If you redefine error_log in 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.log while you retry: no new error lines should appear.
  • nginx -t reports 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

  1. Module ngx_http_proxy_moduleOfficial · nginx.org
  2. Module ngx_http_core_moduleOfficial · nginx.org
  3. A debugging logOfficial · nginx.org
  4. Configuring LoggingOfficial · F5 NGINX docs
  5. nginx 502 Bad Gateway: causes and how to fix it · Netdata