Serving an alternate robots.txt for SSL requests.

Igor Sysoev is at rambler-co.ru
Wed Jan 14 09:57:51 MSK 2009


On Tue, Jan 13, 2009 at 09:15:07PM -0200, Juan Fco. Giordana wrote:

> Thank you Nick for your help,
> 
> I've followed your suggestions and it worked as expected.
> 
> I've changed the rewrite rule since I don't need to capture anything there.
> 
> server {
>     listen 443;
>     [...]
>     location = /robots.txt {
>         rewrite ^    /robots_ssl.txt last;
>     }
> }
> 
> Does anybody know if this is possible to do within a single server 
> context that handle both protocols in version 0.7.*?

If your servers are different only in this part, then in 0.7 you can

     server {
         listen 80;
         listen 443 default ssl;

         location = /robots.txt {
             if ($server_port = 443) {                # or ($scheme = https)
                  rewrite ^   /robots_ssl.txt  last;  # or "break;"
             }
         }

         ...

If the servers have many differences, then it's better to use
separate servers. In this case you do not need rewrite, use just alias:

     server {
         listen 443;

         location = /robots.txt {
             alias  /path/to/robots_ssl.txt;
         }

Yet another way (the better than with if/rewrite):

     map  $scheme  $robots {
          default  robots.txt;
          https    robots_ssl.txt;
     }

     server {
         listen 80;
         listen 443 default ssl;

         location = /robots.txt {
             alias  /path/to/$robots;
         }

         ...


> Thanks.
> 
> On 2009-01-07 15:23:26 Nick Pearson wrote:
> >Hi Juan,
> >
> >Try using two server directives -- one for http and one for https.  The
> >server directive chosen depends on the port that is requested.  Something
> >like this:
> >
> >server {
> >    listen  80;  # http
> >    server_name  www.yoursite.com;
> >    [...]
> >    location /robots.txt {
> >        break;
> >    }
> >}
> >server {
> >    listen  443;  # https
> >    server_name  www.yoursite.com;
> >    [...]
> >    location /robots.txt {
> >        rewrite (.*)  /robots_ssl.txt;
> >    }
> >}
> 

-- 
Igor Sysoev
http://sysoev.ru/en/





More information about the nginx mailing list