Raju Vishwas
Raju Vishwas

Reputation: 2511

413 Request Entity Too Large - File Upload Issue

I am trying to upload 30MB file on my server and its not working.

  1. When I upload 30MB file, the page loads "Page Not Found"

  2. When I upload a 3MB file, I receive "413 Request Entity Too Large" with nginx/0.6.32

I am trying to find nginx so I can increase "client_max_body_size" but I am unable to find nginx installed on my server. I even tried running:

vi /etc/nginx/nginx.conf

or

vi /usr/local/nginx/conf/nginx.conf

to check if the config file exists, but I couldnt find it on my server.

Is there anyway to resolve this issue? Or do I have to installed nginx on my server.

EDIT:

I have made all necessary changes in my php.ini files,

post_max_size 128M
upload_max_filesize 100M
memory_limit 256M

Thanks, Raju

Upvotes: 233

Views: 388978

Answers (16)

Teodorico Maziviala
Teodorico Maziviala

Reputation: 426

For anyone who comes across this issue when running WordPress over docker, the setup is a bit different because we do not have a php.ini file, and here is how you can fix it:

  1. Exec into your container (running WordPress) and copy the contents of your .htaccess file with:
 docker compose exec wordpress bash

or if you are running the pure docker container:

  docker exec -it <container_name/ID> bash
  1. create the file in your host in the same location from where you are running your docker-compose or you can map the path for more convenience. Add the copied values from your container and include these lines:
  post_max_size = 20M  
  upload_max_filesize = 10M

you should have a final file like this:

# BEGIN WordPress
php_value upload_max_filesize 512M
php_value post_max_size 1024M

RewriteEngine On
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

# END WordPress

Next, mount this file .htaccess mapping it with the location of the original.

here is my docker-compose example for a production-ready wordpress setup.

services:
  wordpress:
    image: wordpress:latest
    container_name: container_name
    ports:
      - "8003:80"
    environment:
      WORDPRESS_DB_HOST: WORDPRESS_DB_HOST:PORT
      WORDPRESS_DB_NAME: WORDPRESS_DB_NAME
      WORDPRESS_DB_USER: WORDPRESS_DB_USER
      WORDPRESS_DB_PASSWORD: WORDPRESS_DB_PASSWORD
      WORDPRESS_AUTH_KEY: 'WORDPRESS_AUTH_KEY'
      WORDPRESS_SECURE_AUTH_KEY: 'WORDPRESS_SECURE_AUTH_KEY'
      WORDPRESS_LOGGED_IN_KEY: 'WORDPRESS_LOGGED_IN_KEY'
      WORDPRESS_NONCE_KEY: 'WORDPRESS_NONCE_KEY'
      WORDPRESS_AUTH_SALT: 'WORDPRESS_AUTH_SALT'
      WORDPRESS_SECURE_AUTH_SALT: 'WORDPRESS_SECURE_AUTH_SALT'
      WORDPRESS_LOGGED_IN_SALT: 'WORDPRESS_LOGGED_IN_SALT'
      WORDPRESS_NONCE_SALT: 'WORDPRESS_NONCE_SALT'
      WORDPRESS_TABLE_PREFIX: wp_
      WORDPRESS_DEBUG: 1
      WP_HOME: https://WP_HOME.com
    volumes:
      - wordpress_data:/var/www/html
      - ./wp-config.php:/var/www/html/wp-config.php
      - /.htaccess:/var/www/html/.htaccess
    restart: unless-stopped
volumes:
  wordpress_data:

for nginx:

location / {
    proxy_pass http://your_host_name:your_port;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Host $server_name;
    proxy_set_header X-Forwarded-Proto $scheme;
    proxy_redirect off;
    client_max_body_size 24000M;
    server_tokens off;
}

This should help things out and get you started quickly.

Upvotes: 1

Dieter
Dieter

Reputation: 3831

Source: cybercity

Edit the conf file of nginx:

nano /etc/nginx/nginx.conf

Add a line in the http, server or location section:

client_max_body_size 100M;

Don't use MB it will not work, only the M! You can test the nginx config by:

