设置变量会影响分流 (Nginx特殊case)

本文阅读量 Posted by Kird on 2019-12-02
仅仅将集群修改成变量模式,我的流量怎么开始不对了??

背景,普通locaiton跳转

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
【第一段】
server {
listen 80;
server_name test.com;
location /hello {
proxy_pass http://test_pool/;
proxy_set_header Host $host;
}
}
upstream test_pool{
server 127.0.0.1:65533 ;
}
server {
listen 65533;
server_name test.com;
location / {
return 200 "/";
}
location /hello {
return 200 "/hello";
}

}
1
2
3
访问测试:
curl -H "Host:test.com" http://127.0.0.1/hello
/

同理,增加一个location后,访问curl -H “Host:test.com” http://127.0.0.1/1/hello 返回/hello

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
【第二段】
server {
listen 80;
server_name test.com;
location /1 {
proxy_pass http://test_pool/;
proxy_set_header Host $host;
}

}
upstream test_pool{
server 127.0.0.1:65533 ;
}
server {
listen 65533;
server_name test.com;
location / {
return 200 "/";
}
location /hello {
return 200 "/hello";
}
1
2
3
以上配置情况,如下测试:
curl -H "Host:test.com" http://10.9.198.26/1/hello
/hello

在第二段配置的基础上,将集群改成变量

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
【第三段】
server {
listen 80;
server_name test.com;
location /1 {
set $host_pass test_pool;
proxy_pass http://$host_pass/;
proxy_set_header Host $host;
}

}
upstream test_pool{
server 127.0.0.1:65533 ;
}
server {
listen 65533;
server_name test.com;
location / {
return 200 "/";
}
location /hello {
return 200 "/hello";
}
1
2
3
以上配置情况,如下测试:
curl -H "Host:test.com" http://127.0.0.1/1/hello
/

甚至 在不使用变量的情况下404的url,在使用变量proxy_pass下,全部直接打到集群的根目录,比如:

1
2
curl -H "Host:test.com" http://127.0.0.1/1/hellofdsfsdffdsfsd
/

如果在不使用变量的情况下(第二段配置),则返回:

1
2
curl -H "Host:test.com" http://127.0.0.1/1/hellofdsfsdffdsfsd -I
HTTP/1.1 404 Not Found


支付宝打赏 微信打赏

赞赏支持一下