nginx:为一些IP地址提供站点服务,为其他IP地址提供proxy_pass



我们正在开发一个新网站。我们想更新指向我们服务器的域,这样当我们得到客户的同意时,我们可以快速发布网站。

同时,所有IP地址都应该被代理传递到旧站点(IP地址),我们(和客户)应该被重定向到新站点。

现在它正在工作,但它是一个丑陋的黑客与一些if语句。我读到这是不推荐的。

现在的配置是这样的:

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;
    }
    .....

做这样的事情的正确方法是什么?

您可以使用geo-ip模块,根据客户端的ip地址创建一个变量根:

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;
}

注意:只有当两个站点使用相同的ip地址时,上述配置才会起作用。如果你的服务器位于不同的ip地址,你可以重定向到客户端的预览网站:

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;
        }
    }
}

注意:如果不是总是邪恶的if,这是你唯一的选择。)

最新更新