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

merlin corey merlincorey at dc949.org
Wed Jan 27 01:22:33 MSK 2010


On Sun, Jan 24, 2010 at 3:27 PM, Ian Terrell <ian.terrell at gmail.com> wrote:
> 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. :)
>
> The conditional
>
> if (-f $document_root/cache$request_uri.html)
>
> does seem 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?
>
> Any help is appreciated!
>
>
> _______________________________________________
> nginx mailing list
> nginx at nginx.org
> http://nginx.org/mailman/listinfo/nginx
>

Ian,

Is try_files incompatible with passenger?  Also, what is wrong with
unicorn (would be able to use try_files if proxying to unicorn).

-- Merlin



More information about the nginx mailing list