What nginx errors mean: 502, 504, 413, 414 and 400 explained

nginx shows an error page, or writes a line to its error log, when it cannot pass a request on or refuses it. 413 and 414 mean the request was bigger than a configured limit, and 400 can mean a single header field was too large. Read the error log first, because it tells you which case you have.

Applies to:nginx (open source and Plus)All versions; applies to proxy_pass, FastCGI, uwsgi and SCGI upstreams
On this page

What it means

"nginx error" is not one problem. nginx either returns an error page with a status code to the browser, or writes a line to its error_log, and often both. The code and the log line together tell you what went wrong.

When the complaint is about the request itself, 413 and 414 mean something the client sent was larger than a configured limit, and 400 can mean a single header field was too large. When nginx works as a reverse proxy, 502 and 504 point at the backend application behind it.

This page explains what each one means and gives the first fix to try. For a longer walk through one failure, see nginx error: how to find the cause and fix 502, 504, 413 and 414. For reading the log itself, see Nginx error logs.

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" or "400 Request Header Or Cookie Too Large".
  • A file upload fails with 413 (the nginx docs note that browsers cannot display 413 correctly, so the page may look odd or blank).

The usual causes, most common first:

Backend is down or proxy_pass points at the wrong place
The app crashed, never started, or proxy_pass has the wrong port or socket.
Backend died mid-request
The connection opened but the app closed it before finishing.
Backend is too slow
proxy_read_timeout defaults to 60s and is measured between two successive reads, not over the whole response. proxy_connect_timeout also defaults to 60s.
Response headers from the backend are too large
If the headers do not fit in proxy_buffer_size (4k or 8k), nginx treats the response as invalid.
Request body over client_max_body_size
The default limit is 1m. Larger uploads get 413.
Request line or header too large
A request line longer than one large_client_header_buffers buffer (default 4 8k) gives 414. A single header field, such as a big cookie, over that limit 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 backend

Easy

The log line separates a dead backend from a slow one or a size limit, so you avoid changing settings blindly.

  1. Read the last lines of the error log. The path is whatever error_log says in your config.
    Terminalneeds sudo / root
    sudo tail -n 50 /var/log/nginx/error.log
  2. From the nginx host, call the backend directly. Replace PORT with the port in proxy_pass.
    Terminal
    curl -i http://127.0.0.1:PORT/
  3. Check that something is actually listening on that port.
    Terminalneeds sudo / root
    sudo ss -ltnp

2.Fix 502 when the backend is down

Easy

This is the most common 502: nothing is answering where nginx is looking.

  1. Start or repair the backend app. If it runs in Docker, confirm the container is running and on the same network as nginx.
  2. Make proxy_pass match the real address and port of the backend.
  3. Test the config and reload nginx.
    Terminalneeds sudo / root
    sudo nginx -t && sudo nginx -s reload

3.Fix 504 timeouts

ModerateChanges settings

The backend does not answer within a timeout. Fixing the slow backend is better; raising timeouts only hides it.

Before you start: Longer timeouts keep connections and workers busy longer, which can hurt under load. Back up the config file before editing. Do not lower proxy_connect_timeout (default 60s): a smaller value can cause new 504s on slow connections.

  1. Raise the read timeout in the relevant location block. For PHP-FPM use fastcgi_read_timeout instead.
    nginx.conf
    proxy_read_timeout 120s;
  2. First check that the backend is reachable at all (see the first fix). Only if connects are genuinely slow, raise the connect timeout instead of the read timeout.
    nginx.conf
    proxy_connect_timeout 90s;
  3. Test the config and reload nginx.
    Terminalneeds sudo / root
    sudo nginx -t && sudo nginx -s reload

4.Fix errors caused by large upstream headers

ModerateChanges settings

The first part of the backend response, which holds the headers, must fit in proxy_buffer_size.

Before you start: Larger buffers use more memory per connection. Back up the config before editing.

  1. Increase the buffers in the location or server block that proxies to the backend.
    nginx.conf
    proxy_buffer_size 16k;
    proxy_buffers 8 16k;
  2. Test the config and reload nginx.
    Terminalneeds sudo / root
    sudo nginx -t && sudo nginx -s reload

5.Fix 413 Request Entity Too Large

EasyChanges settings

The upload is larger than client_max_body_size, which defaults to 1m.

Before you start: A high limit lets anyone send large bodies to your server. Pick the smallest value your app needs. Setting 0 disables the check entirely.

  1. Set a higher limit in the http, server or location block.
    nginx.conf
    client_max_body_size 10m;
  2. Test the config and reload nginx.
    Terminalneeds sudo / root
    sudo nginx -t && sudo nginx -s reload

6.Fix 414 and 400 caused by header size

ModerateChanges settings

The request line (414) or a single header field such as a large cookie (400) is bigger than one header buffer.

Before you start: Bigger buffers use more memory per connection. Back up the config before editing.

  1. Raise the header buffer size in the http or server block.
    nginx.conf
    large_client_header_buffers 4 16k;
  2. Test the config and reload nginx.
    Terminalneeds sudo / root
    sudo nginx -t && sudo nginx -s reload

7.Optional: retry another upstream and show a custom error page

AdvancedChanges settings

If you have more than one backend, nginx can try the next one on failure. A custom page replaces the default error screen.

Before you start: By default POST, PATCH and LOCK requests are not retried, unless non_idempotent is set. Enable that only if repeating those requests is safe for your app.

  1. Add retry rules to the proxying location. The default is error timeout.
    nginx.conf
    proxy_next_upstream error timeout http_502 http_504;
    proxy_next_upstream_tries 3;
  2. Optionally serve your own page for server errors. error_page is inherited only if the current level defines none of its own.
    nginx.conf
    error_page 500 502 503 504 /50x.html;
  3. Test the config and reload nginx.
    Terminalneeds sudo / root
    sudo nginx -t && sudo nginx -s reload

Check that it worked

  • Request the same page or repeat the upload and confirm the error code is gone.
  • Watch the log while you test with sudo tail -f /var/log/nginx/error.log and check that no new error lines appear.
  • Run sudo nginx -t and confirm it reports the syntax is ok before and after each change.

If it still fails

  • Turn on debug logging: error_log /path/to/file debug;. It needs a build with --with-debug (check with nginx -V) or the nginx-debug binary. If you redefine error_log at a lower level without debug, debug logging is off there.
  • If the backend itself is failing when you call it with curl, the fault is in the application, not nginx. Look at that app's own logs.

Questions

Is there a maximum for proxy_connect_timeout?

It usually cannot exceed 75 seconds.

Sources

  1. Module ngx_http_proxy_moduleOfficial · nginx.org
  2. Module ngx_http_core_moduleOfficial · nginx.org
  3. A debugging logOfficial · nginx.org
  4. NGINX Reverse ProxyOfficial · F5 NGINX docs