Nginx: 413 Request Entity Too Large Error and Solution

Posted on in Categories , , , , , last updated July 18, 2017

I‘m running nginx as a frond end to php based Apache+mod_fastcgi server. My app lets user upload images upto 2MB in size. When users trying to upload 1.5MB+ size image file using nginx reverse proxy, they are getting the following error on screen:

Nginx 413 Request Entity Too Large

How do I fix this problem and allow image upload upto 2MB in size using nginx web-server working in reverse proxy or stand-alone mode on Unix like operating systems?

Fig.01: 413 - Request Entity Too Large When I am Trying To Upload A File
Fig.01: 413 – Request Entity Too Large When I am Trying To Upload A File

You need to configure both nginx and php to allow upload size.

Nginx configuration

The client_max_body_size directive assigns the maximum accepted body size of client request, indicated by the line Content-Length in the header of request. If size is greater the given one, then the client gets the error “Request Entity Too Large” (413). To fix this issue edit your nginx.conf. Open the Terminal or login to the remote server using ssh client. Type the following command to edit your nginx.conf using a text editor such as vi or joe or nano:
# vi /etc/nginx/nginx.conf
OR
$ sudo nano /etc/nginx/nginx.conf
OR
# vi /usr/local/nginx/conf/nginx.conf
Add the following line to http or server or location context to increase the size limit in nginx.conf, enter:

# set client body size to 2M #
client_max_body_size 2M;

Save and close the file. Reload the nginx webserver, enter:
# /usr/local/nginx/sbin/nginx -s reload
OR
# /sbin/nginx -s reload
OR use the following on RHEL/CentOS/Debian/Ubuntu Linux:
# service nginx reload
OR if you are using systemd based system run:
$ sudo systemctl reload nginx.service

PHP configuration (optional)

Your php installation also put limits on upload file size. Edit php.ini and set the following directives

;This sets the maximum amount of memory in bytes that a script is allowed to allocate
memory_limit = 32M

;The maximum size of an uploaded file.
upload_max_filesize = 2M

;Sets max size of post data allowed. This setting also affects file upload. To upload large files, this value must be larger than upload_max_filesize
post_max_size = 3M

If you are using PHP-FPM, restart it as follows:
$ sudo systemctl restart php-fpm
## OR ##
$ sudo systemctl restart php7.0-fpm.service
## OR ##
$ sudo /usr/local/etc/rc.d/php-fpm restart

Save and close the file. Make sure you reload/restart back-end apache or nginx web server as per your setup. See “PHP Increase Upload File Size Limit” tutorial for more information.

Look out for permission errors too

Apart from setting Nginx and PHP, you need to look out file permission errors. Usually logged into your web server error.log file. Here is a sample error logged in to my lighttpd web server:

2017-07-18 07:09:18: (connections.c.1095) denying upload as opening to temp-file for upload failed: /var/cache/lighttpd/uploads/lighttpd-upload-9UMUXj Permission denied 
2017-07-18 07:23:45: (connections.c.1095) denying upload as opening to temp-file for upload failed: /var/cache/lighttpd/uploads/lighttpd-upload-a4PNg1 Permission denied 
2017-07-18 07:25:50: (connections.c.1095) denying upload as opening to temp-file for upload failed: /var/cache/lighttpd/uploads/lighttpd-upload-xIzdjU Permission denied 
2017-07-18 07:27:30: (connections.c.1095) denying upload as opening to temp-file for upload failed: /var/cache/lighttpd/uploads/lighttpd-upload-2s3I9J Permission denied 
2017-07-18 07:29:04: (connections.c.1095) denying upload as opening to temp-file for upload failed: /var/cache/lighttpd/uploads/lighttpd-upload-9KNsVa Permission denied 
2017-07-18 07:30:19: (connections.c.1095) denying upload as opening to temp-file for upload failed: /var/cache/lighttpd/uploads/lighttpd-upload-myoWrl Permission denied

To fix this you need to setup correct permission using the chmod command or chown command:
$ sudo chown -R lighttpd:lighttpd /var/cache/lighttpd/

Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin and a trainer for the Linux operating system/Unix shell scripting. He has worked with global clients and in various industries, including IT, education, defense and space research, and the nonprofit sector. Follow him on Twitter, Facebook, Google+.

41 comment

  1. when i use
    client_max_body_size 2MB;
    there is something wrong:
    nginx: [emerg] “client_max_body_size” directive invalid value in conf/nginx.conf

    maybe you should use 2m not 2mb

    my nginx version:
    nginx: nginx version: nginx/1.0.5

  2. Thanks for this!

    Something to note: I had `client_max_body_size` being set in a variety of files in the nginx directory. To quickly see if it is being set (and therefore causing overrides or conflicts) you can use the following line in the nginx directory.

    sudo grep -R ‘client_max_body_size’ ./*

    Thanks,

    Josh

  3. @nixCraft
    I would be happy to receive that kind of error (Nginx 413 Request Entity Too Large).

    Unfortunately, my browser (Firefox 20) shows me a page with the message “This page contains no data” when someone tries to upload a file more client_max_body_size

    I have tried to create a custom page 413 and get’s invoked when client_max_body_size get exceeded to no avail.

  4. If you have run bellow command
    # /usr/local/nginx/sbin/nginx -s reload
    OR
    # /sbin/nginx -s reload
    And you are getting error No such file or directory
    Simply restart your nginx
    sudo service nginx restart

    1. This is only if you have a problem with a web application that you host on your own server.

      If it is on a website that is not under your control, then you need to contact the webmaster of that web site.

  5. I increased size to 2M, as mentioned above in client_max_body_size 2M; and reloaded Nginx.
    But i see this error

    500 Internal Server Error

    500 Internal Server Error
    nginx/1.4.6 (Ubuntu)

    1. Are you sure that you’re able running a webserver? Your OS tells you that it doesn’t know something about that command – so what do you think? Right. You have no VI installed. It is – like nginx – a software written for unix/linux not for windows. But there are ports for windows. So even you may use another editor or install VI from its homepage.

Leave a Comment