views:

196

answers:

3

I have a Mongrel cluster behind nginx and am trying to get nginx to serve up the static contents of my site with the following rule (RESTful URLs):

    location / {
        if (-f $request_filename) { 
            break; 
        }

        if (!-f $request_filename) {
            proxy_pass http://mycluster$request_uri;
            break;
        }
    }

When I load up a page which has images in it (referenced from /public/images) the requests for the images just hang forever (same goes for JS, CSS, etc). However, if I go straight to an image URL (right click, "view image") all the files are served as expected - and they continue to be served if i turn the Mongrel cluster off. So nginx can clearly see and access all the static files. Also, when looking in firebug while loading a page I can see that double requests are being made for some of the files (not always the same ones!) and that sometimes one of them is served correctly while the other request will hang indefinitely.

If I change the rule to redirect everything to my cluster the statics are all served fine - exactly the same result as if I go to one of the Mongrels directly:

    location / {
        proxy_pass http://mycluster$request_uri;
    }

That's of course not an option as I very much want nginix to serve this content instead. Another strange thing is it seems the files do actually get sent; for example I can see the images in firebug when hovering over the never completing requests. I'm fairly new to nginix so it's likely I've missed something obvious - I've tried my best to find the answer on my own but I really could do with some help!

Many thanks,

JS

P.S. Could it be that nginx and Mongrel are conflicting over who should serve the file?

A: 

Update: Someone on the nginx mailinglist suggested I change the proxy rule from:

location / {
    if (-f $request_filename) {
    break;
}

if (!-f $request_filename) {
    proxy_pass http://mycluster$request_uri;
    break;
}

to:

location / {
    try_files $uri @fallback;
}

location @fallback {
    proxy_pass http://mycluster$request_uri;
}

It seems the "try_files" directive is the recommended way to check for the existence of a static file. Sadly though, this change does nothing to fix my problem.

JS

John Schulze
A: 

If all of your static files (images, css, JS) are in a folder, then you might try:

location ^~ /static/ {
    root /path/to/the/folder/which/has/static/subfolder;
    expires 30d;
} 
racetrack
A: 

What's the purpose of the first block, anyway? It seems like

location / {
    if (!-f $request_filename) {
        proxy_pass http://mycluster$request_uri;
    }
}

...is sufficient to do what you want, which is to hand the request on to your cluster only if a matching file doesn't exist in your document root.

pjmorse