ev0*_*n37 11 variables nginx proxypass resolver
我试图让 NGINX 的解析器自动更新 DNS 解析缓存,所以我正在过渡到使用变量作为proxy_pass
值来实现这一点。但是,当我确实使用变量时,它会使所有请求都转到请求的根端点并切断 url 的任何其他路径。这是我的配置:
resolver 10.0.0.2 valid=10s;
server {
listen 80;
server_name localhost;
location /api/test-service/ {
proxy_redirect off;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
# If these 2 lines are uncommented, 'http://example.com/api/test-service/test' goes to 'http://example.com/api/test-service/'
set $pass_url http://test-microservice.example.com:80/;
proxy_pass $pass_url;
# If this line is uncommented, things work as expected. 'http://example.com/api/test-service/test' goes to 'http://example.com/api/test-service/test'
# proxy_pass http://test-microservice.example.com:80/;
}
Run Code Online (Sandbox Code Playgroud)
这对我来说没有任何意义,因为硬编码的 URL 和变量的值是相同的。有什么我想念的吗?
编辑:啊,所以我发现了这个问题。但我不完全确定如何处理它。由于这是一个反向代理,我需要在将 URI 传递给代理之前从 URI 中proxy_pass
删除/api/test-service/
它。所以..
这个:
http://example.com/api/test-service/test
Run Code Online (Sandbox Code Playgroud)
应该代理这个:
http://test-microservice.example.com:80/test
Run Code Online (Sandbox Code Playgroud)
而是代理这个:
http://test-microservice.example.com:80/api/test-service/test
Run Code Online (Sandbox Code Playgroud)
当我不使用变量时,它丢弃它没问题。但是变量添加了它。这只是使用变量的本质吗?
Tar*_*ani 11
您在文档中遗漏了一个小点
在 proxy_pass 中使用变量时:
Run Code Online (Sandbox Code Playgroud)location /name/ { proxy_pass http://127.0.0.1$request_uri; }
在这种情况下,如果在指令中指定了 URI,它将按原样传递给服务器,替换原始请求 URI。
所以你的配置需要更改为
set $pass_url http://test-microservice.example.com:80$request_uri;
proxy_pass $pass_url;
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
6096 次 |
最近记录: |