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

2018-01-16 00:51:49 +08:00
 axisray

比如我访问 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;
10540 次点击
所在节点    NGINX
27 条回复
privil
2018-01-16 01:03:55 +08:00
写个模版生成呗
h4lbhg1G
2018-01-16 01:30:12 +08:00
正则表达式先试试,不行就上 lua 脚本喽
gleymonkey
2018-01-16 01:35:01 +08:00
rewrite /wechat_(.*)$ http://wechat_$1 break;
axisray
2018-01-16 01:55:56 +08:00
@gleymonkey rewrite 会返回 302 吧,wechat_XX 是内部域名,客户端访问不到的,一定需要 nginx 反代
msg7086
2018-01-16 04:42:48 +08:00
location ~ \/(wechat_.*)\/ { proxy_pass http://$1/; } 这样?
cxbig
2018-01-16 05:23:59 +08:00
有定数可以用 map 或 regexp
无定数可以用 regexp
paranoiagu
2018-01-16 08:05:08 +08:00
@cxbig 我也有这个疑问,不定数的,怎么写 regexp ?
azh7138m
2018-01-16 08:17:18 +08:00
@axisray rewrite 返回 302 是什么鬼啊
axisray
2018-01-16 08:34:45 +08:00
@azh7138m rewrite 是重定向吧,这个场景需要的是路由
azh7138m
2018-01-16 08:36:00 +08:00
@axisray rewrite 不是重定向,建议自己试一下
axisray
2018-01-16 09:18:13 +08:00
@azh7138m 我试了,会重定向
rewrite /wechat_(.*)$ http://wechat_$1 break;
浏览器会直接跳转到 http://wechat_xxx,然后就失败了
lcdtyph
2018-01-16 09:18:48 +08:00
@azh7138m rewrite 到不同域名会返回 302
v2orz
2018-01-16 09:28:14 +08:00
关注一下
azh7138m
2018-01-16 09:39:16 +08:00
@lcdtyph 那我是怎么用 Nginx 反代 Google 的?


@axisray subs_filter,请
axisray
2018-01-16 09:40:25 +08:00
@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
2018-01-16 09:41:13 +08:00
卧槽,码没打全……
cxbig
2018-01-16 16:43:25 +08:00
@paranoiagu
参看这个: https://stackoverflow.com/questions/13706658/variable-capture-in-nginx-location-matching
只要 regex 规则写对了就行,如果有例外,写在这个 location 之前。
paranoiagu
2018-01-16 19:53:10 +08:00
@cxbig
试了一下,出现了 15 楼一样的错误信息。

@axisray
我也出现了你 15 楼的错误。你搞定了吗?
cxbig
2018-01-17 04:17:10 +08:00
@paranoiagu
一般用 proxy_pass 要同时定义一个 resolver,如:
resolver 8.8.8.8;
国内酌情考虑境内 DNS 的 IP。
paranoiagu
2018-01-17 08:19:42 +08:00
@cxbig 我也想加 resolver,但是我是 docker,其实后端是不同的 docker,所以其实是机器名。而且没有 dns 服务器。我试了用 resolver 127.0.0.1 会提示无法连接 127.0.0.1:53。

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

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

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

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

© 2021 V2EX