skip to content
Skesov.com

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 accept
post_max_size = 100M
; Maximum allowed size for uploaded files
upload_max_filesize = 100M

After saving the changes, restart the PHP-FPM service:

Terminal window
sudo systemctl restart php8.3-fpm

2. 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:

Terminal window
sudo nginx -t
sudo systemctl restart nginx

3. Usage with Docker

When using official images, PHP settings can be overridden via a custom .ini file mounted as a volume:

docker-compose.yml
services:
php:
image: php:8.3-fpm
volumes:
- ./uploads.ini:/usr/local/etc/php/conf.d/uploads.ini

Contents of uploads.ini:

upload_max_filesize = 100M
post_max_size = 100M

Summary

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.