sudo nginx -t

Then you need to restart or reload nginx:

sudo nginx -s reload

OR

sudo systemctl restart nginx

Upvotes: 383

Shamyrat Pashshyyev
Shamyrat Pashshyyev

Reputation: 174

First of all you need to pay attention to the response status code:

  • If it says "413 Content Too Large" it is returned by php itself (php.ini file)
  • If it says "413 Request Entity Too Large", then it is returned by nginx (if you use one).
  • If it says "403 Forbidden" (if you have Modsecurity installed), then most likely it is returned by Modsecurity.

  • If it is returned by php, then you'll need to change:
  • "post_max_size"(applies for all the files sent inside the request)
  • "upload_max_filesize" (applies for an individual file separately)

  • In either
  • "/etc/php/8.2/fpm/php.ini"
  • Or
  • "/etc/php/8.2/cli/php.ini" (Nginx servers usually work with fpm).

  • If it is returned by nginx, then you need to add "client_max_body_size 22M;" directive to the .conf file of your website.
    If it is returned by Modsecurity, then you need to change "SecRequestBodyLimit" directive in /etc/nginx/modsec/modsecurity.conf file to something like "SecRequestBodyLimit 23068672" (It is written in bytes)

    Upvotes: 7

    Ashish Mishra
    Ashish Mishra

    Reputation: 83

    In my case, I was using nginx along with letsencrypt to put a ssl layer in front of my spring boot application.

    Putting clint_max_body_size property in "/etc/nginx/nginx.conf" did not work for me. Instead, I put this property inside the site specific conf file.

    sudo nano /etc/nginx/sites-available/mywebsite.com

    server {
        ...
        client_max_body_size 3M;
        ...
    }
    

    Post saving the above, restarted the nginx server and it worked !

    sudo service nginx stop

    sudo service nginx start

    Upvotes: 1

    Imam Hossain Roni
    Imam Hossain Roni

    Reputation: 1056

    Just open this file and ---

    sudo vim  /etc/nginx/nginx.conf
    

    Add a line in the http, server or location section:

    client_max_body_size 100M;
    

    Then check nginx is okey or not by the following command

    sudo nginx -t
    

    If everything looks fine then you will get

    nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
    nginx: configuration file /etc/nginx/nginx.conf test is successful
    

    Then restart nginx

    sudo systemctl restart nginx
    

    I hope your purpose is served :)

    Upvotes: 1

    George G
    George G

    Reputation: 7695

    Anyone looking for a solution for Apache2 (Ubuntu 18 for me), you can edit:

    /etc/apache2/apache2.conf
    

    And find/edit the line:

    LimitRequestBody 7000000 #7mb
    

    Upvotes: 2

    Hiren Parghi
    Hiren Parghi

    Reputation: 1895

    I got the same error and fixed it with the below steps.

    1. At first, edit the nginx.conf file.

      vi /etc/nginx/nginx.conf

    At the HTTP section, added the below line.

    http {
    
        client_max_body_size 100M;
    }
    
    1. Finally restarted Nginx with the below command.

    systemctl restart nginx

    Upvotes: 8

    albus_severus
    albus_severus

    Reputation: 3704

    sudo nano /etc/nginx/nginx.conf
    
    

    Then add a line in the http section

    http {
        client_max_body_size 100M;
    }
    
    

    don't use MB only M.

    systemctl restart nginx
    

    then for php location

    sudo gedit /etc/php5/fpm/php.ini
    

    for nowdays maximum use php 7.0 or higher

    sudo nano /etc/php/7.2/fpm/php.ini     //7.3,7.2 or 7.1 which php you use
    

    check those increasing by your desire .

    memory_limit = 128M 
    post_max_size = 20M  
    upload_max_filesize = 10M
    

    restart php-fpm

    service php-fpm restart 
    

    Upvotes: 26

    Evan
    Evan

    Reputation: 1082

    I add the changes directly to my virtualhost instead the global config of nginx, like this:

       server {
         client_max_body_size 100M;
         ...
       }
    

    And then I change the params in php.ini, like the comments above:

       max_input_time = 24000
       max_execution_time = 24000
       upload_max_filesize = 12000M
       post_max_size = 24000M
       memory_limit = 12000M
    

    and what you can not forget is to restart nginx and php-fpm, in centos 7 is like this:

      systemctl restart nginx
      systemctl restart php-fpm
    

    Upvotes: 18

    Saqib Ashraf
    Saqib Ashraf

    Reputation: 94

    Use:

    php -i
    

    command or add:

    phpinfo();
    

    to get the location of configuration file.

    Update these variables according to your need and server

    max_input_time = 24000
    max_execution_time = 24000
    upload_max_filesize = 12000M
    post_max_size = 24000M
    memory_limit = 12000M
    

    On Linux you will need to restart nginx / apache and phpfpm service so the new ini settings are loaded. On xampp, ammps you can restart these from control panel that comes with such applications.

    Upvotes: 0

    Tai Ho
    Tai Ho

    Reputation: 576

    Open file/etc/nginx/nginx.conf Add or change client_max_body_size 0;

    Upvotes: 0

    Nafas Sait
    Nafas Sait

    Reputation: 41

    I got the upload working with above changes. But when I made the changes I started getting 404 response in file upload which lead me to do further debugging and figured out its a permission issue by checking nginx error.log

    Solution:

    Check the current user and group ownership on /var/lib/nginx.

    $ ls -ld /var/lib/nginx
    

    drwx------. 3 nginx nginx 17 Oct 5 19:31 /var/lib/nginx

    This tells that a possibly non-existent user and group named nginx owns this folder. This is preventing file uploading.

    In my case, the username mentioned in "/etc/nginx/nginx.conf" was

    user vagrant; 
    

    Change the folder ownership to the user defined in nginx.conf in this case vagrant.

    $ sudo chown -Rf vagrant:vagrant /var/lib/nginx
    

    Verify that it actually changed.

    $ ls -ld /var/lib/nginx
    drwx------. 3 vagrant vagrant 17 Oct  5 19:31 /var/lib/nginx
    

    Reload nginx and php-fpm for safer sade.

    $ sudo service nginx reload
    $ sudo service php-fpm reload
    

    The permission denied error should now go away. Check the error.log (based on nginx.conf error_log location).

    $ sudo nano /path/to/nginx/error.log
    

    Upvotes: 2

    Sanaulla
    Sanaulla

    Reputation: 1609

    First edit the Nginx configuration file (nginx.conf)

    Location: sudo nano /etc/nginx/nginx.conf

    Add following codes:

    http {
            client_max_body_size 100M;
    }
    

    Then Add the following lines in PHP configuration file(php.ini)

    Location: sudo gedit /etc/php5/fpm/php.ini

    Add following codes:

    memory_limit = 128M 
    post_max_size = 20M  
    upload_max_filesize = 10M
    

    Upvotes: 38

    Assuming that you made the necessary changes in your php.ini files:

    You can resolve the issue by adding the following line in your nginx.conf file found in the following path:

    /etc/nginx/nginx.conf
    

    then edit the file using vim text editor as follows:

    vi /etc/nginx/nginx.conf
    

    and add client_max_body_size with a large enough value, for example:

    client_max_body_size 20MB;
    

    After that make sure you save using :xi or :wq

    And then restart your nginx.

    That's it.

    Worked for me, hope this helps.

    Upvotes: 7

    Please enter domain nginx file :

    nano /etc/nginx/sites-available/domain.set
    

    Add to file this code

    client_max_body_size 24000M;
    

    If you get error use this command

    nginx -t
    

    Upvotes: 9

    Arun
    Arun

    Reputation: 1085

    -in php.ini (inside /etc/php.ini)

     max_input_time = 24000
     max_execution_time = 24000
     upload_max_filesize = 12000M
     post_max_size = 24000M
     memory_limit = 12000M
    

    -in nginx.conf(inside /opt/nginx/conf)

    client_max_body_size 24000M
    

    Its working for my case

    Upvotes: 57

    Related Questions