September 13th, 2011, by alex

Getting nginx to play nicely with Rails and the static content in /public

I’m far from a good sysadmin. I don’t have the patience to RTFM, I never remember syntax, and I usually don’t have the discipline to document what I do, meaning any sysadmin work usually ends up a frustrating sequence of trial and error.

I’m moving all my stuff from Slicehost to Linode, which I’ve put off long enough because of the above. Vishal is making me use nginx, which is actually surprisingly straightforward.

I quickly ran into a problem, though. mobilefolk.com is a Rails app, with a bunch of back-end stuff we don’t really use, and a whole bunch of static content dumped in the public folder. For example, http://www.mobilefolk.com/whackakitty/. Problem was, that got routed to a Rails action, which doesn’t exist - while http://www.mobilefolk.com/whackakitty/index.html actually led to the right place. I tried rewriting “/” as “index.html”, but predictably, that made the static content work while breaking all Rails actions.

After some unfocused Google searching, the solution is pretty simple:

# check for index.html for directory index
# if its there on the filesystem then rewite
# the url to add /index.html to the end of it
# and then break to send it to the next config rules.
if (-f $request_filename/index.html) {
rewrite (.*) $1/index.html break;
}

(from http://topfunky.net/svn/shovel/nginx/conf/nginx.conf).

Almost makes me want to learn nginx properly!


Add your comment