-1

I have 2 sites: abc.domain.com & def.domain.com

Being subdomains, they share the same cert and also the same root folder. Essentially they go to the same site, but for different clients.

server {
    listen 443 ssl;
    server_name abc.domain.com def.domain.com;

    root /var/www/project;

    location /favicon.ico {
        alias /var/www/project/src/favicon_abc.ico;
        #use /var/www/project/src/favicon_def.ico for def.domain.com;
    }
}

Can I use a different favicon for each site, without creating a separate declaration?

1 Answer 1

1

Sure. You can use either some kind of automatic host-to-filename conversion, e.g. (using the $host variable):

server {
    ...
    server_name abc.example.com def.example.com;
    ...
    location = /favicon.ico {
        # will search for /var/www/project/src/favicon_abc.example.com.ico,
        # /var/www/project/src/favicon_def.example.com.ico, etc.
        alias /var/www/project/src/favicon_$host.ico;
    }
    ...

or even something more complex, e.g. (using regex named capture):

server {
    ...
    server_name ~(?<subdomain>.*)\.example\.com$;
    ...
    location = /favicon.ico {
        # will search for /var/www/project/src/favicon_abc.ico,
        # /var/www/project/src/favicon_def.ico, etc.
        alias /var/www/project/src/favicon_$subdomain.ico;
    }
    ...

or use map directive to hardcode the list of matches (should be defined in http context rather than server one):

map $host $icon {
    abc.example.com  favicon_abc.ico;
    def.example.com  favicon_def.ico;
    ...
    default          favicon.ico; # if none of above match
}
server {
    ...
    server_name example.com *.example.com;
    ...
    location = /favicon.ico {
        alias /var/www/project/src/$icon;
    }
    ...

Moreover, you can use the include directive and populate such a list using some third party tools including it afterwards to nginx config.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Ivan. Using the last method with map worked for me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.