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.
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_passhas 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_timeoutdefaults to 60s and is measured between two successive reads, not over the whole response.proxy_connect_timeoutalso 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_buffersbuffer (default4 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
The log line separates a dead backend from a slow one or a size limit, so you avoid changing settings blindly.
- Read the last lines of the error log. The path is whatever
error_logsays in your config.sudo tail -n 50 /var/log/nginx/error.log - From the nginx host, call the backend directly. Replace PORT with the port in
proxy_pass.curl -i http://127.0.0.1:PORT/ - Check that something is actually listening on that port.
sudo ss -ltnp
2.Fix 502 when the backend is down
This is the most common 502: nothing is answering where nginx is looking.
- Start or repair the backend app. If it runs in Docker, confirm the container is running and on the same network as nginx.
- Make
proxy_passmatch the real address and port of the backend. - Test the config and reload nginx.
sudo nginx -t && sudo nginx -s reload
3.Fix 504 timeouts
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.
- Raise the read timeout in the relevant
locationblock. For PHP-FPM usefastcgi_read_timeoutinstead.proxy_read_timeout 120s; - 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.
proxy_connect_timeout 90s; - Test the config and reload nginx.
sudo nginx -t && sudo nginx -s reload
4.Fix errors caused by large upstream headers
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.
- Increase the buffers in the
locationorserverblock that proxies to the backend.proxy_buffer_size 16k; proxy_buffers 8 16k; - Test the config and reload nginx.
sudo nginx -t && sudo nginx -s reload
5.Fix 413 Request Entity Too Large
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.
- Set a higher limit in the
http,serverorlocationblock.client_max_body_size 10m; - Test the config and reload nginx.
sudo nginx -t && sudo nginx -s reload
6.Fix 414 and 400 caused by header size
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.
- Raise the header buffer size in the
httporserverblock.large_client_header_buffers 4 16k; - Test the config and reload nginx.
sudo nginx -t && sudo nginx -s reload
7.Optional: retry another upstream and show a custom error page
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.
- Add retry rules to the proxying
location. The default iserror timeout.proxy_next_upstream error timeout http_502 http_504; proxy_next_upstream_tries 3; - Optionally serve your own page for server errors.
error_pageis inherited only if the current level defines none of its own.error_page 500 502 503 504 /50x.html; - Test the config and reload nginx.
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.logand check that no new error lines appear. - Run
sudo nginx -tand 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 withnginx -V) or thenginx-debugbinary. If you redefineerror_logat a lower level withoutdebug, 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
- Module ngx_http_proxy_moduleOfficial · nginx.org
- Module ngx_http_core_moduleOfficial · nginx.org
- A debugging logOfficial · nginx.org
- NGINX Reverse ProxyOfficial · F5 NGINX docs