比如我访问 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;
@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: "*******"
上生产发现不行,怀疑是版本问题,改成下面这样
location ~ ^\/(?<domain>wechat_.*)\/ {
proxy_pass http://$domain/;
}
访问 www.abc.com/bbb/,则转发到 http://bbb/,这种没有问题
但是访问 www.abc.com/bbb/xxx 的时候有问题,报404错误
结帖,问题解决,感谢各位
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
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;
}
1
privil 2018-01-16 01:03:55 +08:00 via iPhone
写个模版生成呗
|
2
h4lbhg1G 2018-01-16 01:30:12 +08:00
正则表达式先试试,不行就上 lua 脚本喽
|
3
gleymonkey 2018-01-16 01:35:01 +08:00
rewrite /wechat_(.*)$ http://wechat_$1 break;
|
4
axisray OP @gleymonkey rewrite 会返回 302 吧,wechat_XX 是内部域名,客户端访问不到的,一定需要 nginx 反代
|
5
msg7086 2018-01-16 04:42:48 +08:00 1
location ~ \/(wechat_.*)\/ { proxy_pass http://$1/; } 这样?
|
6
cxbig 2018-01-16 05:23:59 +08:00
有定数可以用 map 或 regexp
无定数可以用 regexp |
7
paranoiagu 2018-01-16 08:05:08 +08:00 via Android
@cxbig 我也有这个疑问,不定数的,怎么写 regexp ?
|
11
axisray OP |
13
v2orz 2018-01-16 09:28:14 +08:00
关注一下
|
15
axisray OP @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: "*******" |
16
axisray OP 卧槽,码没打全……
|
17
cxbig 2018-01-16 16:43:25 +08:00 1
@paranoiagu
参看这个: https://stackoverflow.com/questions/13706658/variable-capture-in-nginx-location-matching 只要 regex 规则写对了就行,如果有例外,写在这个 location 之前。 |
18
paranoiagu 2018-01-16 19:53:10 +08:00
|
19
cxbig 2018-01-17 04:17:10 +08:00
|
20
paranoiagu 2018-01-17 08:19:42 +08:00 via Android
@cxbig 我也想加 resolver,但是我是 docker,其实后端是不同的 docker,所以其实是机器名。而且没有 dns 服务器。我试了用 resolver 127.0.0.1 会提示无法连接 127.0.0.1:53。
|
21
cxbig 2018-01-18 03:21:18 +08:00
|
22
paranoiagu 2018-01-18 08:25:03 +08:00 via Android
@cxbig 对,我一个个写死没问题的。改正则就不行了。
|
23
axisray OP @paranoiagu 嗯,我也是 docker 环境,测试没问题,放到生产就炸了
|
24
axisray OP @paranoiagu 15 楼那个错误对我来说是正常的,因为我环境里没那几个域名,瞎写的,解析不出来就对了
|
25
axisray OP |
26
axisray OP @paranoiagu
resolver 127.0.0.11; 看下面的回答,是 11 而不是 1 写死的话能解决,但是可能会有问题 建议还是写个脚本去解析 /etc/resolv.conf https://stackoverflow.com/questions/35744650/docker-network-nginx-resolver#comment68472082_37656784 |
27
paranoiagu 2018-01-22 08:24:11 +08:00 via Android
@axisray 多谢,今天再试试看。
|