【问题标题】:dynamically change nginx proxy pass动态更改 nginx 代理通行证
【发布时间】:2022-01-25 17:50:50
【问题描述】:

您好,我想在 nginx 中设置动态代理通道。 $myvar 将采用 ops/dev/qa 等值。但是当我使用 nginx -t 验证上述 nginx 配置文件时,我得到“proxy_set_header”指令在此处是不允许的。希望您能帮我找出这段代码的问题。

location ~^/reporting-(?<myvar>[a-zA-Z]+) {

    if ($myvar = "ops") {


            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_pass          https://xx.xx.xx.xx:8080/reporting;
    }

            if ($myvar = "dev") {


            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_pass          https://xx.xx.xx.xx:8083/reporting;
    }



 }

嗨,谢谢蒂莫。我确信这个解决方案基本上是正确的。但我仍在为 url 重写而苦苦挣扎。为了测试这一点,我应用了下面的代码。

location /reporting-ops {

        proxy_set_header    Host $host;

        rewrite ^/reporting-ops/(.*) /reporting/$1 break;

        proxy_pass http://192.168.1.25:8931/reporting/$1;  #option 1

     #  proxy_pass http://192.168.1.25:8931/$1; # option 2

     }

但是当我这样做时,我的浏览器 URL 会更改为 /reporting,如下所示。我认为浏览器 url 不应该改变 & 重写应该在请求到达 nginx 时发生。

【问题讨论】:

    标签: nginx dynamic nginx-reverse-proxy nginx-config nginx-location


    【解决方案1】:

    在这种用例中,您应该尽量避免使用 if。 https://www.nginx.com/resources/wiki/start/topics/depth/ifisevil/

    此外,proxy_set_header 指令不允许在 if 的上下文中使用。 https://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_set_header

    我会使用map 来解决这个问题

    
    map $uri $upstream {
      ~^/reporting-dev/ 1.2.3.4:8080;
      ~^/reporting-qa/ 1.2.3.4:8081;
      default 1.2.3.4:8082;
    }
    
    server {
    
      location ~^/reporting-([a-zA-Z]+)/ {
         proxy_set_header Host $host;
         proxy_redirect off;
         proxy_pass http://$upstream/reporting;
      }
    }
    
    

    如果您需要重写 $uri 以将“后面”的任何内容传递给上游,请执行此操作。

    
    map $ruri $upstream {
      ~^/reporting-dev/ 127.0.0.1:8080;
      ~^/reporting-qa/  127.0.0.1:8081;
    }
    
    server {
      #Keep the requested URI before! we rewrite it for the upstream
      set $ruri $uri;
      location / {
        return 200 "No match\n";
      }
    
    
      location ~ ^/reporting-([a-zA-Z]+)/ {
         proxy_set_header Host $host;
         #rewrite the URI to get anything but reporting-qa|dev
         rewrite /reporting-([a-z]+)/(.*) /reporting/$2 break;
         proxy_pass http://$upstream;
      }
    }
    
    
    server {listen 8080; return 200 "OK DEV = $host$request_uri\n";}
    server {listen 8081; return 200 "OK QA = $host$request_uri\n";}
    
    

    【讨论】:

    • 感谢您的支持。当我在 location 块中使用正则表达式和内部代理传递时,我收到一条消息“proxy_pass”不能在正则表达式给出的位置中包含 URI 部分 ....
    • 已更新我的答案。您是否需要 request_uri 而不需要 reporting-qa 作为对上游请求的一部分。喜欢 /reporting-qa/some/ressource -> /reporting/some/resource?
    • 再次感谢您。是的,我需要将/reporting-qa/some/resource 重定向到reporting/some/resource。
    • 那么第二个示例(带有重写)将为您工作。刚刚在我的服务器上测试过。
    • @TimoStark 我也遇到了 @Prageeth 提到的同样问题。当我在浏览器中尝试x.x.x.x/reporting-ops 时,我被重定向到x.x.x.x/reporting/login.html:jsessionid=D1.... 如何在不更改浏览器URL 的情况下透明地将请求发送到x.x.x.x/reporting
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-12-04
    • 2016-05-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-05
    • 1970-01-01
    相关资源
    最近更新 更多