Home > Mobile >  Nginx how to serve digest favicon file when request is for non-digest favicon.ico
Nginx how to serve digest favicon file when request is for non-digest favicon.ico

Time:09-16

I'm using Nginx in front of a rails app. Nginx is serving static assets from public with the following location rule:

location ~ ^/assets/ {
            gzip_static     on;
            gzip            off;
            expires         1y;
            add_header      Cache-Control public;
            add_header      Last-Modified "";
            add_header      ETag "";
            break;
        }

This is working fine for the standard Rails digest assets but every now and then I get a request for /favicon.ico directly which will fail as it is a non-digest asset.

What I'd like to do is use a Nginx location directive to map/alias/rewrite /favicon.ico to /assets/favicons/favicon-somereallylongdigest.ico.

As rails will keep up to the last 3 assets there could be three files which match, I'm not worried about that and are happy to match any of the files found.

I'm not strong on Nginx config so any help is appreciated.

CodePudding user response:

You could use a permanent redirect:

location = /favicon.ico {
    return 301 $scheme://$host/assets/favicons/favicon-somereallylongdigest.ico;
}
  • Related