I am attempting to migrate to nginx from Apache using Passenger both in instances to host a Rails application. The application requires a request, that is to have an image- when the image is available at /system/logos/$requestedimage it should get offered, or it ought to be permitted hitting the Rails application to create it as needed (where this will make it cached to /system/logos).

In Apache I made use of the next:

RewriteCond %{DOCUMENT_ROOT}/system/logos/%{REQUEST_FILENAME} -f
RewriteRule ^/(.*)$ http://assets.clg.eve-metrics.com/system/logos/$1

This labored fine. The assets. subdomain is yet another subdomain but with similar root, just Passenger disabled, particularly setup for hosting static files (expires-smart).

In nginx I'm while using following:

server {
  listen 80;
  passenger_enabled on;
  server_name  clg.eve-metrics.com www.clg.eve-metrics.com;
  root /opt/www/clg/current/public;
  gzip             on;
  gzip_min_length  1000;
  gzip_proxied     expired no-cache no-store private auth;
  gzip_types       text/plain application/xml text/css application/javascript;
  gzip_disable     msie6;
  error_page   500 502 503 504  /50x.html;
  location = /50x.html {
  }
  if (-f $document_root/system/logos$request_filename) { 
    rewrite ^/(.*)$ http://assets.clg.eve-metrics.com/system/logos/$1 break;
  }
}

This does not work so well. Whatsoever, actually. It never redirects towards the cached path also it never hits the Rails application. It's like nginx is presuming it is a static resource so not passing it onto Passenger. It is possible to method to stop this behavior therefore it hits the application?

My rails application is running on nginx and passenger. I've moved my rails cache directory in the default /public to /public/system/cache/. To really make it work, I needed to place this into my vhost config file:

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

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

I recall which i too attempted to really make it use $request_filename, but did not get results. Try with $uri rather and find out whether it works :-)

James, please do this configuration file https://gist.github.com/711913 and give consideration about this location config:

  location ~* \.(png|gif|jpg|jpeg|css|js|swf|ico)(\?[0-9]+)?$ {
      access_log off;
      expires max;
      add_header Cache-Control public;
  }

passenger will not let Rails to handle your assets files for those who have right permissions (user run nginx should has permissions to gain access to to file for directly)