Nginx: 413 – Request Entity Too Large Error and Solution

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?

The error “413 – Request Entity Too Large” indicates that web server configured to restrict large file size. Nginx can be set to allow the maximum size of the client request body using client_max_body_size directive. If the size of a request exceeds the configured value, the 413 (Request Entity Too Large) error returned to the client. You will see an error as follows:

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

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
Use nano text editor:
$ sudo nano /etc/nginx/nginx.conf
Must be run as root:
# 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;

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).
Save and close the file. Reload the nginx webserver, enter:
# /usr/local/nginx/sbin/nginx -s reload
Use nginx itself to reload it:
# /sbin/nginx -s reload
For RHEL/CentOS/Debian/Ubuntu Linux, try:
# service nginx reload
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/
See also Fix client intended to send too large body: xyz bytes in Nginx.

🐧 If you liked this page, please support my work on Patreon or with a donation.
🐧 Get the latest tutorials on SysAdmin, Linux/Unix, Open Source/DevOps topics:
CategoryList of Unix and Linux commands
File Managementcat
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilitiesdig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
45 comments… add one
  • janus rokkjær Nov 25, 2015 @ 18:25

    Does not work any of the solutions sorry back to the writing board

  • Japo Domingo Jan 20, 2016 @ 22:43

    Thanks!

  • Syamala Jul 20, 2016 @ 6:48

    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)

  • xyz Aug 5, 2016 @ 9:49

    thanks

  • Sascha Oct 14, 2016 @ 12:29

    Thanks!

  • Yogesh Oct 23, 2016 @ 20:48

    Thank you very much.

  • Ricoyap Nov 2, 2016 @ 11:34

    it says “VI is not recognized as an internal or an external command,operable program or batch file” when i type it in CMD Windows 7 64 bit,can u help me?

    • E. Nov 8, 2016 @ 11:18

      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.

      • patrick Jan 16, 2017 @ 10:09

        just use another editor like nano

  • Michael Pankratov Nov 24, 2016 @ 17:53

    Take a look at this file for default values as well:
    /etc/nginx/sites-available/default_ssl.conf

  • Vincent Dec 6, 2016 @ 23:14

    Very helpful, thanks :)!

  • DockerSymfony Feb 2, 2017 @ 13:09

    It seems even after all these years, this fix is still valid!

    Thanks!

  • Ceci T. Feb 28, 2017 @ 16:35

    Thanks, it works!

  • Egor Apr 8, 2017 @ 21:52

    I don’t even have this nginx file anywhere. I use Godaddy shared hosting. Please helpl.

    Thank you!

    • 🐧 Vivek Gite Apr 9, 2017 @ 13:47

      In that case contact Godaddy support. They will help you. This tutorial is for sysadmin/devops with server shell access.

  • Luis Lpez Aug 31, 2017 @ 5:08

    Cool, thanks. It works. For Ubuntu, the second option of command always works.

  • Han Li Oct 8, 2017 @ 17:32

    Having upload issue with Drupal running in a new ec2 instance. after modifying the nginx and php config, it now accepts upload with 100M.

    This post really helps a lot. Thanks for sharing!

  • amaru Nov 23, 2017 @ 13:13

    work fine

  • Alejandro Ferguson Nov 10, 2020 @ 22:45

    thank you very much!

    Adding the following line helped to fix my issue:
    # set client body size to 2M #
    client_max_body_size 2M;

    On the nginx.conf file

    ##########
    
    If this doesn't work for any of you i recommend the following (Just if you are on a Nginx server)
    
    - On : /etc/nginx/sites-available/website.com.conf
    and add client_max_body_size 2M;
    - Also if the above didn't work try: Go to /var/www/website.com/public/wp-config.php 
    and add client_max_body_size 2M;
    
    One of this shoudl work

Leave a Reply

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

Use HTML <pre>...</pre>, <code>...</code> and <kbd>...</kbd> for code samples.