根据请求头分发
loc: /aa ==> http://127.0.0.1:28080/aa
pro: /aa ==> http://127.0.0.1:8011/api/
其它: /aa ==> http://127.0.0.1:8022/api/
大佬们,原来只有 loc 和其它一切正常,加上 pro 就不行了,如何配置可以实现这个效果吗?似乎 if 里面 proxy_pass 不能有/api/地址
location /aa {
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header X-real-ip $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
if ($http_environment_name = "loc" ) {
proxy_pass http://127.0.0.1:28080;
break;
}
if ($http_environment_name = "pro" ) {
proxy_pass http://127.0.0.1:8011/api/;
break;
}
proxy_pass http://127.0.0.1:8022/api/;
}
1
Hopetree 4 天前
以前写过一个判断是否移动端的,可以参考我这个写法:![]( https://tendcode.com/cdn/2024/202501131703259.png)
|
2
smallparking 4 天前 1
使用 map ,测试了一下没问题
``` # 根据请求头 environment_name 映射目标地址 map $http_environment_name $backend { default http://127.0.0.1:8022/api/; # 默认地址 loc http://127.0.0.1:28080; # loc 的地址 pro http://127.0.0.1:8011/api/; # pro 的地址 } server { listen 80; location /aa { proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade"; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $remote_addr; # 动态选择后端 proxy_pass $backend; } } ``` ``` ~ » cat test_njet.sh 130 ↵ xtcc@xtcc-b560m-power curl -H "environment-name: " http://127.0.0.1:8089/aa curl -H "environment-name: ssss" http://127.0.0.1:8089/aa curl -H "environment-name: loc" http://127.0.0.1:8089/aa curl -H "environment-name: pro" http://127.0.0.1:8089/aa ``` ``` 2025/01/13 17:02:01 [error] 238783#0: *1 connect() failed (111: Connection refused) while connecting to upstream, client: 127.0.0.1, server: , request: "GET /aa HTTP/1.1", upstream: "http://127.0.0.1:8022/api/", host: "127.0.0.1:8089" 2025/01/13 17:02:01 [error] 238784#0: *3 connect() failed (111: Connection refused) while connecting to upstream, client: 127.0.0.1, server: , request: "GET /aa HTTP/1.1", upstream: "http://127.0.0.1:8022/api/", host: "127.0.0.1:8089" 2025/01/13 17:02:01 [error] 238785#0: *5 connect() failed (111: Connection refused) while connecting to upstream, client: 127.0.0.1, server: , request: "GET /aa HTTP/1.1", upstream: "http://127.0.0.1:28080/aa", host: "127.0.0.1:8089" 2025/01/13 17:02:01 [error] 238786#0: *7 connect() failed (111: Connection refused) while connecting to upstream, client: 127.0.0.1, server: , request: "GET /aa HTTP/1.1", upstream: "http://127.0.0.1:8011/api/", host: "127.0.0.1:8089" ``` 顺便推广一下我们的 njet: https://gitee.com/njet-rd/njet |
3
guanzhangzhang 4 天前 1
写 map 设置变量值即可
|
4
5200 4 天前
要不你统一一下,
http://127.0.0.1:28080 http://127.0.0.1:8011/api/ http://127.0.0.1:8022/api/ 在 api 后面去掉 /, 不然你替换后的路径看着有问题,会有两个 // api//test 这样,可能有些语言会兼容,但是有些语言就直接报错。 |
5
weilai99 4 天前
proxy_pass api 后边不要加左斜杠,否则转发时会将 location 里的 /aa 去掉,实际转发到了 api/*** 而非/api/aa/***
|
6
exqibao OP @smallparking @guanzhangzhang 还是不太行,使用 map 配置 pro http://127.0.0.1:8011/api/;这种情况访问不通,只有 url 没/api/的是可以正常访问,还是统一一下/api 好些
|
7
exqibao OP 发现 rewrite 可以解决
location /aa { proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade"; proxy_set_header X-real-ip $remote_addr; proxy_set_header X-Forwarded-For $remote_addr; if ($http_environment_name = "loc" ) { proxy_pass http://127.0.0.1:28080; break; } if ($http_environment_name = "pro" ) { rewrite ^/aa /(.*)$ /api/$1 break; proxy_pass http://127.0.0.1:8011; break; } proxy_pass http://127.0.0.1:8022/api/; } |
8
exqibao OP 应该 map 也是可以的,发现 pro 服务有点问题,没重新试,当前用 rewrite
|