【问题标题】:Nginx change request header value conditionallyNginx 有条件地更改请求标头值
【发布时间】:2018-12-04 22:03:42
【问题描述】:

如何根据nginx反向代理中的另一个头值有条件地更改请求头值?

【问题讨论】:

    标签: if-statement nginx http-headers nginx-reverse-proxy


    【解决方案1】:

    您可以使用 nginx ngx_http_map_module。 一个很好的例子在这里 - Mapping Headers in Nginx

    基本上你需要将请求的标头(我在下面的配置中使用 from_header)映射到新的标头(示例中的 to_header),然后使用proxy_set_header

    map $http_from_header $to_header {
        default a;
        value_1 b;
        value_2 c;
    }
    
    server {
    ....
    
        location / {
            include proxy_params;
            proxy_set_header To_Header $to_header;
       }
    }
    

    Nginx 采用任何 HTTP 标头,将它们小写,并将破折号转换为下划线。它们可以作为以 $http_ 开头的变量访问。

    这样你应该得到你需要的东西。

    祝你好运

    【讨论】:

      猜你喜欢
      • 2018-06-27
      • 2022-01-21
      • 1970-01-01
      • 2019-02-12
      • 2021-09-15
      • 2015-07-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多