Search code examples
ruby-on-railsruby-on-rails-3.1nginxamazon-cloudfront

nginx + Rails 3.1 + Cloudfront assets subdirectories 404 missing


I'm using Rails 3.1 asset pipeline which is served using a custom origin Cloudfront CDN.

This is what I have written in my nginx.conf to serve the assets in gzip and for caching:

                location ^~ /assets/ {
                    allow all;
                    gzip_http_version 1.0;
                    gzip_static on;
                    expires 365d;
                    add_header Last-Modified "";
                    add_header ETAg "";
                    add_header Cache-Control public;
            }

The problem is that subdirectories e.g. /background/ in my images asset folder have their items missing with 404s.

When I disable the nginx location config above the problem goes away. How do I configure nginx properly to serve the assets in the subdirectories?

Thanks


Solution

  • This worked for me:

    location ~ ^/(assets)/  {
        root /opt/appname/public; # or whatever the path is to your app's public folder
        gzip_http_version 1.0;
        gzip_static on;
        access_log off;
        expires 1y;
        add_header Cache-Control public;
        add_header Last-Modified "";
        add_header ETag "";
        break;
    }