How to Increase Maximum File Upload Size in Nginx and PHP
/ 2 min read
Table of Contents
By default, most web servers and PHP interpreters have strict limits on uploaded file sizes (usually 2 to 8 MB). This leads to “413 Request Entity Too Large” errors in Nginx or limit exceeded warnings in your CMS.
To resolve this issue, you must change settings at both the web server and PHP levels.
1. PHP Configuration
Edit your php.ini file. The path depends on your PHP version and execution method (FPM or Apache). For example: /etc/php/8.3/fpm/php.ini.
Find and modify the following parameters:
; Maximum size of POST data that PHP will acceptpost_max_size = 100M
; Maximum allowed size for uploaded filesupload_max_filesize = 100MAfter saving the changes, restart the PHP-FPM service:
sudo systemctl restart php8.3-fpm2. Nginx Configuration
Nginx has its own limit on the client request body size. If not increased, the request will be rejected before it reaches PHP.
Add the client_max_body_size directive to the http, server, or location block of your configuration file (usually /etc/nginx/nginx.conf):
server { listen 80; server_name example.com;
# Increase the limit to 100 MB client_max_body_size 100M;
# ... rest of configuration}Test the configuration and restart Nginx:
sudo nginx -tsudo systemctl restart nginx3. Usage with Docker
When using official images, PHP settings can be overridden via a custom .ini file mounted as a volume:
services: php: image: php:8.3-fpm volumes: - ./uploads.ini:/usr/local/etc/php/conf.d/uploads.iniContents of uploads.ini:
upload_max_filesize = 100Mpost_max_size = 100MSummary
To ensure large file uploads work correctly, verify that the client_max_body_size in Nginx is equal to or greater than the upload_max_filesize and post_max_size in PHP.