在nginx配置(配置文件)之间切换?

时间:2016-12-12 19:40:13

标签: nginx

鉴于以下nginx配置......

upstream myupstream {
  server localhost:9000;
}

location ~ ^/.* {
  root /path/to/var ;
  try_files $uri $uri/ /index.html =404;

  # proxy_pass http://myupstream;
  # proxy_redirect     off;
  ...
}

我希望在代理(使用端口9000处的节点服务器)和服务(在/ path / to / var)之间轻松切换,而无需注释掉每个块。是否有任何工具或本机解决方案可以在不同的nginx配置之间切换? (解决方案需要使用nginx -s reload

1 个答案:

答案 0 :(得分:0)

好的,试试吧。请求/anyurl/?test=1切换到代理,/anyurl/?test=0切换回本地磁盘。

root /path/to/var;
error_page 464 = @proxy;

location / {
    # check if test argument found in request
    if ($arg_test ~ ^\d+$) {
        # set cookie and redirect to same location
        add_header Set-Cookie "test=$arg_test";
        return 302 $uri;
    }
    if ($cookie_test = "1") {
        # cookie "test" found, redirect to named location via custom error code
        # it can be any unused http code same as in error_page directive
        return 464;
    }
    try_files $uri $uri/ /index.html =404;
}

location @proxy {
    proxy_pass http://myupstream;
}