I have a php nginx web site for which I defined default locations. This works fine except that I can’t get the favicon.ico to be returned. The favicon.ico file is stored at the root of the root directory.

I currently have this:

location / { error_page 404 = @pmwiki; log_not_found off; }
location = /robots.txt { access_log off; log_not_found off; }
location @pmwiki { rewrite ^/(.*)$ /?n=$1 last; }
location ~ ^/(cookbook|local|scripts|wiki.d|wikilib.d) { deny all; }
location ^~ /docs { autoindex on; types { text/plain php; } }
location ~ \.php$ { . . . }
location ~ /\. { deny all; }

I need to add the following, but don’t know what to put in place of the question marks.

location = /favicon.ico { ???? }
  • Probably the only statement you need is root /path/to/directory; specifying the directory containing the file favicon.ico. Mar 26 '19 at 10:06
  • @RichardSmith I already have a root statement above the list of location statements, but when I copy pasted the root statement in place of the ???, it worked. Is there a way to avoid the root path copy ?
    – chmike
    Mar 26 '19 at 10:19
  • Yes. Don't do it. location = /favicon.ico { } will inherit the value of root from the surrounding context. Mar 26 '19 at 10:22
  • @RichardSmith this is strange. I tested this and it didn’t work. Now it works. Thank you very much. If you provide an answer, I’ll accept it.
    – chmike
    Mar 26 '19 at 10:34

You do not need anything between the {}. An empty block will inherit values from the surrounding context for many of the directives, including the current document root.

So the simplest solution to point the URI /favicon.ico to the file /path/to/root/favicon.ico is:

root /path/to/root;
location = /favicon.ico { }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.