nginx 一个 location 中如何设置多个 proxy_set_header?

2022-07-19 09:38:37 +08:00
 yasi

根 location 中要判断客户端 UA ,根据不同 UA 转发到不同服务(如:aaa.com/bbb.com ),配置时写两个 proxy_set_header 会报语法错误。应该怎么实现呢,请大神赐教。

location  / {
       set $mobile_rewrite do_not_perform;

       if ($http_user_agent ~* '(windows|Windows|Intel Mac OS X)') {
               set $mobile_rewrite perform;
       }
       if ($args ~ h5=1){
               set $mobile_rewrite do_not_perform;
       }
       if ($mobile_rewrite = perform) {
               #proxy_pass http://10.20.6.164:8080;
               proxy_pass  http://aaa.com;
       }
     

       proxy_set_header Host "bbb.com";
       proxy_next_upstream http_502 http_504 error timeout invalid_header;
       proxy_pass  http://10.20.127.34/;
   }
1582 次点击
所在节点    NGINX
2 条回复
poembre
2022-07-19 13:51:15 +08:00
- 如果你用 openresty 的话 , 可以这样,更加灵活
```
location / {

set $upstream_url 'http://10.20.127.34';

access_by_lua_block {
if string.find(xxx,xxx) then
ngx.var.upstream_url = "http://www.baidu.com"
end
}

proxy_pass $upstream_url;
}

```
humbass
2022-07-19 22:44:40 +08:00
用 Stream 模块实现,判断 UNI 即可。

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/867174

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX