,

Increase File Upload Limit in Nginx for WordPress

This tutorial guides students and new users on how to fix the common WordPress error “413 Request Entity Too Large” when uploading files via Nginx. The error arises from attempting to make a request larger than the server limit. The solution includes adjusting the server settings to process larger requests, modifying the WordPress directory permissions,…

This brief tutorial shows students and new users steps to resolve a common error with WordPress when trying to upload files, media, and other data and get shown an error that says “413 Request Entity Too Large” via Nginx.

This error, 413 request entity too large, occurs when you try to upload or make a client request that is too large to be processed by the web server, in the case of an Nginx HTTP server.

If the server setting has a request size limit that is too small, your users/clients may come across this error a lot. So you will probably want to adjust the web server settings to allow larger requests.

One of the common issues web admins encounter when managing WordPress is allowing the server to allow file upload via the WordPress media library.

WordPress allows users to upload new themes and plugin files; however, if your Nginx-powered website isn’t configured to allow the large file to be uploaded, the upload process will always fail.

Some common errors users get when dealing with file uploads with WordPress are:

  • Http error attempting to upload media
  • the uploaded file exceeds the upload_max_filesize directive in php.ini
  • maximum execution time exceeded
  • allowed memory size exhausted and many more.

Before continuing with the steps below, please back up your system.

Configure WordPress directory permissions

First, ensure the directory WordPress is running in has the correct permission for the Nginx web server to operate. On Ubuntu systems, the root directory is almost always at /var/www/html.

So, run the commands below to give Nginx web server full access to that directory.

sudo chown -R www-data:www-data /var/www/html/
sudo chmod -R 755 /var/www/html/

Adjust PHP-FPM settings

Next, adjust PHP-FPM settings to allow larger file uploads. PHP-FPM can only upload a specific file size by default, so there’s a limit. Adjust the file upload size limit and other directives in PHP-FPM.

On Ubuntu 18.04 and up, the default PHP-FPM configuration file is stored in the file below:

/etc/php/7.x/fpm/php.ini

The x in the line above can either be a 0 or 1, 2, 3 or 4

So, open the PHP-FPM configuration file by running the commands below and adjusting the settings to suit your environment.

sudo nano /etc/php/7.2/fpm/php.ini

Then scroll down the file line by line and adjust each directive with the value below:

memory_limit = 256M
post_max_size = 32M
upload_max_filesize = 100M
file_uploads = On
max_execution_time = 600
max_input_time = 600

Save your changes.

If the file size you’re uploading is greater than 100MB, then adjust the upload_max_filesize to be greater than the file size.

Adjust Nginx configurations

Nginx also has limited definitions.

If you don’t define the size limit in the Nginx configuration, whatever you do in the PHP configuration file may not apply to Nginx. To allow Nginx also to upload a larger file, open Nginx configuration and add the values as defined below:

On Ubuntu systems, Nginx default site configuration files are stored at /etc/Nginx/sites-available/default

If you have a custom file in there, adjust the highlighted values also.

server {
    listen 80;
    listen [::]:80;
    root /var/www/html/wordpress;
    index index.php index.html index.htm;
    server_name example.com www.example.com;

    client_max_body_size 100M;

location / {
    try_files $uri $uri/ /index.php?$query_string;
   }

location ~ \.php$ {
      include snippets/fastcgi-php.conf;
      fastcgi_pass unix:/var/run/php/php7.1-fpm.sock;
      fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
      include fastcgi_params;
      fastcgi_connect_timeout 300s;
      fastcgi_read_timeout 300s;
      fastcgi_send_timeout 300s;
  }
}

Save the file and continue.

Finally, restart Nginx and PHP-FPM for the new settings to take effect

sudo systemctl reload nginx.service
sudo systemctl reload php7.2-fpm.service

Now go and try to upload the file you want with a size smaller than 100MB

This should do it.

Richard Avatar

Comments

  1. It is working. Thank you so much!

  2. moudrix Avatar

    good tutorial and this is working great.
    please update command for reload nginx, use service not serive.
    btw sorry for my bad english.

  3. Alex Teixeira Avatar
    Alex Teixeira

    Thanks, it works to me…

  4. deekgrow Avatar

    Thank, it works to me too…

Leave a Reply

Your email address will not be published. Required fields are marked *