18

I have a rails app using Rails 3.2.3, Ruby 1.9.3, and Phusion Passenger with the Nginx module. App users need to upload large files. I added the directive client_max_body_size 500M; to the location block in the nginx.conf (below) and stopped and started nginx through using Ctrl-C to stop nginx and passenger start to restart nginx. However, when I try to upload a file that is 127 mb, I get the error "413 Request Entity Too Large". Can someone let me know what I am overlooking?

Thanks,

My nginx.conf

#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    passenger_root /Users/mcmahling/.rvm/gems/ruby-1.9.3-p125/gems/passenger-3.0.12;
    passenger_ruby /Users/mcmahling/.rvm/wrappers/ruby-1.9.3-p125/ruby;
    include       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  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  165;

    #gzip  on;

    server {
        listen       80;
        server_name  localhost;
        client_max_body_size       4G;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
             root   html;
             index  index.html index.htm;
             client_max_body_size       4G;
                       client_body_buffer_size    128k;
             client_body_temp_path      /usr/local/nginx/client_body_temp;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443;
    #    server_name  localhost;

    #    ssl                  on;
    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_timeout  5m;

    #    ssl_protocols  SSLv2 SSLv3 TLSv1;
    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers   on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}
iblue
  • 29,609
  • 19
  • 89
  • 128
user437969
  • 579
  • 2
  • 11
  • 25

4 Answers4

27

Change client_max_body_size 4G; to 4g; or 500m; insde the server block

Tobias J
  • 19,813
  • 8
  • 81
  • 66
Mike
  • 1,626
  • 1
  • 12
  • 14
  • Thank you for the answer. I changed the value to gb,GB,mb, and MB, respectively; however, I receive an error that those values are invalid. – user437969 May 06 '12 at 14:27
  • 6
    It should just be 4g or 500m. – nil May 07 '12 at 14:46
  • 1
    I had the same problem. Added `client_max_body_size 4g;` inside the `http` block, problem solved. – Matthew Clark May 14 '12 at 20:50
  • 1
    In case you have multiple config files laying around that might also be setting this parameter, use the following find all occurrences of it: `sudo grep -R 'client_max_body_size' ./*` – Joshua Pinter Dec 07 '12 at 17:39
  • Thanks, worked. I put client_max_body_size 4g; in the server block. Don't forget to restart nginx. Also, if you have PHP, you'll have to set the client_max_body_size there too. – Sven Apr 01 '14 at 09:51
  • Maybe I misunderstand something here, but only with allowing bigger files the problem doesn't go away, does it? If I try to upload 1001mb, then the problem will come again, doesn't it? – Joshua Muheim Jul 29 '14 at 17:25
  • Can someone explain what a `g` and a `m` are? Since you're increasing the size from 4g to 500m I wouldn't imagine that means it's chaning from 4gb to 500mb as that's obviously decreasing the size. – Catfish Aug 15 '14 at 17:40
  • @MatthewClark Is it important to add `client_max_body_size 4g;` in which part of the config file? I've added it but I have still the same problem! I'm using Rails with Nginx on our own server. – Aboozar Rajabi Feb 12 '18 at 15:14
4

Judging from Nginx Error 413 and http://forum.slicehost.com/index.php?p=/discussion/1714/nginx-413-when-uploading-file-1mb-or-larger/p1, you might try having client_max_body_size specified just a single time, possibly only in the server section, although that seems dubious.

Also, as mentioned in another response, I think you want '4g' as the value.

Community
  • 1
  • 1
muffinista
  • 6,676
  • 2
  • 30
  • 23
0

This is just a wild guess, and since I know neither Rails, Ruby nor nginx, I can not really find out if my idea applies, but here is it anyway, maybe it helps...

Did you check by what method the files are transfered? Your problem reminds me of a situation where my Ajax-requests failed mystically. After some research, I found out all requests where sent via GET (which was the wrong way to do it in the first place). Changing it to POST solved my problem then.

Since you are sending files there, it might be a totally different thing (I had data about 2KiB in size or so).

kratenko
  • 7,354
  • 4
  • 36
  • 61
0

client_max_body_size 10m; works for me, with ';' at the and, and restart Nginx and Unicorn, or apache...etc

Like @MaffooClock says, inside the http block.