DevOpsil

Apache 413 Request Entity Too Large: Fixing File Upload Failures With LimitRequestBody And PHP Configuration

Riku TanakaRiku Tanaka4 min read

Apache 413 Request Entity Too Large: Quick Fix Reference

The 413 error means Apache is rejecting your upload before it even hits PHP. Here's exactly what to change and where.

The Problem at a Glance

413 Request Entity Too Large

Two layers can block large uploads:

  1. ApacheLimitRequestBody directive
  2. PHPupload_max_filesize and post_max_size ini settings

Both must be configured. Fixing only one won't solve it.


Apache Configuration

Global Fix (httpd.conf or apache2.conf)

# Default is 0 (unlimited) — but distros often set it low
LimitRequestBody 104857600
# 104857600 bytes = 100MB
<VirtualHost *:80>
    ServerName uploads.example.com
    DocumentRoot /var/www/html

    # Allow up to 100MB uploads
    LimitRequestBody 104857600
</VirtualHost>

Directory-Level Fix

<Directory "/var/www/html/uploads">
    LimitRequestBody 52428800
    # 52428800 = 50MB
</Directory>

.htaccess Fix (if AllowOverride is enabled)

LimitRequestBody 104857600

Common Size Reference

SizeBytes
10MB10485760
50MB52428800
100MB104857600
256MB268435456
512MB536870912

PHP Configuration

php.ini Settings

; File must be smaller than this — set FIRST
upload_max_filesize = 100M

; POST body limit — must be LARGER than upload_max_filesize
post_max_size = 128M

; Max time a script runs (increase for large uploads)
max_execution_time = 300

; Max time waiting for data
max_input_time = 300

; Memory limit — should be >= post_max_size
memory_limit = 256M

Rule of thumb: memory_limit > post_max_size > upload_max_filesize

Find Your Active php.ini

php --ini | grep "Loaded Configuration"
# or
php -r "echo php_ini_loaded_file();"

Override Per Virtual Host (php_admin_value)

<VirtualHost *:80>
    ServerName uploads.example.com

    php_admin_value upload_max_filesize 100M
    php_admin_value post_max_size 128M
    php_admin_value memory_limit 256M
    php_admin_value max_execution_time 300
</VirtualHost>

Note: php_admin_value only works with mod_php. For PHP-FPM, edit the pool's php.ini or use php_admin_value in the FPM pool config.

PHP-FPM Pool Override

; /etc/php/8.1/fpm/pool.d/www.conf
php_admin_value[upload_max_filesize] = 100M
php_admin_value[post_max_size] = 128M
php_admin_value[memory_limit] = 256M
php_admin_value[max_execution_time] = 300

Nginx Proxy in Front of Apache?

If Apache sits behind Nginx, Nginx will reject the request first.

# nginx.conf or server block
client_max_body_size 100M;

Apply Changes

# Test Apache config before reloading
apachectl configtest
# or
apache2ctl -t

# Reload Apache (no downtime)
sudo systemctl reload apache2      # Debian/Ubuntu
sudo systemctl reload httpd        # RHEL/CentOS

# Restart PHP-FPM if you changed pool config
sudo systemctl restart php8.1-fpm

Verify Your Settings Are Active

<?php
// Drop this in a temp file to verify — delete after!
echo 'upload_max_filesize: ' . ini_get('upload_max_filesize') . "\n";
echo 'post_max_size: ' . ini_get('post_max_size') . "\n";
echo 'memory_limit: ' . ini_get('memory_limit') . "\n";
echo 'max_execution_time: ' . ini_get('max_execution_time') . "\n";
# Check Apache LimitRequestBody is parsed
apachectl -S 2>&1 | grep -i limit

Quick Diagnostic Checklist

  • Is LimitRequestBody set in the right context (VirtualHost/Directory)?
  • Is upload_max_filesize set correctly in the active php.ini?
  • Is post_max_size greater than upload_max_filesize?
  • Is memory_limit greater than post_max_size?
  • Did you reload Apache and restart PHP-FPM after changes?
  • Is Nginx (or another proxy) in front with its own body size limit?
  • Are you testing with a hard reload (bypass browser cache)?

The One-Liner Sanity Check

curl -X POST https://yourdomain.com/upload \
  -F "file=@/tmp/testfile.bin" \
  -w "\nHTTP Status: %{http_code}\n" \
  --max-time 60

If you still get 413 after all of the above, check your CDN or WAF — Cloudflare's free plan caps uploads at 100MB by default.

Share:

Was this article helpful?

Riku Tanaka
Riku Tanaka

SRE & Observability Engineer

If it's not measured, it doesn't exist. SLO-driven, metrics-obsessed, and the person who gets paged at 3 AM so you don't have to. Observability isn't optional.

Related Articles

ApacheQuick RefBeginnerNeeds Review

Fix Apache mod_rewrite Not Working

Diagnose and fix Apache mod_rewrite rules not being applied, covering module loading, AllowOverride settings, .htaccess issues, and rewrite logging.

Aareez Asif·
3 min read

More in Apache

View all →

Discussion