我有以下的nginx.conf配置文件:
当我访问 https://myHost/monitoring/prometheus/graph 时,会被重定向到 /graph (https://myHost/graph)。 当我不使用变量并直接将 URL 放置到 proxy_pass 中时,一切都按预期工作。我可以访问 https://myHost/monitoring/prometheus/graph 并查看 Prometheus。
有人能向我解释一下为什么使用变量会导致路由行为不同吗?我需要使用变量来强制nginx在每个请求中解析DNS名称。
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;
}
当我访问 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;
}
有人能向我解释一下为什么使用变量会导致路由行为不同吗?我需要使用变量来强制nginx在每个请求中解析DNS名称。
80
。其他端口对我来说都无法工作。我还向prometheus v2.28.1添加了--web.route-prefix=/
和--web.external-url=http://<ip>/prometheus
标志。另一个注意点是警告nginx: [warn] duplicate MIME type "text/html"
,看起来sub_filter_types
不再必要。 - philo