How to set a favicon.ico for a specific Virtual Host on Nginx?

Solution 1:

This is how we do it in our specific vhost config (sites-available/[vhostconfigfile]) under the server directive:

location = /favicon.ico {
    alias /var/www/media/images/favicon.X.ico;
}

That way you can put it anywhere you want with no html whatsoever.

The ".X." is not required at all, and only denotes that you can change this filename to anything you like. I simply use the ".X." as a placeholder to identify the specific sub domain that I am referencing. Its purely for organization.

Solution 2:

favicon.ico file should be placed in website root directory which is defined by nginx root directive. Or you could pass URL to favicon by using following code in HTML:

<link rel="shortcut icon" href="http://example.com/myicon.ico" />