What should my Nginx rewrite rules be for Rails with Passenger for page caching in a subdirectory?

I'm using Nginx 0.7.64, Passenger 2.2.9, Rails 2.3.5. I have my page caching directory set to /public/cache, and I'd like to be able to serve cached pages when requested over HTTP, but always hit the Rails app when requested over HTTPS.

The bulk of my config looks like this:

server {
  listen 80;
  server_name website.com www.website.com;
  proxy_set_header X-Forwarded-Proto http;
  root /home/deploy/website/current/public;
  passenger_enabled on;

  if (-f $document_root/cache/$request_filename.html) { 
    rewrite (.*) $document_root/cache/$1.html break;
  }
}

server {
  listen       443;
  server_name website.com www.website.com;
  root /home/deploy/website/current/public;
  passenger_enabled on;
  proxy_set_header X-Forwarded-Proto https;

  ssl                  on;
  ssl_certificate      /home/deploy/website/shared/ssl/www.website.com.combined.crt;
  ssl_certificate_key  /home/deploy/website/shared/ssl/www.website.com.key;
}

I anticipate that when I request website.com/about, I should be served /public/cache/about.html, but instead I hit the Rails server (tailing the log shows it).

Thinking I might have an inappropriate slash (and not seeing $document_root in most examples), I've also tried all of the following variations, none of which work:

if (-f cache$request_filename.html) { 
  rewrite (.*) cache$1.html break;
}

if (-f /cache$request_filename.html) { 
  rewrite (.*) /cache$1.html break;
}

if (-f cache/$request_filename.html) { 
  rewrite (.*) cache/$1.html break;
}

if (-f /cache/$request_filename.html) { 
  rewrite (.*) /cache/$1.html break;
}

I've also thrown the root, passenger_enabled, and rewrite rules into a separate location / block, but that also does not work. I've also reordered the statements so that passenger_enabled would come at the end. I've also tried using $uri. Clearly I'm misunderstanding something!

This is a little bit simplified, since I also have an XML api that is cached in places (presumably the rewrite rule will be the same except for the .html parts), as well as I'll need to serve public/cache/index.html when the root of website.com is requested. I just want to get any single piece of it working. :)

Any help is appreciated!

Update

The conditional

if (-f $document_root/cache$request_uri.html)

Seems to work! However, what I would think would be the rewrite does not work! Trying

if (-f $document_root/cache$request_uri.html) {
  rewrite (.*) /cache$1.html break;
  break;
}

Rewrites the URL as /cache/cache/about.html.html and sends it to Rails, which promptly barfs. It looks doubled, yes! But if I rewrite to just /cache$1 it sends /cache/cache/about to Rails, and $1.html sends /about.html.html to Rails, and just $1 sends simply /about which goes to Rails and does not hit the cache. Obviously this is not correct behavior Is Nginx rewriting it and then Passenger is rewriting it, too?


Solution 1:

The answer was found here: https://stackoverflow.com/questions/1177979/nginx-rewrite-rules-with-passenger

The config ended up being:

# root
if (-f $document_root/cache/$uri/index.html) {
  rewrite (.*) /cache/$1/index.html break;
}

# pages like /about, cached with .html but accessed without
if (-f $document_root/cache/$uri.html) {
  rewrite (.*) /cache/$1.html break;
}

# pages like /api/v1/something.xml, cached as xml
if (-f $document_root/cache/$uri) {
  rewrite (.*) /cache/$1 break;
}

Solution 2:

try_files version:

server {
  listen 80;
  server_name website.com www.website.com;
  location / {
    root /home/deploy/website/current/public;
    try_files $uri /cache/$uri/index.html /cache/$uri.html /cache/$uri @passenger;
  }
  location @passenger {
    root /home/deploy/website/current/public;
    proxy_set_header X-Forwarded-Proto http;
    passenger_enabled on;
  }
}

note that it might or might not work with passenger. I'm sure it works with unicorn, mongrel, etc though.