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:

Adblock detected 😱

My website is made possible by displaying online advertisements to my visitors. I get it! Ads are annoying but they help keep this website running. It is hard to keep the site running and producing new content when so many people block ads. Please consider donating money to the nixCraft via PayPal/Bitcoin, or become a supporter using Patreon.

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;

# 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 allocatememory_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_filesizepost_max_size= 3M

;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

Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.

Your support makes a big difference:

I have a small favor to ask. More people are reading the nixCraft. Many of you block advertising which is your right, and advertising revenues are not sufficient to cover my operating costs. So you can see why I need to ask for your help. The nixCraft takes a lot of my time and hard work to produce. If everyone who reads nixCraft, who likes it, helps fund it, my future would be more secure. You can donate as little as $1 to support nixCraft:

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.

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

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.