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

faviconnginxvirtualhost

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.

Best Answer

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