Quick Summary: The HTTP error
413 Request Entity Too Large(or413 Content Too Large) occurs in Nginx when a client attempts to upload a file, image, or API payload whose size exceeds Nginx’s defaultclient_max_body_sizelimit of 1MB (1m). This commonly triggers during file uploads, WordPress theme/plugin installs, Docker registry pushes through a reverse proxy, or Kubernetes Ingress traffic.
Understanding the Root Causes
Before forwarding an HTTP POST or PUT request to your backend application (Node.js, PHP-FPM, Python, or Docker container), Nginx inspects the incoming Content-Length header against its configured body size threshold. If the payload is larger than allowed, Nginx immediately aborts the request with status 413. Common causes include:
- Default 1MB Nginx Limit: Omitting
client_max_body_sizeinnginx.confor virtual host blocks causes Nginx to enforce its restrictive 1MB default. - Configuration Scope Mismatch: Setting
client_max_body_sizeinside onelocationblock while the upload endpoint is handled by anotherserverorlocationblock. - Kubernetes Nginx Ingress Defaults: Running behind an Ingress controller that also defaults to 1MB unless annotated explicitly.
- Downstream Backend Caps: Even after raising Nginx’s limit, upstream runtimes like PHP (
upload_max_filesize,post_max_size) or Express/FastAPI may still reject large bodies.
Step 1: Quick Fix – Increase client_max_body_size in Nginx
Open your Nginx site configuration or main /etc/nginx/nginx.conf and set client_max_body_size in the http, server, or specific location block:
# Edit your virtual host config (Ubuntu/Debian example)
sudo nano /etc/nginx/sites-available/default
# Add client_max_body_size inside the server block (e.g., 50M, 100M, or 0 for unlimited)
server {
listen 80;
server_name example.com;
client_max_body_size 50M;
location / {
proxy_pass http://127.0.0.1:3000;
}
}
# Validate configuration syntax and reload Nginx
sudo nginx -t && sudo systemctl reload nginx
To apply the limit globally across all virtual hosts on the server, place client_max_body_size 50M; directly inside the http { ... } block of /etc/nginx/nginx.conf.
Step 2: Configure Dockerized Nginx and Kubernetes Ingress
If Nginx runs inside a Docker container or as a Kubernetes Ingress controller, update the mounted configuration or Ingress annotations:
# Option A: Docker Compose mounted custom config
# Mount a custom upload.conf into /etc/nginx/conf.d/
echo "client_max_body_size 100M;" > ./nginx-upload.conf
# In docker-compose.yml:
# volumes:
# - ./nginx-upload.conf:/etc/nginx/conf.d/upload.conf:ro
# Restart or reload the Nginx container:
docker compose exec nginx nginx -s reload
# Option B: Kubernetes Nginx Ingress annotation
# Add under metadata.annotations in your Ingress YAML:
metadata:
annotations:
nginx.ingress.kubernetes.io/proxy-body-size: "100m"
Step 3: Align Upstream Backend Limits (PHP-FPM / Node.js)
If Nginx accepts the payload but the upload still fails at the application layer, verify that your backend runtime matches or exceeds Nginx’s new limit:
# For PHP-FPM / WordPress backends, update php.ini:
sudo nano /etc/php/8.2/fpm/php.ini
# Set both post_max_size and upload_max_filesize (>= client_max_body_size):
upload_max_filesize = 50M
post_max_size = 55M
# Restart PHP-FPM to apply changes:
sudo systemctl restart php8.2-fpm
Verification & Testing Steps
Verify the active Nginx configuration and test uploading a payload larger than 1MB using curl:
# 1. Confirm Nginx syntax is valid and inspect active client_max_body_size directives
sudo nginx -t
sudo nginx -T | grep -i client_max_body_size
# 2. Generate a temporary 5MB dummy file and test an HTTP POST upload
dd if=/dev/zero of=/tmp/test_5mb.bin bs=1M count=5
curl -i -F "file=@/tmp/test_5mb.bin" http://localhost/upload
# 3. Clean up the temporary test file
rm -f /tmp/test_5mb.bin
Troubleshooting Summary Comparison
| Environment / Layer | Default Limit | Required Configuration |
|---|---|---|
| Standard Linux Nginx | 1m (1MB) |
Set client_max_body_size 50M; in http or server block |
| Docker Nginx Container | 1m (1MB) |
Mount drop-in /etc/nginx/conf.d/upload.conf and reload |
| Kubernetes Nginx Ingress | 1m (1MB) |
Annotate with nginx.ingress.kubernetes.io/proxy-body-size: "100m" |
| PHP-FPM / WordPress Backend | 2M / 8M |
Raise upload_max_filesize and post_max_size in php.ini |
Leave a Reply