34

I am using Nginx as my webserver for the first time. I didn't have any trouble to set it up and everything works great. The problem came when the designer asked me if he could send me "the icon in the title bar" to "put it up there".

# /opt/nginx/conf/nginx.conf
...
server {
    listen 80 ;
    server_name *.website.com website.com;
    root /home/webuser/sites/website;
}

My directory:

/home/webuser/sites/website/
|_ index.html
|_ main.css
|_ favicon.ico

Is it possible to put a specific favicon.ico to each Virtual Host? Where should you put that file and how can you configure it?

EDIT:

I just realized that it was a completely different problem. Both answers were right but my problem was the permission. I don't know why the file favicon.ico ended up having permissions 600 and of course the moment I did:

chmod +r favicon.ico

Worked like a charm. I will leave this here if it happens to someone else.

ersamy
  • 443
  • 1
  • 4
  • 5

3 Answers3

72

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.

eficker
  • 873
  • 1
  • 7
  • 8
  • 1
    This is perfect for my situation. I have a simple index.htm with links to Webmin and phpVirtualBox for my server and I wanted all 3 to have the same custom favicon. – RyanScottLewis May 17 '12 at 00:03
  • why you need `=` in there? – holms Sep 16 '13 at 17:28
  • 4
    The = may not be *required*, but it may be a modest speedup and is correct. See the docs: http://nginx.org/en/docs/http/ngx_http_core_module.html#location – Scott Stafford Oct 15 '14 at 13:53
  • First, is there a need for the `.X` in `/var/www/media/images/favicon.X.ico`? Second, I've tried this solution exactly as posted, still not working. Perhaps this doesn't work with `auto_index on;` ? Perhaps it requires @AlexD recommendation. – JamesThomasMoon Jul 31 '15 at 04:20
  • You are right, the '.X' is not required. We just have many favicons all located in the same folder for the different sites we run, so this is how we name them uniquely. It should be the exact filename for the file you would like to serve as the 'favicon.ico'. The directive I list above allows for arbitrary naming of the actual file while allowing the webserver to serve the correct filename 'favicon.ico' to the client's browser. – eficker Aug 01 '15 at 05:40
  • The `=` means an exact match; NGINX's fastest processing. – suchislife Dec 20 '21 at 03:41
18

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" />

AlexD
  • 8,179
  • 2
  • 28
  • 38
0

This means, wherever the virtual host's files are taken from (root directory) you should put that specific favicon.ico file.

U4iK_HaZe
  • 631
  • 5
  • 13