【问题标题】:Divert request to different server as per request-url in NGINX根据 NGINX 中的请求 URL 将请求转移到不同的服务器
【发布时间】:2021-12-24 08:30:21
【问题描述】:

我想实现如下所示的目标,但找不到可能性:

server {
    listen 80;
    server_name localhost;

    location /get-product {
        if(request=GET){  
            proxy_pass http://get-app-server:8080/products;
            proxy_set_header Host "get-app-server";
        }
        if(request=POST){  
            proxy_pass http://post-app-server:8080/products;
            proxy_set_header Host "post-app-server";
        }
    }
}

我该如何执行这样的决定?

【问题讨论】:

    标签: python nginx flask


    【解决方案1】:

    有许多可能的解决方案。例如,使用伪造的内部 URI (/post/get-product) 来处理以下两种情况之一:

    location /get-product {
        if ($request_method = POST) {
            rewrite ^ /post$uri last;
        }
        proxy_pass http://get-app-server:8080/products;
        proxy_set_header Host "get-app-server";
    }
    location /post/get-product {
        internal;
        proxy_pass http://post-app-server:8080/products;
        proxy_set_header Host "post-app-server";
    }
    

    注意if 语句的正确语法和变量名。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-06-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-06
      • 2014-12-28
      相关资源
      最近更新 更多