nginx proxyPass 使用变量传递给 prometheus

nginx proxyPass to prometheus using variable

我有以下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/(.*)$ / break;
}

当我导航到 https://myHost/monitoring/prometheus/graph I get redirected to /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/(.*)$ / break;
}

任何人都可以向我解释为什么使用变量会导致路由方面的不同行为吗?我需要使用变量来强制 nginx 解析每个请求的 dns 名称。

我刚刚弄明白了。 如 docs

中所述

When variables are used in proxy_pass:

location /name/ {
    proxy_pass http://127.0.0.1$request_uri;
}

In this case, if URI is specified in the directive, it is passed to the server as is, replacing the original request 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/(.*)$ / break;
}

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

此沙箱示例显示了完整的工作堆栈,包括 nginx 配置。 https://github.com/prometheus-community/prometheus-playground/tree/master/nginx

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

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