How can I return a 204 rather than 404 for a missing favicon.ico file in Nginx? How can I return a 204 rather than 404 for a missing favicon.ico file in Nginx? nginx nginx

How can I return a 204 rather than 404 for a missing favicon.ico file in Nginx?


I think this is what you're looking for:

location = /favicon.ico {         try_files $uri = 204;}

It will try to find the file at the URI or 204 as fallback. Here's the relevant documentation.


A simple

location /favicon.ico { return 204; }

would be the easiest.


The important bit is that the = and the HTTP status Code need to be one word and there must not be any space between. If you leave a space between, your error.log will fill quickly as 95% of all requests have a favicon.ico of some sort.

The following should be saved for reusability and just included where necessary.

# (i) No need to log presence or absence.location = /favicon.ico {  access_log     off;  log_not_found  off;  try_files      $uri  =204;}