Cameron
Cameron

Reputation: 651

Nginx is ignoring client_max_body_size

For some reason, putting client_max_body_size 16M; in my nginx.conf file has no effect - I'm still getting an HTTP 413 error when I try to upload images to my webserver. I've restarted Nginx after each change to the configuration file, and I've tried putting the client_max_body_size directive in the location {} block, in the server {} block, and in the http {} block. I've even tried all three at the same time.

In looking around for an answer to this, someone suggested looking for client_max_body_size lines in other configuration files, like proxy.conf, which I do not have.

My Nginx config is nothing special - simply upstreaming a connection to a bunch of Rainbows (Unicorns) running on port 8080.

I'm trying to upload a 4.5mb jpg file, running Nginx 1.0.5 on Ubuntu 11.10. Any idea why this isn't working?

UPDATE:

Seems one (?) of the Rainbows workers is restarting every 30 seconds. Here's the output from rainbows.stderr.log:

E, [2012-01-29T17:27:05.977487 #25218] ERROR -- : adding listener failed addr=0.0.0.0:8080 (in use)  
E, [2012-01-29T17:27:05.978011 #25218] ERROR -- : retrying in 0.5 seconds (4 tries left)  
E, [2012-01-29T17:27:06.478767 #25218] ERROR -- : adding listener failed addr=0.0.0.0:8080 (in use)  
E, [2012-01-29T17:27:06.478964 #25218] ERROR -- : retrying in 0.5 seconds (3 tries left)  
E, [2012-01-29T17:27:06.979509 #25218] ERROR -- : adding listener failed addr=0.0.0.0:8080 (in use)  
E, [2012-01-29T17:27:06.979650 #25218] ERROR -- : retrying in 0.5 seconds (2 tries left)
E, [2012-01-29T17:27:07.480190 #25218] ERROR -- : adding listener failed addr=0.0.0.0:8080 (in use)
E, [2012-01-29T17:27:07.480353 #25218] ERROR -- : retrying in 0.5 seconds (1 tries left)  
E, [2012-01-29T17:27:07.980809 #25218] ERROR -- : adding listener failed addr=0.0.0.0:8080 (in use)  
E, [2012-01-29T17:27:07.980987 #25218] ERROR -- : retrying in 0.5 seconds (0 tries left)  
E, [2012-01-29T17:27:08.481638 #25218] ERROR -- : adding listener failed addr=0.0.0.0:8080 (in use)  
/usr/local/forrager/shared/bundle/ruby/1.9.1/gems/unicorn-4.1.1/lib/unicorn/socket_helper.rb:144:in `initialize': Address already in use - bind(2) (Errno::EADDRINUSE)  
from /usr/local/forrager/shared/bundle/ruby/1.9.1/gems/unicorn-4.1.1/lib/unicorn/socket_helper.rb:144:in `new'  
from /usr/local/forrager/shared/bundle/ruby/1.9.1/gems/unicorn-4.1.1/lib/unicorn/socket_helper.rb:144:in `bind_listen'  
from /usr/local/forrager/shared/bundle/ruby/1.9.1/gems/unicorn-4.1.1/lib/unicorn/http_server.rb:222:in `listen'  
from /usr/local/forrager/shared/bundle/ruby/1.9.1/gems/unicorn-4.1.1/lib/unicorn/http_server.rb:733:in `block in inherit_listeners!'  
from /usr/local/forrager/shared/bundle/ruby/1.9.1/gems/unicorn-4.1.1/lib/unicorn/http_server.rb:733:in `each'  
from /usr/local/forrager/shared/bundle/ruby/1.9.1/gems/unicorn-4.1.1/lib/unicorn/http_server.rb:733:in `inherit_listeners!'  
from /usr/local/forrager/shared/bundle/ruby/1.9.1/gems/unicorn-4.1.1/lib/unicorn/http_server.rb:121:in `start'
from /usr/local/forrager/shared/bundle/ruby/1.9.1/gems/rainbows-4.3.1/bin/rainbows:122:in `<top (required)>'  
from /usr/local/forrager/shared/bundle/ruby/1.9.1/bin/rainbows:19:in `load'  
from /usr/local/forrager/shared/bundle/ruby/1.9.1/bin/rainbows:19:in `<main>'  

Upvotes: 1

Views: 2154

Answers (2)

Cameron
Cameron

Reputation: 651

I finally solved this problem with help from a coworker. Turns out the problem was occurring because I had improperly configured Rainbows (Unicorn). In addition to specifying the max body size in the Nginx config, I also should have specified it in my Rainbows config. I had tried putting a call to client_max_body_size in my Rainbows config before, which didn't work. As it turns out, the client_max_body_size call should be placed in a Rainbows! block like so:

Rainbows! do
  client_max_body_size(16 * 1024 * 1024) # 16 megs
end

Problem solved. I can now upload 4 and 5mb files with no issues. Thanks everyone!

Upvotes: 2

Lee
Lee

Reputation: 2242

The bit you said was nothing special, actually is. Make sure the Unicorn process can write to files in /tmp - anything over a certain size is written out to disk instead of being held in memory. So make sure it can make temporary files. See tmpio.rb in Unicorn for detail.

Also, make sure you have client_max_body_size under http {} and nowhere else.

Upvotes: 0

Related Questions