使用变量将nginx proxyPass传递给prometheus

使用变量将nginx proxyPass传递给prometheus,nginx,reverse-proxy,prometheus,proxypass,Nginx,Reverse Proxy,Prometheus,Proxypass,我有以下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

我有以下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;
}
当我导航到时,我被重定向到/graph()

当我不使用变量并将url直接放置到proxy_pass时,一切都按预期进行。我可以导航到普罗米修斯并看到它

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名称。

我刚刚找到了答案。 如合同中所述

在代理过程中使用变量时:

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;
}

我认为最好使用prometheus
--web.externalURL
命令行选项

这个沙盒示例显示了一个完整的工作堆栈,包括nginx config。

值得注意的是
docker compose.yml
文件的prometheus命令部分

command:
- --config.file=/etc/prometheus/prometheus.yml
- --web.route-prefix=/
- --web.external-url=http://example.com/prometheus