35

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.

3
  • The 600 permission might relate to the umask (umask for that user is set to 077 for example).
    – jcisio
    Sep 5, 2011 at 15:06
  • The permissions issue fixed it for me as well!
    – Kzqai
    Oct 16, 2014 at 16:15
  • Permission issue here as well. Likely due to creating the ico file by uploading a png to a site. Sep 18, 2015 at 8:15

3 Answers 3

19

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

4
  • I have my favicon.ico file in there... still not working
    – ersamy
    Sep 5, 2011 at 14:04
  • 1
    Check grep favicon.ico nginx-{access,error}.log and check output from file favicon.ico - IE doesn't understand anything other than ICO format. Also, you config shows only one VirtualHost.
    – AlexD
    Sep 5, 2011 at 14:15
  • 1
    good with php crap, not good with static files setup
    – holms
    Sep 16, 2013 at 17:27
  • this won't work when feating static file on their own. Feb 9, 2022 at 12:23
74

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.

6
  • 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. May 17, 2012 at 0:03
  • 1
    why you need = in there?
    – holms
    Sep 16, 2013 at 17:28
  • 5
    The = may not be required, but it may be a modest speedup and is correct. See the docs: nginx.org/en/docs/http/ngx_http_core_module.html#location Oct 15, 2014 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. Jul 31, 2015 at 4: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 1, 2015 at 5:40
0

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .