15

When I try to upload a file to my site, I'm getting the Nginx "413 Request Entity Too Large" error, however in my nginx.conf file I've already explicitly stated the max size to be about 250MB at the moment, and changed the max file size in php.ini as well (and yes, I restarted the processes). The error log gives me this:

2010/12/06 04:15:06 [error] 20124#0: *11975 client intended to send too large body: 1144149 bytes, client: 60.228.229.238, server: www.x.com, request: "POST /upload HTTP/1.1", host: "x.com", referrer: "http://x.com/"

As far as I know, 1144149 bytes isn't 250MB... Is there something I'm missing here?

Here's the base Nginx config:

user  nginx;
worker_processes  8;
worker_rlimit_nofile 100000;

error_log   /var/log/nginx/error.log;
#error_log  /var/log/nginx/error.log  notice;
#error_log  /var/log/nginx/error.log  info;

pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
    use epoll;
}


http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    client_max_body_size 300M;
    tcp_nopush      on;
    tcp_nodelay     on;
    server_tokens   off;
    gzip            on;
    gzip_static     on;
    gzip_comp_level 5;
    gzip_min_length 1024;
    keepalive_timeout  300;
    limit_zone   myzone  $binary_remote_addr  10m;

    # Load config files from the /etc/nginx/conf.d directory
    include /etc/nginx/sites/*;
}

And the vhost for the site:

server {
    listen      80;
    server_name www.x.com x.com;

    access_log  /var/log/nginx/x.com-access.log;

    location / {
        index   index.html index.htm index.php;
        root    /var/www/x.com;

        if (!-e $request_filename) {
            rewrite ^/([a-z,0-9]+)$ /$1.php last;
            rewrite ^/file/(.*)$ /file.php?file=$1;
        }

        location ~ /engine/.*\.php$ {
            return 404;
        }

        location ~ ^/([a-z,0-9]+)\.php$ {
            fastcgi_pass    127.0.0.1:9000;
            fastcgi_index   index.php;
            fastcgi_param   SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include     fastcgi_params;
        }
    }
}
EJay
  • 447
  • 2
  • 5
  • 9

4 Answers4

16

Not knowing the version of your nginx build and what modules it was built with makes this tough, but try the following:

  1. Copy your client_max_body_size 300M; line into the location / { } part of your vhost config. I'm not sure if it's overriding the default (which is 1 MB) properly.

  2. Are you using nginx_upload_module? If so make sure you have the upload_max_file_size 300MB; line in your config as well.

A. R. Younce
  • 1,913
  • 17
  • 22
2

My setup was:

php.ini

...
upload_max_filesize = 8M
...

nginx.conf

...
client_max_body_size 8m;
...

The nginx showed the error 413 when it was uploaded.

Then I had an idea: I will not let nginx show the error 413, client_max_body_size set to a value greater than upload_max_filesize, thus:

php.ini

...
upload_max_filesize = 8M
...

nginx.conf

...
client_max_body_size 80m;
...

What happened?

When you upload smaller than 80MB nginx will not display the error 413, but PHP will display the error if the file is up to 8MB.

This solved my problem, but if someone upload a file larger than 80MB error 413 happens, nginx rule.

renedet
  • 287
  • 3
  • 12
  • I've added client_max_body_size 64m; into the http { } part of the conf, restarted and it worked! Thanks! – flunder May 23 '13 at 19:53
  • this resolved it for me. I thought body parser on my node server was the issue but completely forgot about setting this line in nginx – Stephen Tetreault Dec 11 '17 at 18:04
0

Try the following steps to resolve the error.

  • Open the Nginx configuration file (nginx.conf) in a text editor.

    $ sudo nano /etc/nginx/nginx.conf
    
  • Add the directive client_max_body_size under the http block:

    http {
       # Basic Settings
       client max body size 16M;
       ...
    }
    
  • Open nginx default file in a text editor

    $ sudo nano /etc/nginx/sites-enabled/default
    
  • Add the directive client_max_body_size under location block.

    location / {
       ...
       client_max_body_size 100M;
    }
    
  • Restart Nginx using the following command.

    $ sudo systemctl restart nginx
    

Optional: If you have a time-consuming process running on the backend server then you have to adjust the timeout attribute of the server to avoid 504 timeout error.

  • Open the Nginx default file in a text editor

    $ sudo nano /etc/nginx/sites-enabled/default
    
  • Add the directives proxy_connect_timeout, proxy_send_timeout proxy_read_timeout under the location block:

    location /api {
        client_max_body_size 100M;
        proxy_connect_timeout 6000;
        proxy_send_timeout 6000;
        proxy_read_timeout 6000;
        proxy_pass http://localhost:5001;
    }
    
  • Restart Nginx using the following command.

    $ sudo systemctl restart nginx
    
Codemaker2015
  • 12,190
  • 6
  • 97
  • 81
0

I also add that you could define it in the *.php location handler

location ~ ^/([a-z,0-9]+)\.php$ {

Being the "lower" one in the cascading level, it would be an easy way to see if the problem comes from your nginx config or modules.

It sure doesn't come from PHP because the 413 error "body too large" is really a NGinx error.

nembleton
  • 2,392
  • 1
  • 18
  • 20