Nginx error 413: Request entity too large Quick Fix

A quick article today about a common error people encounter with Nginx: the infamous 413 Request Entity Too Large HTTP error. I had the problem myself just yesterday on one of my newly configured servers, so I thought I'd make a blog post about it to make sure I don't forget this next time.

Error 413: what does it mean?

This error shows up when a visitor sends too much data in the HTTP request. This is normally caused by:
• an overy large file being uploaded by the visitor
• more generally too much POST data being sent by the client
The fix is as simple as setting a directive in your Nginx configuration, read on below.

How to fix it? client_max_body_size

To fix this, you need to increase the value of the client_max_body_size directive. This directive defines the maximum amount of data Nginx will accept in an HTTP request. By default this value is set to 1 megabyte, meaning if you attempt to upload a file larger than 1 megabyte you'll be getting an Error 413: Request entity too large page. You can insert this directive at three levels:

  • In the http block: this will set the directive value for all server and locations in your configurationn
  • In the server block: this will set the directive value for all locations of one particular server
  • In the location block: this will set the directive value for one specific location in a particular server
In this example I'm going to insert it in my http block and set it to 500 megabytes:
http {
    client_max_body_size 500M; # allows file uploads up to 500 megabytes

Anything else I need to do?

After changing your server configuration, don't forget to reload Nginx for the configuration to apply. There are also some configuration settings you need to change if you are using PHP. If you aren't, just ignore the rest of this post. You'll need to open your php.ini configuration file and look for two values:

  • upload_max_filesizeMaximum allowed size for uploaded files (default: 2 megabytes). You need to increase this value if you expect files over 2 megabytes in size.
  • post_max_sizeMaximum size of POST data that PHP will accept (default: 8 megabytes). Files are sent via POST data, so you need to increase this value if you are expecting files over 8 megabytes.

After making the changes, don't forget to reload PHP-FPM as well so that the changes are applied correctly. That's all! You shouldn't be getting any more upload errors after that.


Anonymous said...

Hello and thank you for the information! :)

However I'm not that into the HTML-stuff and not everybody is anyway.

So how exactly can I fix the problem?

Kind regards

Sebastian said...

You need to have access to the nginx config files and know how to edit them. HTML isn't involved in this.
Basically: If you don't know what this post is talking about, there is no way for you to fix it.

Buck Jones said...

What would the fix be in the local block?

chrisdt78 said...

I get what i have to do but im confused on how to get to the files where i put everything any help

Luke Darling said...

Thanks, very thorough tutorial. I knew about PHP's upload_max_filesize, but not the other two problem causers.

David Radovanovic said...

I found your tutorial after being unable to upload DonateNOW WordPress theme. Simple and easy to understand. In my case:

vi /etc/nginx/nginx.conf <-- added new html directive.

sudo nginx -s reload <-- nothing printed to screen to confirm the reload, though it must have worked ;)

vi /etc/php.ini <-- changed the default max directives.

service php-fpm restart <-- it worked after the restart!!


Anonymous said...

On my development machine I put mine in the server block in /etc/nginx/sites-available/mysite
I put just the `client_max_body_size 2M;` part so it effects everything for that project.

This reminds me to do it on the production server when I setup a new dev.

Asysteo said...

I recently have problem with enginx too...after php update i got problem "502 Bad Gateway" and i cant find sollution to solve this

Elangovan S said...

Thanks ...!

413 Request Entity Too Large said...

Here is Quick Fix - http://atulhost.com/fix-nginx-error-413-request-entity-too-large

facebook iniciar sesion celular gratis said...

Thank for sharing!Great! Thanks for such post and please keep
facebook movil gratis and facebook movil and apply facebook iniciar sesion celular

click pakcers said...

Very Good Article

packers and movers hyderbad

packers and movers in hyderbad

movers and packers hyderbad

Click Packers

Click Packers said...

Very Good Article

packers and movers hyderabad

packers and movers in hyderabad

movers and packers hyderabad

Click Packers

Search This Blog