Web Server Intermediate

Nginx 413 Request Entity Too Large: Troubleshooting client_max_body_size on Alpine Linux

Resolve Nginx 413 errors on Alpine Linux caused by `client_max_body_size` limits. This guide provides step-by-step solutions for web servers and Docker containers.

👨‍💻
Senior Systems Architect • Verified in Staging Labs

Resolve Nginx 413 errors on Alpine Linux caused by `client_max_body_size` limits. This guide provides step-by-step solutions for web servers and Docker containers.

When interacting with a web application served by Nginx, you might encounter an error indicating that your request is too large for the server to process. This typically happens when uploading large files, submitting extensive form data, or sending large API payloads. The server responds with a 413 Request Entity Too Large HTTP status code, preventing the operation from completing. This guide will walk you through diagnosing and resolving this common Nginx error, specifically on Alpine Linux environments, including containerized deployments.

Symptom & Error Signature

The most immediate symptom is a 413 Request Entity Too Large error displayed in your web browser or returned by an API client.

Browser Output Example:

<html>
<head><title>413 Request Entity Too Large</title></head>
<body>
<center><h1>413 Request Entity Too Large</h1></center>
<hr><center>nginx/1.24.0</center>
</body>
</html>

Nginx Error Log Signature: On the server side, checking your Nginx error logs (commonly located at /var/log/nginx/error.log on Alpine, or streamed to stderr in Docker containers) will reveal entries similar to:

2023/10/26 10:30:15 [error] 1234#1234: *50 client intended to send too large body: 5242880 bytes, client: 192.168.1.100, server: example.com, request: "POST /upload HTTP/1.1", host: "example.com"

This log entry explicitly mentions "client intended to send too large body" and specifies the size of the body the client attempted to send (5242880 bytes = 5MB), which exceeds the configured limit.

Root Cause Analysis

The 413 Request Entity Too Large error is almost universally caused by the client_max_body_size directive in your Nginx configuration. This directive sets the maximum allowed size of the client request body, specified in bytes, kilobytes, megabytes, or gigabytes.

By default, Nginx sets client_max_body_size to 1m (1 Megabyte). This default is often insufficient for modern web applications that handle file uploads, large images, video files, or extensive JSON/XML payloads.

Nginx implements this limit as a security measure to prevent denial-of-service (DoS) attacks, where malicious clients might attempt to flood the server with extremely large requests, consuming resources and potentially crashing the server or exhausting its disk space.

The client_max_body_size directive can be configured at different levels within the Nginx configuration hierarchy:

  • http context: Applies globally to all virtual hosts.
  • server context: Applies to a specific virtual host.
  • location context: Applies to requests matching a specific URL path within a virtual host.

The most specific context takes precedence. For example, a location block's client_max_body_size will override a server block's, which will override the http block's.

It's also critical to understand that if your application uses a backend like PHP-FPM, those services often have their own POST body size limits (post_max_size and upload_max_filesize in php.ini) that must also be adjusted to be at least as large as the Nginx client_max_body_size. Failure to do so will result in issues even if Nginx is configured correctly.

Step-by-Step Resolution

Follow these steps to diagnose and resolve the Nginx 413 Request Entity Too Large error on Alpine Linux.

1. Identify the Nginx Configuration File

First, you need to locate the Nginx configuration file(s) that are active on your system. On Alpine Linux, Nginx's main configuration file is typically located at /etc/nginx/nginx.conf. This file often includes other configuration files from directories like /etc/nginx/http.d/ or /etc/nginx/conf.d/.

If Nginx is running inside a Docker container, the configuration might be mounted from a host volume or baked directly into the Docker image. You'll need to access the container to modify its configuration.

To find active configuration files: Connect to your Alpine Linux server or Docker container:

# For a standard Alpine Linux server
ssh user@your_alpine_server

# For a running Docker container
docker exec -it <container_name_or_id> sh

Then, you can use nginx -t to test the configuration and see where the main config file is loaded from:

nginx -t

This command will usually output something like:

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

This confirms /etc/nginx/nginx.conf is the primary file. You might then inspect nginx.conf for include directives to find other configuration files.

2. Modify client_max_body_size in Nginx Configuration

Using a text editor like vi (often default on Alpine) or nano (if installed via apk add nano), open the relevant Nginx configuration file.

# To edit the main Nginx config
vi /etc/nginx/nginx.conf

# To edit a specific virtual host config (example)
vi /etc/nginx/http.d/default.conf

You can place the client_max_body_size directive in the http, server, or location block.

  • Global (http context): Apply to all sites served by this Nginx instance.

    # /etc/nginx/nginx.conf or similar
    http {
        client_max_body_size 50M; # Set maximum client body size to 50 Megabytes
        # ... other http settings
    }
    
  • Per Virtual Host (server context): Apply to a specific domain or site.

    # /etc/nginx/http.d/your_site.conf or similar
    server {
        listen 80;
        server_name example.com;
    
        client_max_body_size 25M; # Set maximum client body size for example.com to 25 Megabytes
    
        # ... other server settings
    }
    
  • Per URL Path (location context): Apply only to requests matching a specific path. This is useful if only certain parts of your application handle large uploads.

    # /etc/nginx/http.d/your_site.conf or similar
    server {
        listen 80;
        server_name example.com;
    
        location /upload_large_files {
            client_max_body_size 100M; # Allow up to 100M for this specific upload path
            # ... proxy_pass or fastcgi_pass directives
        }
    
        location / {
            client_max_body_size 10M; # Default for other paths
            # ...
        }
    }
    

