How to Fix “Nginx Request Line Too Large (6060 > 4094)” Error: Header Buffer Configuration Guide


2 views

The error message you're seeing (Request Line is too large (6060 > 4094)) occurs when Nginx receives an HTTP request where the request line (method + URI + protocol) exceeds its default buffer limits. This commonly happens with:

  • API endpoints with long query strings
  • Batch operations with multiple parameters
  • Complex routing patterns

Nginx has two critical directives controlling header buffers:

client_header_buffer_size 8k;      # Default: 1k
large_client_header_buffers 4 8k;  # Default: 4 8k

The 4094 limit comes from the default request line size restriction in Nginx's source code (NGX_HTTP_MAX_REQUEST_LINE).

Here's a production-tested configuration to handle large request lines:

http {
    # Increase buffer sizes
    client_header_buffer_size 16k;
    large_client_header_buffers 8 32k;
    
    # Optional: Increase timeout for slow clients
    client_header_timeout 60s;
    
    # Required for HTTP/2
    http2_max_field_size 64k;
    http2_max_header_size 256k;
}

For API endpoints handling package queries (like your case), consider these optimizations:

location /api/categorize {
    # Specific buffer settings for this endpoint
    client_header_buffer_size 32k;
    large_client_header_buffers 16 64k;
    
    # Alternative: Process POST instead of GET for huge queries
    if ($request_method = GET) {
        return 405 "Use POST for large package lists";
    }
}

When dealing with very large request lines (10KB+), consider:

  1. Switching from GET to POST requests
  2. Implementing pagination or batch processing
  3. Using request body instead of query parameters
  4. Setting up an API gateway for request transformation

After configuration changes, verify with:

sudo nginx -t
sudo systemctl reload nginx

Test with a long URL using curl:

curl -v "http://yourserver/api/categorize?packages=$(for i in {1..500}; do echo -n "package$i,"; done)"

When working with API endpoints that accept large query parameters (like package lists), you might encounter:

Bad Request
Request Line is too large (6060 > 4094)

This typically occurs when the cumulative size of:

  • The HTTP method (GET/POST/etc.)
  • The URI path (/api/categorize)
  • The query string (?packages=...)
  • The HTTP version (HTTP/1.1)

exceeds Nginx's default buffer limitations.

While you've already tried large_client_header_buffers and client_header_buffer_size, the solution requires understanding their interplay:

# Sets buffer size for regular client request headers
client_header_buffer_size 16k;

# Sets maximum number and size of buffers for large headers
large_client_header_buffers 4 32k;

The 4094 byte limit comes from Nginx's default request line length restriction. Unlike header buffers, this requires a different directive:

http {
    # Increase maximum allowed size of the client request line
    client_max_body_size 10M;  # For POST requests
    client_header_buffer_size 16k;
    large_client_header_buffers 4 32k;
    server_names_hash_bucket_size 128;
    
    # Critical for long query strings:
    server {
        listen 80;
        server_name example.com;
        
        # The actual solution:
        large_client_header_buffers 4 32k;
        client_header_buffer_size 32k;
        
        # For HTTP/1.0 compatibility:
        merge_slashes off;
    }
}

For an API endpoint receiving large package lists:

location /api/categorize {
    # Specifically for this endpoint
    client_header_buffer_size 64k;
    large_client_header_buffers 8 64k;
    
    # Handle the actual request
    proxy_pass http://backend;
    proxy_set_header X-Real-IP $remote_addr;
}

When dealing with extremely large parameter lists:

  • POST instead of GET: Convert to POST with body content
  • Chunking: Split requests into multiple calls
  • Compression: Use gzip for parameter values

After configuration changes:

sudo nginx -t  # Test configuration
sudo systemctl reload nginx  # Apply changes

Check the effective limits with:

curl -v "http://yourserver.com/api/categorize?packages=$(python3 -c 'print("a"*5000)')"