Correct way to do redirect

Igor Sysoev is at rambler-co.ru
Sat Aug 30 19:33:55 MSD 2008


On Sat, Aug 30, 2008 at 05:28:25AM +0700, Chris Savery wrote:

> I'd like to do a geo based redirect. Users who initially hit my site 
> would be given an IP round robin from the  DNS servers. Then each node 
> would test the ip and decide if it would be better to redirect to the 
> closer node. Only doing this for index.php, eg.
> 
> http {
> ...
>    geo $region {
>        default NA;
>        include georegions.conf;
>        }
> ...
> 
> server {
> ...
>           location ~ ^/index.php {
>               if ($region = NA)
> --> what to write here to redirect to other IP?
>          }
>        location ~ \.php$ { fastcgi_pass 127.0.0.1:9000; include 
> fastcgi_params; }
>    }
> ... other servers...

First it's better to use more effective location:

-           location ~ ^/index.php {
+           location = /index.php {


Or you may do it for "/" only:

            location = / {

As to redirect, I think it's better to use something like this:

    geo $region  {
        default  "";
        include  georegions.conf;
    }

    server {
         location = /index.php {

             if ($region) {
                 rewrite  ^   http://$region/index.php;
             }

             fastcgi_pass 127.0.0.1:9000;
             ...
         }

The georegions.conf is site specific, i.e, for NA site:

     x.x.x.x/x   "";                # NA net
     x.x.x.x/x   eu.domain.com;     # EU net
     x.x.x.x/x   as.domain.com;     # AS net

for EU site:

     x.x.x.x/x   na.domain.com;     # NA net
     x.x.x.x/x   "";                # EU net
     x.x.x.x/x   as.domain.com;     # AS net


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





More information about the nginx mailing list