V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
NGINX
NGINX Trac
3rd Party Modules
Security Advisories
CHANGES
OpenResty
ngx_lua
Tengine
在线学习资源
NGINX 开发从入门到精通
NGINX Modules
ngx_echo
yasi
V2EX  ›  NGINX

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

  •  
  •   yasi · 2022-07-19 09:38:37 +08:00 · 1369 次点击
    这是一个创建于 644 天前的主题,其中的信息可能已经有所发展或是发生改变。

    根 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/;
       }
    
    2 条回复    2022-07-19 22:44:40 +08:00
    poembre
        1
    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
        2
    humbass  
       2022-07-19 22:44:40 +08:00
    用 Stream 模块实现,判断 UNI 即可。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1139 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 22:49 · PVG 06:49 · LAX 15:49 · JFK 18:49
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.