The value 50M means 50 Megabytes. You can use K for kilobytes and G for gigabytes. Choose a value that accommodates your application's needs but avoids excessively large limits for security reasons. A common starting point for file uploads is 25M or 50M. For very large files (e.g., video uploads), you might need 500M or 1G.

3. Test Nginx Configuration Syntax

After modifying the configuration, it's crucial to test its syntax before reloading Nginx. This prevents potential downtime due to a misconfiguration.

nginx -t

A successful test will output:

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

If there are errors, Nginx will point to the line number and provide a description of the issue. Correct any reported errors before proceeding.

4. Reload or Restart Nginx

Once the configuration is tested and confirmed valid, you need to apply the changes by reloading or restarting Nginx.

  • For Nginx running directly on Alpine Linux (systemd or OpenRC):

    If your Alpine server uses systemd (less common, but possible), use:

    systemctl reload nginx
    

    If your Alpine server uses OpenRC (more common for native Alpine installs), use:

    rc-service nginx reload
    

    If you're unsure or reload fails, a full restart might be necessary:

    # systemd
    systemctl restart nginx
    # OpenRC
    rc-service nginx restart
    
  • For Nginx running inside a Docker container:

    The safest method is often to use the nginx -s reload command directly within the container.

    docker exec -it <container_name_or_id> nginx -s reload
    

    If nginx -s reload doesn't work (e.g., Nginx is not running as PID 1 or in a way that responds to signals), you might need to restart the entire Docker container:

    docker restart <container_name_or_id>
    

Using nginx -s reload or systemctl reload nginx is generally preferred over restart, as reload allows Nginx to gracefully shut down old worker processes and start new ones with the updated configuration, minimizing downtime. A full restart will stop all Nginx processes and then start them again, leading to a brief interruption of service.

5. Verify PHP-FPM Configuration (If Applicable)

If your Nginx server is acting as a reverse proxy for a PHP application using PHP-FPM, you must also adjust PHP's own size limits. PHP has two crucial directives in its php.ini file that affect upload sizes:

  • upload_max_filesize: The maximum size of an uploaded file.
  • post_max_size: The maximum size of POST data that PHP will accept. This includes all form data, not just files.

Both of these values must be equal to or greater than your client_max_body_size setting in Nginx. If Nginx allows a 50MB upload but PHP's post_max_size is 8MB, the upload will still fail, often without clear Nginx errors, but potentially with PHP warnings or empty $_FILES arrays.

Locate php.ini: On Alpine Linux, PHP configuration files are typically found in /etc/phpX/php.ini (where X is the PHP version, e.g., php81). In Docker containers, it might be in /usr/local/etc/php/php.ini or similar paths.

# For a standard Alpine PHP install
vi /etc/php81/php.ini

# Common path in Dockerized PHP-FPM images
vi /usr/local/etc/php/php.ini

Modify php.ini: Find and adjust the following lines:

; Maximum allowed size for uploaded files.
upload_max_filesize = 50M

; Maximum size of POST data that PHP will accept.
post_max_size = 50M

Set these values to match or exceed your Nginx client_max_body_size.

Restart PHP-FPM: After modifying php.ini, you must restart the PHP-FPM service to apply the changes.

  • For PHP-FPM running directly on Alpine Linux (systemd or OpenRC):

    # systemd
    systemctl reload php-fpm
    # OpenRC
    rc-service php-fpm reload
    

    (Note: php-fpm service name might vary, e.g., php81-fpm)

  • For PHP-FPM running inside a Docker container: The best practice is usually to restart the PHP-FPM container:

    docker restart <php-fpm_container_name_or_id>
    

    Alternatively, if php-fpm is running as PID 1, you can send a signal to reload workers gracefully (though restarting the container is often simpler):

    docker exec -it <php-fpm_container_name_or_id> kill -USR2 1
    

6. Consider Upstream Proxies and Load Balancers

If your Nginx server is behind another proxy, CDN, or load balancer (e.g., Cloudflare, HAProxy, AWS ELB/ALB), these services often have their own request body size limits. If you've adjusted Nginx and PHP but still face 413 errors, investigate the configuration of these upstream components. For example, Cloudflare's free tier has a 100MB upload limit, which can be increased on paid plans.

7. Retest the Upload/Request

Finally, attempt the operation that previously caused the 413 Request Entity Too Large error. If all configurations have been correctly applied, your request should now succeed. If not, re-check Nginx and PHP-FPM logs for new errors and review all configuration changes.

👨‍💻

Johnathon Wheeler

Senior Systems Architect & DevOps Engineer • Austin, TX

Connect on LinkedIn →

Johnathon has over 16 years of hands-on experience designing, debugging, and scaling Linux web hosting stacks, container clusters, and high-availability database architectures. Every guide on ButItWorkedLocal is independently tested against Debian 12, Ubuntu 24.04/22.04 LTS, Rocky Linux, and Docker environments to guarantee reproducibility in production.

🛡️

Our Production Verification Guarantee

Encountering a bug not covered here or running a non-standard kernel configuration? Our solutions are continually refined against real production incidents. Submit an environment trace for our editorial team to replicate.