Default page

I´m doing this to show a default favicon.ico, when the site doesn´t have
one:

   location = /favicon.ico {
      if (!-f $request_filename) {
        rewrite ^.*$ /favicon.ico.default;
      }
    }
    location = /favicon.ico.default {
      internal;
      alias html/favicon.ico;
      break;
    }

But I think it´s to ugly and far from DRY.
Is there any way to short this code?

On Mon, 2008-06-30 at 17:16 -0300, Marcos N. wrote:

      break;
    }

But I think it´s to ugly and far from DRY.
Is there any way to short this code?

Not sure how “DRY” applies in this context…

What you probably want is something like this:

location = /favicon.ico {
error_page 404 html/favicon.ico;
}

Cliff

your solution is DRY, but I´m not sure if it´s right to send a 404 code,
is it?

Hello!

On Tue, Jul 01, 2008 at 08:03:23AM -0300, Marcos N. wrote:

your solution is DRY, but I?m not sure if it?s right to send a 404 code, is it?

Feel free to use

error_page 404 = html/favicon.ico;

instead (note the ‘=’).

See http://wiki.codemongers.com/NginxHttpCoreModule#error_page for
details.

Maxim D.

My current solution works:

location = /favicon.ico {
if (!-f $request_filename) {
rewrite ^.*$ /favicon.ico.default;
}
}
location = /favicon.ico.default {
internal;
alias html/favicon.ico;
break;
}

but would be nice if possible to do this:

location = /favicon.ico {
if (!-f $request_filename) {
alias html/favicon.ico;
break;
}

But alias can´t be used inside an if. Why that?

On Tue, Jul 01, 2008 at 05:09:33PM +0400, Maxim D. wrote:

instead (note the ‘=’).

See http://wiki.codemongers.com/NginxHttpCoreModule#error_page for
details.

No, html/favicon.ico will not work. It should be redirected to something
like
/html/favicon.ico, and then some location /html/… should be described.

Do this to return No Content…

location = /favicon.ico {
return 204;
}

Or this to do basically the same, which spits out an empty gif from
memory…

location = /favicon.ico {
empty_gif;
}

On Tue, Jul 1, 2008 at 8:27 AM, Marcos N. [email protected]
wrote:

    alias html/favicon.ico;
    }

your solution is DRY, but I?m not sure if it?s right to send a 404
code,

No, html/favicon.ico will not work. It should be redirected to something
like
/html/favicon.ico, and then some location /html/… should be described.

Maxim D.

On Mon, Jun 30, 2008 at 5:24 PM, Cliff W. [email protected]
wrote:

On Mon, 2008-06-30 at 17:16 -0300, Marcos N. wrote:

I?m doing this to show a default favicon.ico, when the site doesn?t
have

On Tue, Jul 01, 2008 at 10:27:45AM -0300, Marcos N. wrote:

     break;

But alias can?t be used inside an if. Why that?

This is internal problem. You may try

location = /favicon.ico {
if (!-f $request_filename) {
root html;
break;
}
}

but anyway, “if (!-f $request_filename)” is ugly contruction.

On Tue, 2008-07-01 at 11:08 -0300, Marcos N. wrote:

The point is that I want to return a default icon, but only if the
site doesn´t have one.
That´s why the ugly if (!-f $request_filename) { construction.
empty_gif; wouldn´t solve the problem ether.
I´m using the same for show a default error page if the domain doesn´t
have it´s own.

Do you think that touch disk with !-f $request_filename can be a
problem for performance?

What’s wrong with using the error_page directive? If you are returning
the same gif for all domains that don’t have a favicon it should work
fine. As Maxim pointed out, you can override the 404 return code.

Cliff

I realize that error_page will never work, cause I need to send a file
that is not reachable by uri,
those files are inside nginx html folder.
What now?

The point is that I want to return a default icon, but only if the
site doesn´t have one.
That´s why the ugly if (!-f $request_filename) { construction.
empty_gif; wouldn´t solve the problem ether.
I´m using the same for show a default error page if the domain doesn´t
have it´s own.

Do you think that touch disk with !-f $request_filename can be a
problem for performance?

Hello!

On Wed, Jul 02, 2008 at 07:04:22AM -0300, Marcos N. wrote:

I realize that error_page will never work, cause I need to send a file
that is not reachable by uri,
those files are inside nginx html folder.
What now?

location /my_files_not_reachable_by_uri/ {
internal;
root …;
}

or even

location @my_files_not_reachable_by_uri {
root …;
}

See http://wiki.codemongers.com/NginxHttpCoreModule for more
information.

Maxim D.

On Die 01.07.2008 17:50, Igor S. wrote:

On Tue, Jul 01, 2008 at 10:27:45AM -0300, Marcos N. wrote:

My current solution works:

[snipp]

location = /favicon.ico {
if (!-f $request_filename) {
root html;
break;
}
}

but anyway, “if (!-f $request_filename)” is ugly contruction.

Why do you mean this, is there a better solution?

BR

Aleks