站点图标 Lee's Blog

使用NGINX代理Grafana

配置Grafana配置文件

配置文件可能存在于/etc/grafana/grafana.ini中,如果像我一样下载官方tar.gz文件解压的话,配置文件在$grafana/conf/defaults.ini

[server]
# Protocol (http, https, h2, socket)
protocol = http
# The http port to use
http_port = 3000
# The public facing domain name used to access grafana from a browser
domain = example.com
# Redirect to correct domain if host header does not match domain
# Prevents DNS rebinding attacks
enforce_domain = true
# The full public facing url
#root_url = %(protocol)s://%(domain)s:%(http_port)s/gr/
root_url = %(protocol)s://%(domain)s/grafana
# Serve Grafana from subpath specified in `root_url` setting. By default it is set to `false` for compatibility reasons.
serve_from_sub_path = false

需要注意的是”domain”需要填写,端口可以更换,更重要的”serve_from_sub_path”一定要填写为false,网上很多教程都使用true,这样会导致在配置好nginx后访问https://xxx/grafana 后跳转到http://xxx:3000/grafana

很多服务都会提供这种子地址访问,如果不需要nginx配置https的话可以考虑这种方式,将端口进行更改,”serve_from_sub_path”填写为true,”protocol”填写https 下面的”cert_file””cert_key”填写上证书位置,这样不通过nginx代理也可以直接开启https。

NGINX配置

    location /grafana {
        root   html;
        index  index.html index.htm;
        add_header 'Access-Control-Allow-Origin' '*';
        add_header Access-Control-Allow-Methods GET,POST,OPTIONS,DELETE;
        add_header 'Access-Control-Allow-Headers' 'userId,DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
        proxy_pass http://127.0.0.1:3000;
        rewrite ^/grafana/(.*) /$1 break;
        proxy_set_header   Host $host;
    }
退出移动版