Nginx rewrite rule for favicon.ico files

Nginx rewrite rules for favicon.ico for multiple subdomains.

In nginx /etc/sites-avaliable/vhost_config_file, I have the following
rewrite rules for serving multiple subdomains with their favicon.ico
files:

if ($host = example1.example.com){
   rewrite ^/favicon.ico$ /favicon_example.ico break;
 }

if ($host = example2.example.com){
   rewrite ^/favicon.ico$ /favicon_example.ico break;
 }

if ($host = example3.example.com){
   rewrite ^/favicon.ico$ /favicon_example.ico break;
 }

Please, how can I make this simple operation by adding ONE GENERAL
REWRITE
RULE
:

That rule must do:

1.For subdomains example1,example2… to check in location
/images/$host.ico
is there a favicon_example.ico file and serve it.

2.If there is no such file, than to serve just a plain favicon.ico file.

Thank’s in advance…

Posted at Nginx Forum:

On Aug 31, 2012, at 14:28 , nexon wrote:

RULE**:

That rule must do:

1.For subdomains example1,example2… to check in location /images/$host.ico
is there a favicon_example.ico file and serve it.

2.If there is no such file, than to serve just a plain favicon.ico file.

Thank’s in advance…

Why not to create a separate server block for each site instead of this
if/rewrite mess ?


Igor S.

That rule must do:
1.For subdomains example1,example2… to check in location
/images/$host.ico is there a favicon_example.ico file and serve it.
2.If there is no such file, than to serve just a plain favicon.ico file.

From you description I don’t fully understand what exactly is your
current
file structure but you can use try_files (
http://wiki.nginx.org/HttpCoreModule#try_files ) and add the $host
variable
in whatever place is needed.

For example:

location /favicon.ico {
try_files /images/$host/favicon_example.ico /favicon.ico;
}

rr

Igor S. Wrote:

[email protected]
nginx Info Page

Can’t do that, because I have to serve different favicon.ico to
different
subdomain.
In example, there is several domains in server_name and some of them
need
to have their own favicon.ico,
if there is no favicon.ico for some of them, than they need to be served
with the main favicon.ico file.

Posted at Nginx Forum:

On Aug 31, 2012, at 15:27 , nexon wrote:


nginx mailing list
[email protected]
nginx Info Page

Can’t do that, because I have to serve different favicon.ico to different
subdomain.
In example, there is several domains in server_name and some of them need
to have their own favicon.ico,
if there is no favicon.ico for some of them, than they need to be served
with the main favicon.ico file.

Anything except favicons in this servers is the same ?


Igor S.

Anything except favicons in this servers is the same ?


Igor S.


nginx mailing list
[email protected]
nginx Info Page

Please explain your question.
If I understand you correctly, you are asking is every subdomain have
their
own page, than yes.
The trick is that if subdomain have it’s own favicon than it needs to be
served with it, if not, than to be served with main favicon.ico…

Posted at Nginx Forum:

On Aug 31, 2012, at 15:44 , nexon wrote:

Anything except favicons in this servers is the same ?

Please explain your question.
If I understand you correctly, you are asking is every subdomain have their
own page, than yes.
The trick is that if subdomain have it’s own favicon than it needs to be
served with it, if not, than to be served with main favicon.ico…

The question is why do you try to push everything inside one server
instead of separating processing in several servers:

server {
server_name sub1.domain.com;

}

server {
server_name sub2.domain.com;

}

server {
server_name sub3.domain.com;

}


Igor S.

try_files /images/$host/favicon_example.ico /favicon.ico;

}

rr


nginx mailing list
[email protected]
nginx Info Page

Could you please be more specific in sintax?

Thank’s

Posted at Nginx Forum:

On Friday 31 August 2012 17:13:53 nexon wrote:
[…]

Could you please be more specific in sintax?

http://nginx.org/r/try_files

wbr, Valentin V. Bartenev

On Fri, 31 Aug 2012 06:28:39 -0400 (EDT), “nexon”
[email protected] wrote:

 }

Please, how can I make this simple operation by adding ONE GENERAL
REWRITE RULE
:

That rule must do:

1.For subdomains example1,example2… to check in
location /images/$host.ico is there a favicon_example.ico file and
serve it.

2.If there is no such file, than to serve just a plain favicon.ico
file.

Would this help? [note: from memory, not tested]

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

M.

Igor S. Wrote:

served with it, if not, than to be served with main favicon.ico…
server_name sub2.domain.com;
Igor S.


nginx mailing list
[email protected]
nginx Info Page

I am using several vhosts and this is part of the setup file for one of
them:

server_name sub1.example.com *.domain.com domain.com sub2.example.com;

if ($host = something1.domain.com){
rewrite ^/favicon.ico$ /favicon_domain.com.ico break;
}

if ($host = something2.domain.com){
rewrite ^/favicon.ico$ /favicon_domain.com.ico break;
}

etc…

I plan to put all *.ico files in location /images/

something1.domain.com.ico
something2.domain.com.ico
something3.domain.com.ico

etc…

So what I need is:
when request is for something1.domain.com.ico happens
than to check in location /images/ is there a file
something1.domain.com.ico
and serve it if it is there,
but if there is no such a file, than to serve clasic favicon.ico file.

Please help…

Posted at Nginx Forum:

On Fri, Aug 31, 2012 at 09:52:48AM -0400, nexon wrote:

The trick is that if subdomain have it’s own favicon than it needs

I am using several vhosts and this is part of the setup file for one of
}

So what I need is:
when request is for something1.domain.com.ico happens
than to check in location /images/ is there a file something1.domain.com.ico
and serve it if it is there,
but if there is no such a file, than to serve clasic favicon.ico file.

server {
server_name something1.domain.com;

location = /favicon.ico {
    alias  /image/something1.domain.com.ico;
}

...

}

server {
server_name something2.domain.com;

location = /favicon.ico {
    alias  /image/something2.domain.com.ico;
}

...

}

server {
server_name something3.domain.com;

location = /favicon.ico {
    alias  /image/something3.domain.com.ico;
}

...

}


Igor S.

Yes, this would be much beter, but I am stuck with my conf as it is…

Thank’s

Posted at Nginx Forum:

[email protected]
nginx Info Page

I have several vhosts, this is part of the config file for vhost in
question:

server_name sub1.domain.com *.mydomain.com mydomain.com
sub2.domain.com;

if ($host = www.mydomain.com){
rewrite ^/favicon.ico$ /favicon_mydomain.ico break;
}

if ($host = boat.mydomain.com){
rewrite ^/favicon.ico$ /favicon_boat.ico break;
}

if ($host = car.mydomain.com){
rewrite ^/favicon.ico$ /favicon_car.ico break;
}

etc…

I plan to put all .ico files in /images/ dir.
favicon_mydomain.ico
favicon_boat.ico
favicon_car.ico

and many more…

So what I need is next:
when request is happens for car.mydomain.com,
nginx searches for /images/favicon_car.ico and it serves it if exists,
if not than serves regular favicon.ico.

Please help…

Posted at Nginx Forum:

Thank you Valentin, this solved my problem…

Posted at Nginx Forum:

Thank’s Mark, problem solved…

Posted at Nginx Forum:

On Aug 31, 2012, at 18:50 , nexon wrote:

Yes, this would be much beter, but I am stuck with my conf as it is…

The more complex configuration I will eventually create the more often
you will stuck with it. It’s better to create a scaleable configuration
from the very start or at least as early as possible.


Igor S.