nginx closes connection on some pictures

Solution 1:

The main thing I forgot is to check /var/log/nginx/error.log.

2012/07/12 08:46:44 [crit] 24074#0: *3 open() 
"/var/lib/nginx/proxy/1/00/0000000001" failed (13: Permission denied) 
while reading upstream, client: 109.173.96.30, server: site.com, request: 
"GET /images/theme/front/clean.jpg HTTP/1.1", upstream: 
"http://123.234.123.234:8080/images/theme/front/clean.jpg", 
host: "www.site.com", referrer: "http://www.google.com"

So I fixed /var/lib/nginx/proxy/* directories permissions (sudo chown -R www-data:www-data /var/lib/nginx/proxy/*) and now everything works great. Thank everybody for help.

Solution 2:

A possible reason for the closing of the connection is a slow connection and a short keepalive_timeout. The default value for the keepalive_timeout is 75s. If it is too short and the connection is slow, then it could be closed too early.

http {
    ..
    keepalive_timeout 75;
}

One reason why your image download could be slow is your application. If you are using a Ruby-on-Rails application with an Asset Pipeline in combination with Nginx, then the image download could be slow because you are using the wrong image urls (without fingerprint generated by the asset pipeline). The Rails helpers asset_path and image_tag generate the right urls form files with fingerprint which can be downloaded quickly.

Solution 3:

Another very important thing to check is: Make sure you have disk space left!

For me it was like following:

[user@server]# df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/vda1        30G   29G     0 100% /

For me, nginx was unable to write to the disk eventually causing the same issue! Hope it helps someone!