Troubleshooting HTTP 400/414/413 Errors in NGINX: A Complete Guide

Common NGINX HTTP Request Size Errors and Their Solutions

Here is a sample nginx.conf configuration file:

user root;
worker_processes 1;

error_log logs/error.log debug;

#pid logs/nginx.pid;

events {
    worker_connections 1024;
}

http {
    #large_client_header_buffers 4 16k;

    server {
        listen 80;
        server_name localhost;

        location / {
            root /root/nginx-quic/html;
            index index.html index.htm;
        }
    }
}

1. Oversized Request Headers (HTTP 400/414)

  • HTTP 400 (Bad Request): The request headers are too large and cannot be parsed.
  • HTTP 414 (URI Too Long): The request line (URI) is excessively long.

Simulating Oversized Request Headers

You can use the following command to simulate a request with an overly large header:

curl -v -H "$(head -c 8192 < /dev/zero | tr '\0' 'A'): value" http://localhost

This command sends a request header containing 8192 characters. The result:

< HTTP/1.1 400 Bad Request
< Server: nginx/1.25.4
< Date: Sat, 23 Nov 2024 13:26:23 GMT
< Content-Type: text/html
< Content-Length: 233
< Connection: close

Check the error.log, and you may see the following error:

2024/11/23 21:26:23 [info] 1494546#0: *1 client sent too long header line: "AAAAAAAAAAAAAAAAAAAAA...

Simulating an Oversized URI

Use this command to simulate a request with an overly long URI:

curl -v "http://localhost/$(head -c 10000 < /dev/zero | tr '\0' 'a')"

This sends a request with a URI containing 10,000 characters. The result:

< HTTP/1.1 414 Request-URI Too Large
< Server: nginx/1.25.4
< Date: Sat, 23 Nov 2024 13:28:47 GMT
< Content-Type: text/html
< Content-Length: 177
< Connection: close

Check the error.log, and you may see this error:

2024/11/23 21:30:40 [info] 1495051#0: *1 client sent too long URI while reading client request line, client...

Solution

Increase the buffer size using the large_client_header_buffers directive:

large_client_header_buffers 4 16k;

2. Oversized Request Body (HTTP 413)

  • HTTP 413 (Payload Too Large): The request body exceeds the allowed size.

Simulating an Oversized Request Body

By default, NGINX sets the client_max_body_size to 1 MB. Create a file larger than the limit and simulate a POST request:

dd if=/dev/zero of=large_file bs=1M count=2
curl -v -X POST -F "file=@large_file" http://localhost

The client will return:

< HTTP/1.1 413 Request Entity Too Large
< Server: nginx/1.25.4
< Date: Sat, 23 Nov 2024 14:33:04 GMT
< Content-Type: text/html
< Content-Length: 183
< Connection: close

Check the error.log, and you’ll see:

2024/11/23 22:33:04 [error] 1495158#0: *7 client intended to send too large body: 2097366 bytes

Solution

Increase the maximum allowed request body size using the client_max_body_size directive:

client_max_body_size 50m;

Summary

Issue Error Code Configuration Option Solution
Oversized request headers or URI 400/414 large_client_header_buffers Increase buffer size or number of buffers
Oversized request body 413 client_max_body_size Increase the maximum allowed request body size

Comments are closed.