bernhardrusch
bernhardrusch

Reputation: 11890

nginx: serve site for some IP Adresses, proxy_pass for others

We are developing a new site. We would like to update the domain pointing to our server, so we can release the site quickly, when we get the okay from the customer.

In the meantime all IP addresses should be proxy_pass'd to the old site (IP address) and we (and the customer) should be redirected to the new site.

Right now it is working, but it is an ugly hack with some if statements. I've read that this is not recommended.

Right now the configuration is something like this:

server {
    listen x.x.x.x:80;
    server_name yyyyy;
    root /etc/www;
    index index.html index.htm;
    set $needRewrite Y;
    if ($remote_addr ~* w.x.y.z) { set $needRewrite N;}
    location / {
      if ($needRewrite = Y) {
         proxy_pass http://old_address;
         break;
      }
      try_files $uri $uri/ /index.php?$args;
    }
    .....

What is the correct way to do something like this ?

Upvotes: 0

Views: 89

Answers (1)

Cole Tierney
Cole Tierney

Reputation: 10314

You could use the geo ip module to have a variable root based on the client's ip address:

geo $www_root {
    default /etc/www;
    w.x.y.z/32 /etc/client/www;
}

server {
    listen x.x.x.x:80;
    server_name yyyyy;
    root $www_root;
    index index.html index.htm;
    try_files $uri $uri/ /index.php?$args;
}

Note: The above config will only work if both sites use the same ip address. If your servers are on different ip addresses, you could just redirect to the client's preview site:

geo $client {
    default 0;
    w.x.y.z/32 1;
}

server {
    listen x.x.x.x:80;
    server_name yyyyy;
    root /etc/www;
    index index.html index.htm;
    try_files $uri $uri/ /index.php?$args;

    location / {
        if ($client) {
            return 302 http://client-preview-site;
        }
    }
}

Note: If is not always evil if it's your only option. ;)

Upvotes: 1

Related Questions