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

求教, nginx 怎么在 proxy_pass 的时候 携带 http 请求头部 Host 参数,并且值是 upstream 中配置的 server,不是客户端携带的 Host;

  •  
  •   ytlm · 2019-05-28 11:14:38 +08:00 · 4459 次点击
    这是一个创建于 1788 天前的主题,其中的信息可能已经有所发展或是发生改变。
    upstream test_backend {
        server test1.com;
        server test2.com;
    }
    
    location / {
        proxy_set_header Host $host;
        proxy_pass http://test_backend ;
    }
    
    这里的 $host 是请求客户端的 Host,代理过去也是客户端请求过来的 Host,不是我想要的;
    
    如果配置成 $proxy_host,我测试发现代理过去的 Host 设置成 test_backend,也不是我想要的;
    
    求教:
    我想要的代理过去的 Host  是 upstream 中配置的 test1.com 或者 test2.com ,根据最终访问哪个域名自动设置;
    
    
    第 1 条附言  ·  2019-05-29 09:15:24 +08:00

    结帖,

    没有找到好的方法,为了先让服务能用起来简单的配置了一下;

    
    upstream test_backend {
        server test1.com max_fails=3;
        server test2.com backup;
    }
    
    location / {
        proxy_set_header Host test1.com;
        proxy_pass http://test_backend ;
    }
    
    
    22 条回复    2019-07-11 09:26:42 +08:00
    phpfpm
        1
    phpfpm  
       2019-05-28 11:48:11 +08:00
    test1 自己都不知道自己是啥???
    mooncakejs
        2
    mooncakejs  
       2019-05-28 11:49:53 +08:00 via iPhone
    分开写,
    ytlm
        3
    ytlm  
    OP
       2019-05-28 12:13:56 +08:00
    @phpfpm #1 test1.comtest2.com 都是后端的服务,我不知道那边的处理逻辑,但是如果不携带正确的 Host 代理请求就会失败,
    ytlm
        4
    ytlm  
    OP
       2019-05-28 12:20:58 +08:00
    @mooncakejs #2 这个分开写是指什么呀?

    location 是一样的,upstream 分开写的话,感觉不好写
    KasuganoSoras
        5
    KasuganoSoras  
       2019-05-28 12:22:38 +08:00
    proxy_set_header Host $upstream_addr;
    Citrus
        6
    Citrus  
       2019-05-28 12:23:00 +08:00
    这个要求有点诡异啊,如果 test1.comtest2.com 是不同的服务,为啥要在同一个 location 里返回。。。这不是导致每次刷新都会看到不一样的页面么。。。
    原版 nginx 似乎做不到这个。
    另外不推荐这么些,nginx 会缓存 dns 解析的结果。如果域名指向的 IP 变了,nginx 会在 TTL 之后再更新。
    如果真的要这么玩,可以试下 openresty 的 balance_by_lua,用 lua 脚本选择后端 IP,然后修改请求头。
    Citrus
        7
    Citrus  
       2019-05-28 12:24:09 +08:00
    @KasuganoSoras upstream_addr 变量在这个要求里面不太试用吧,我记得这个变量会变成实际解析出来的 IP 端口,而不是域名哦。。。
    KasuganoSoras
        8
    KasuganoSoras  
       2019-05-28 12:31:56 +08:00
    可以试下
    if($upstream_addr = "test1.com 的 IP") {
    proxy_set_header Host test1.com;
    }
    if($upstream_addr = "test2.com 的 IP") {
    proxy_set_header Host test2.com;
    }
    ... 以此类推 ...
    KasuganoSoras
        9
    KasuganoSoras  
       2019-05-28 12:33:11 +08:00
    如果 upstream 的域名有多个 IP (例如 CNAME 负载均衡),可以通过修改 Hosts 将它们指向一个固定的 IP
    ytlm
        10
    ytlm  
    OP
       2019-05-28 13:54:29 +08:00
    @KasuganoSoras #5 这个变量得到的是一个 ip 地址,我想要的是域名;

    proxy_set_header 不能用到这个 if 块下面;
    ytlm
        11
    ytlm  
    OP
       2019-05-28 14:07:40 +08:00
    @Citrus #6 具体后端服务是什么情况不太清楚,有可能是用两个域名做主备,服务是两个,但是连的数据库是一个;

    两个域名对应的 ip 都是不一样的;

    url 都是一样的,所以用 location 区分不出来;
    est
        12
    est  
       2019-05-28 14:13:09 +08:00
    不加 proxy_set_header 试试呢?
    ytlm
        13
    ytlm  
    OP
       2019-05-28 14:15:32 +08:00
    @est #12 不加 proxy_set_header 的话,代理出去的请求头 Host 会被默认设置成 test_backend ; 这个 upstream 的名称
    werty
        14
    werty  
       2019-05-28 14:22:35 +08:00
    用 openresty 的话, 可以用 rewrite_by_lua 搞一段 lua 脚本, 可以 if else
    est
        15
    est  
       2019-05-28 14:26:56 +08:00
    @ytlm 实在不行,这个办法可以保底。在 nginx 里给 test1.com test2.com 分别建一个反代,proxy_set_header 设置好。

    然后再用你这个配置,指向 nginx 自己的反代。

    二层反代。
    ytlm
        16
    ytlm  
    OP
       2019-05-28 14:32:03 +08:00
    @werty #14 用 rewrite_by_lua 应该不太好做,用 balance_by_lua 应该是可以的;
    主要是不想写那么麻烦,想通过简单的配置解决;
    ytlm
        17
    ytlm  
    OP
       2019-05-28 14:33:45 +08:00
    @est #15 没有太明白这个呢,感觉不是好分开的,因为 url 是同一个;
    firebroo
        18
    firebroo  
       2019-05-28 17:08:02 +08:00
    @ytlm 他的意思是在 testx.com 和你现有的 nginx 之前再加个 nginx
    firebroo
        19
    firebroo  
       2019-05-28 17:08:21 +08:00
    @firebroo 之前=》之间
    mooncakejs
        20
    mooncakejs  
       2019-05-28 19:29:56 +08:00
    @ytlm 就是二层反代
    liwb2
        21
    liwb2  
       2019-07-10 21:02:02 +08:00
    可以尝试使用 http_map_module 中的 map 指令看看

    map http://nginx.org/en/docs/http/ngx_http_map_module.html

    $upstream_addr http://nginx.org/en/docs/http/ngx_http_upstream_module.html#variables

    map $upstream_addr $remote_server_name {
    default test1.com;
    'test1.com:port' test1.com;
    'test2.com:port' test2.com;
    }


    proxy_set_header Host $remote_server_name;

    注意 default 取值
    ytlm
        22
    ytlm  
    OP
       2019-07-11 09:26:42 +08:00
    @liwb2 #21 嗯嗯,感觉应该是可以的,没有实际尝试;但是可能还有一种情况是这个域名的 ip 不是固定的,或者中途换 ip 了,那这边也要相应的修改并重启 nginx ;
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   987 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 31ms · UTC 22:39 · PVG 06:39 · LAX 15:39 · JFK 18:39
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.