使用仪表板作为子路径的grafana的Nginx反向代理

时间:2018-12-29 21:26:33

标签: nginx grafana nginx-reverse-proxy

我想使用dashboard作为grafana安装的nginx位置。

问题是grafana在其中一些网址中使用了仪表板,例如https://example.com/grafana/dashboard/new?orgId=1,我希望它是https://example.com/dashboard/dashboard/new?orgId=1,我认为我的nginx位置正在重写为https://example.com/dashboard/new?orgId=1

当我设置为使用grafana作为子路径时,它都可以按预期工作;

grafana.ini:

[server]
http_addr = 127.0.0.1
domain = example.com
root_url = %(protocol)s://%(domain)s/grafana/

nginx配置:

# Upstream Servers
upstream grafana_server {
    server localhost:3000;
}

server {
    listen 80;
    listen [::]:80;

    server_name example.com www.example.com;

    return 301 https://$host$request_uri;
}

server {
    listen 443 ssl default_server;
    listen [::]:443 ssl default_server;

    include snippets/ssl-example.com.conf;
    include snippets/ssl-params.conf;

    root /var/www/example.com/html;

    index index.html index.htm;

    server_name example.com www.example.com;

    location /grafana/ {
        proxy_pass http://grafana_server/;
        proxy_set_header Host $host;
    }
}

但是将其更改为dashboard并导航到https://example.com/dashboard/dashboard/new?orgId=1会导致该URL被重写为https://example.com/dashboard/new?orgId=1

grafana.ini:

[server]
http_addr = 127.0.0.1
domain = example.com
root_url = %(protocol)s://%(domain)s/dashboard/

nginx配置:

# Upstream Servers
upstream grafana_server {
    server localhost:3000;
}

server {
    listen 80;
    listen [::]:80;

    server_name example.com www.example.com;

    return 301 https://$host$request_uri;
}

server {
    listen 443 ssl default_server;
    listen [::]:443 ssl default_server;

    include snippets/ssl-example.com.conf;
    include snippets/ssl-params.conf;

    root /var/www/example.com/html;

    index index.html index.htm;

    server_name example.com www.example.com;

    location /dashboard/ {
        proxy_pass http://grafana_server/;
        proxy_set_header Host $host;
    }
}

所以我已经尝试过在nginx位置进行重写,但是无法使其按要求工作(真的不知道在这里做什么)

location ~ (\/dashboard\/) {
    proxy_pass http://grafana_server$1;
    proxy_set_header Host $host;
}

location ~ /dashboard/ {
    rewrite ^ /dashboard/$1;
    proxy_pass http://grafana_server;
    proxy_set_header Host $host;
}

任何帮助将不胜感激。

此致

1 个答案:

答案 0 :(得分:0)

我知道这有点晚了-但我偶然发现了同一问题,并认为如果别人碰到这个话题,我会分享的。

这不是nginx的问题,而是grafana本身。

除了用其他方法root_url重命名/dashboard的最后一部分外,我无法解决其他问题