› NGINX
› NGINX Trac
› 3rd Party Modules
› Security Advisories
› CHANGES
› OpenResty
› ngx_lua
› Tengine
在线学习资源
› NGINX 开发从入门到精通
NGINX Modules
› ngx_echo
axisray
V2EX  ›  NGINX

Nginx 配置写法请教,根据路径转发到不同的服务器

  •  
  •   axisray · Jan 16, 2018 · 11998 views
    This topic created in 3222 days ago, the information mentioned may be changed or developed.

    比如我访问 www.abc.com/aaa/,则转发到 http://aaa/

    比如我访问 www.abc.com/bbb/,则转发到 http://bbb/

    ……

    除了像这样一条一条写,还有啥别的办法吗?

            location /wechat_qyw/ {
                proxy_pass http://wechat_qyw/;
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    
            }
    
            location /wechat_qyw/ {
                proxy_pass http://wechat_qyw/;
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    
            }
    
            location /wechat_sd/ {
                proxy_pass http://wechat_sd/;
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    
    Supplement 1  ·  Jan 16, 2018

    @msg7086 灰常感谢 按自己需求稍改了下:

    location ~ ^\/(wechat_.*)\/ { 
    proxy_pass http://$1/; 
    } 
    

    测试结果:

    2018/01/16 09:37:39 [error] 7637#0: *15 no resolver defined to resolve wechat_fafa, client: 121.35.***.***, server: ******, request: "GET /wechat_fafa/awefaews HTTP/1.1", host: "*******" 
    2018/01/16 09:38:01 [error] 7637#0: *15 no resolver defined to resolve wechat_fafa, client: 121.35.***.***, server: ******, request: "GET /wechat_fafa/awefaews HTTP/1.1", host: "*******"
    
    Supplement 2  ·  Jan 20, 2018

    上生产发现不行,怀疑是版本问题,改成下面这样

            location ~ ^\/(?<domain>wechat_.*)\/ { 
                proxy_pass http://$domain/; 
            } 
    
    

    访问 www.abc.com/bbb/,则转发到 http://bbb/,这种没有问题
    但是访问 www.abc.com/bbb/xxx 的时候有问题,报404错误

    Supplement 3  ·  Jan 21, 2018

    结帖,问题解决,感谢各位

            location ~ ^\/(?<domain>wechat_.*) {
                proxy_pass http://$domain;
            }
    
    

    测试结果

    nginx_1         | 192.168.19.1 - - [21/Jan/2018:13:47:50 +0800] "GET /wechat_hubei/wxweb/signview HTTP/1.1" 200 56 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36" "-"
    wechat_hubei_1  | [Wx][21/01/18 13:48:00.838][INFO][SZWX.Controllers.WxWebController][SignView],Request URL:http://wechat_hubei/wxweb/signview
    
    Supplement 4  ·  Jan 21, 2018

    UPDATE

            location ~ ^\/(?<domain>wechat_.*)\/$ {
                resolver 127.0.0.11;
                proxy_pass http://$domain;
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            }
    
    27 replies  •  2018-01-22 08:24:11 +08:00
    privil
    privil
    Jan 16, 2018 via iPhone
    1
    写个模版生成呗
    h4lbhg1G
    h4lbhg1G
    Jan 16, 2018
    2
    正则表达式先试试,不行就上 lua 脚本喽
    gleymonkey
    gleymonkey
    Jan 16, 2018
    3
    rewrite /wechat_(.*)$ http://wechat_$1 break;
    axisray
    axisray
    OP
    Jan 16, 2018
    4
    @gleymonkey rewrite 会返回 302 吧,wechat_XX 是内部域名,客户端访问不到的,一定需要 nginx 反代
    msg7086
    msg7086
    Jan 16, 2018❤️1
    5
    location ~ \/(wechat_.*)\/ { proxy_pass http://$1/; } 这样?
    cxbig
    cxbig
    Jan 16, 2018
    6
    有定数可以用 map 或 regexp
    无定数可以用 regexp
    paranoiagu
    paranoiagu
    Jan 16, 2018 via Android
    7
    @cxbig 我也有这个疑问,不定数的,怎么写 regexp ?
    azh7138m
    azh7138m
    Jan 16, 2018 via Android
    8
    @axisray rewrite 返回 302 是什么鬼啊
    axisray
    axisray
    OP
    Jan 16, 2018
    9
    @azh7138m rewrite 是重定向吧,这个场景需要的是路由
    azh7138m
    azh7138m
    Jan 16, 2018 via Android
    10
    @axisray rewrite 不是重定向,建议自己试一下
    axisray
    axisray
    OP
    Jan 16, 2018
    11
    @azh7138m 我试了,会重定向
    rewrite /wechat_(.*)$ http://wechat_$1 break;
    浏览器会直接跳转到 http://wechat_xxx,然后就失败了
    lcdtyph
    lcdtyph
    Jan 16, 2018 via iPhone
    12
    @azh7138m rewrite 到不同域名会返回 302
    v2orz
    v2orz
    Jan 16, 2018
    13
    关注一下
    azh7138m
    azh7138m
    Jan 16, 2018
    14
    @lcdtyph 那我是怎么用 Nginx 反代 Google 的?


    @axisray subs_filter,请
    axisray
    axisray
    OP
    Jan 16, 2018
    15
    @msg7086 灰常感谢
    按自己需求稍改了下:
    location ~ ^\/(wechat_.*)\/ {
    proxy_pass http://$1/;
    }
    测试结果:
    2018/01/16 09:37:39 [error] 7637#0: *15 no resolver defined to resolve wechat_fafa, client: 121.35.***.***, server: sz.sangfor.site, request: "GET /wechat_fafa/awefaews HTTP/1.1", host: "*******"
    2018/01/16 09:38:01 [error] 7637#0: *15 no resolver defined to resolve wechat_fafa, client: 121.35.***.***, server: sz.sangfor.site, request: "GET /wechat_fafa/awefaews HTTP/1.1", host: "*******"
    axisray
    axisray
    OP
    Jan 16, 2018
    16
    卧槽,码没打全……
    cxbig
    cxbig
    Jan 16, 2018❤️1
    17
    @paranoiagu
    参看这个: https://stackoverflow.com/questions/13706658/variable-capture-in-nginx-location-matching
    只要 regex 规则写对了就行,如果有例外,写在这个 location 之前。
    paranoiagu
    paranoiagu
    Jan 16, 2018
    18
    @cxbig
    试了一下,出现了 15 楼一样的错误信息。

    @axisray
    我也出现了你 15 楼的错误。你搞定了吗?
    cxbig
    cxbig
    Jan 17, 2018
    19
    @paranoiagu
    一般用 proxy_pass 要同时定义一个 resolver,如:
    resolver 8.8.8.8;
    国内酌情考虑境内 DNS 的 IP。
    paranoiagu
    paranoiagu
    Jan 17, 2018 via Android
    20
    @cxbig 我也想加 resolver,但是我是 docker,其实后端是不同的 docker,所以其实是机器名。而且没有 dns 服务器。我试了用 resolver 127.0.0.1 会提示无法连接 127.0.0.1:53。
    cxbig
    cxbig
    Jan 18, 2018
    21
    @paranoiagu
    你的意思是这个 proxy_pass 都是不同的内部 docker container ?
    docker compose 有相关的内部网络配置的
    paranoiagu
    paranoiagu
    Jan 18, 2018 via Android
    22
    @cxbig 对,我一个个写死没问题的。改正则就不行了。
    axisray
    axisray
    OP
    Jan 20, 2018
    23
    @paranoiagu 嗯,我也是 docker 环境,测试没问题,放到生产就炸了
    axisray
    axisray
    OP
    Jan 20, 2018
    24
    @paranoiagu 15 楼那个错误对我来说是正常的,因为我环境里没那几个域名,瞎写的,解析不出来就对了
    axisray
    axisray
    OP
    Jan 21, 2018
    26
    @paranoiagu
    resolver 127.0.0.11;
    看下面的回答,是 11 而不是 1
    写死的话能解决,但是可能会有问题

    建议还是写个脚本去解析 /etc/resolv.conf
    https://stackoverflow.com/questions/35744650/docker-network-nginx-resolver#comment68472082_37656784
    paranoiagu
    paranoiagu
    Jan 22, 2018 via Android
    27
    @axisray 多谢,今天再试试看。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   924 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 42ms · UTC 20:13 · PVG 04:13 · LAX 12:13 · JFK 15:13
    ♥ Do have faith in what you're doing.