Yan*_*lem 5 reverse-proxy nginx proxypass prometheus
我有以下 nginx.conf
location /monitoring/prometheus/ {
resolver 172.20.0.10 valid=5s;
set $prometheusUrl http://prometheus.monitoring.svc.cluster.local:9090/;
proxy_set_header Accept-Encoding "";
proxy_pass $prometheusUrl;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
sub_filter_types text/html;
sub_filter_once off;
sub_filter '="/' '="/monitoring/prometheus/';
sub_filter 'var PATH_PREFIX = "";' 'var PATH_PREFIX = "/monitoring/prometheus";';
rewrite ^/monitoring/prometheus/?$ /monitoring/prometheus/graph redirect;
rewrite ^/monitoring/prometheus/(.*)$ /$1 break;
}
Run Code Online (Sandbox Code Playgroud)
当我导航到https://myHost/monitoring/prometheus/graph 时,我被重定向到 /graph ( https://myHost/graph )
当我不使用变量并将 url 直接放置到 proxy_pass 时,一切都按预期工作。我可以导航到https://myHost/monitoring/prometheus/graph并查看 prometheus。
location /monitoring/prometheus/ {
resolver 172.20.0.10 valid=5s;
proxy_set_header Accept-Encoding "";
proxy_pass http://prometheus.monitoring.svc.cluster.local:9090/;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
sub_filter_types text/html;
sub_filter_once off;
sub_filter '="/' '="/monitoring/prometheus/';
sub_filter 'var PATH_PREFIX = "";' 'var PATH_PREFIX = "/monitoring/prometheus";';
rewrite ^/monitoring/prometheus/?$ /monitoring/prometheus/graph redirect;
rewrite ^/monitoring/prometheus/(.*)$ /$1 break;
}
Run Code Online (Sandbox Code Playgroud)
谁能向我解释为什么在路由方面使用该变量会导致不同的行为?我需要使用变量来强制 nginx 解析每个请求的 dns 名称。
我只是想通了。如文档中所述
在 proxy_pass 中使用变量时:
Run Code Online (Sandbox Code Playgroud)location /name/ { proxy_pass http://127.0.0.1$request_uri; }在这种情况下,如果在指令中指定了 URI,它将按原样传递给服务器,替换原始请求 URI。
所以问题是我在变量(尾随 /)中指定了一个请求 uri。删除此后/一切正常。
这里的工作配置:
location /monitoring/prometheus/ {
set $prometheusUrl http://prometheus.monitoring.svc.cluster.local:9090;
proxy_set_header Accept-Encoding "";
proxy_pass $prometheusUrl;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
sub_filter_types text/html;
sub_filter_once off;
sub_filter '="/' '="/monitoring/prometheus/';
sub_filter 'var PATH_PREFIX = "";' 'var PATH_PREFIX = "/monitoring/prometheus";';
rewrite ^/monitoring/prometheus/?$ /monitoring/prometheus/graph redirect;
rewrite ^/monitoring/prometheus/(.*)$ /$1 break;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3784 次 |
| 最近记录: |