Nginx rewrite rule for favicon.ico files

Igor Sysoev igor at sysoev.ru
Fri Aug 31 14:01:25 UTC 2012


On Fri, Aug 31, 2012 at 09:52:48AM -0400, nexon wrote:
> Igor Sysoev Wrote:
> -------------------------------------------------------
> > 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;
> >     ...
> > }
> 
> 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.

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 Sysoev



More information about the nginx mailing list