Search code examples
ruby-on-railsnginxassets

nginx assets in subfolders


I was wondering if there is a better way to define assets path in nginx. For example, I want nginx to serve assets in subfolders. For now, I use this technique which does not look very dry to me.

location ^~ /assets/ {
    gzip_static on;
    expires max;
    add_header Cache-Control public;
}

location ^~ /assets/downloads {
    gzip_static on;
    expires max;
    add_header Cache-Control public;
}

Could you suggest something better for folders inside assets folder? Thank you in advance.


Solution

  • Your config is almost right (for what you're trying to achieve). Since I don't have the full config, I'll guess a solution:

    location ^~ /assets/ {
        alias /absolute/path/to/assets/;
        gzip_static on;
        expires max;
        add_header Cache-Control public;
    }
    

    Notice the alias directive (as well as the trailing slash on the absolute path). This tells NGINX to take the URI and recurse the assets directory (searching within subfolders). It's similar to using root and try_files when serving content. For differences between root and alias, see this link.