在反向代理后运行 Grafana
简介
在本教程中,你将配置 Grafana 在反向代理后运行。
在代理后运行 Grafana 时,你需要配置域名,以便让 Grafana 知道如何正确渲染链接和重定向。
- 在 Grafana 配置文件中,将
server.domain
改为你将要使用的域名
[server]
domain = example.com
- 重启 Grafana 使更改生效。
配置反向代理
配置 nginx
nginx 是一个高性能的负载均衡器、Web 服务器和反向代理。
- 在你的 nginx 配置文件中的
http
部分,添加以下内容
# This is required to proxy Grafana Live WebSocket connections.
map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}
upstream grafana {
server localhost:3000;
}
server {
listen 80;
root /usr/share/nginx/html;
index index.html index.htm;
location / {
proxy_set_header Host $host;
proxy_pass http://grafana;
}
# Proxy Grafana Live WebSocket connections.
location /api/live/ {
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
proxy_set_header Host $host;
proxy_pass http://grafana;
}
}
- 重新加载 nginx 配置。
- 导航到运行 nginx 的机器的 80 端口。你将看到 Grafana 登录页面。
对于使用 WebSocket 连接的 Grafana Live,你可能需要提高 nginx 的 worker_connections
选项值,默认值为 512
。默认值限制了与 Grafana Live 的并发连接数。
此外,请注意,上述配置仅在 location /
的 proxy_pass
值为字面字符串时有效。如果此处想使用变量,则必须使用 重写规则。更多信息,请参阅 GitHub issue #18299。
要配置 nginx 在 子路径 下提供 Grafana 服务,请更新 location
块
# This is required to proxy Grafana Live WebSocket connections.
map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}
upstream grafana {
server localhost:3000;
}
server {
listen 80;
root /usr/share/nginx/www;
index index.html index.htm;
location /grafana/ {
proxy_set_header Host $host;
proxy_pass http://grafana;
}
# Proxy Grafana Live WebSocket connections.
location /grafana/api/live/ {
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
proxy_set_header Host $host;
proxy_pass http://grafana;
}
}
为每个 location 块添加重写规则
rewrite ^/grafana/(.*) /$1 break;
注意
如果 nginx 执行 TLS 终止,则必须相应地设置
root_url
和protocol
配置。如果你从https://example.com/grafana/
提供 Grafana 服务,则root_url
应为https://example.com/grafana/
或https://%(domain)s/grafana/
,并且在 Grafana 配置文件的server
部分将相应的domain
配置值设置为example.com
。将protocol
设置为http
。
配置 HAProxy
要配置 HAProxy 在 子路径 下提供 Grafana 服务
frontend http-in
bind *:80
use_backend grafana_backend if { path /grafana } or { path_beg /grafana/ }
backend grafana_backend
server grafana localhost:3000
# Requires haproxy >= 1.6
http-request set-path %[path,regsub(^/grafana/?,/)]
# Works for haproxy < 1.6
# reqrep ^([^\ ]*\ /)grafana[/]?(.*) \1\2
server grafana localhost:3000
配置 IIS
注意
要使用 IIS,必须安装 URL 重写模块。
要配置 IIS 在 子路径 下提供 Grafana 服务,请在 IIS 管理器 中为父级网站创建 Inbound Rule
,设置如下:
- 模式:
grafana(/)?(.*)
- 勾选
忽略大小写
复选框 - 重写 URL 设置为
https://:3000/{R:2}
- 勾选
追加查询字符串
复选框 - 勾选
停止处理后续规则
复选框
这是在 web.config
中生成的重写规则
<rewrite>
<rules>
<rule name="Grafana" enabled="true" stopProcessing="true">
<match url="grafana(/)?(.*)" />
<action type="Rewrite" url="https://:3000/{R:2}" logRewrittenUrl="false" />
</rule>
</rules>
</rewrite>
有关更详细的说明,请参阅关于 IIS URL 重写的教程。
配置 Apache
要使用 Apache 作为代理,请确保已正确安装和配置。
- 确保 Apache 代理模块
mod_proxy
已安装并启用。要启用,运行以下命令
a2enmod proxy
a2enmod proxy_http
- 要配置代理,编辑站点配置文件。为此,在
<VirtualHost>
部分内部,添加以下代码
ProxyPreserveHost on
ProxyPass / http://your_grafana_server:3000
ProxyPassReverse / http://your_grafana_server:3000
- 最后,重启 Apache 使设置生效。
重启后,导航到你的 Apache 服务器的 80 端口,你将被重定向到 Grafana。
要配置托管在子路径中的 Grafana,请用以下代码替换子路径(假设你的 Grafana 实例位于子路径 your_path
)
ProxyPreserveHost on
ProxyPass /your_path http://your_grafana_server:3000
ProxyPassReverse /your_path http://your_grafana_server:3000
ProxyPass / http://your_grafana_server:3000/your_path
ProxyPassReverse / http://192.168.250.5:3000/your_path
请注意,包含 your_path
的行必须放在引用根路径 (/
) 的行之前,这样才能正常工作。
配置 Traefik
Traefik 云原生应用代理。
使用 Docker 提供者和以下标签配置域名或子域名的路由和服務。
labels:
traefik.http.routers.grafana.rule: Host(`grafana.example.com`)
traefik.http.services.grafana.loadbalancer.server.port: 3000
部署在 子路径
labels:
traefik.http.routers.grafana.rule: Host(`example.com`) && PathPrefix(`/grafana`)
traefik.http.services.grafana.loadbalancer.server.port: 3000
使用文件提供者的示例。
http:
routers:
grafana:
rule: Host(`grafana.example.com`)
service: grafana
services:
grafana:
loadBalancer:
servers:
- url: http://192.168.30.10:3000
http:
routers:
grafana:
rule: Host(`example.com`) && PathPrefix(`/grafana`)
service: grafana
services:
grafana:
loadBalancer:
servers:
- url: http://192.168.30.10:3000
在子路径下提供 Grafana 服务的备选方案
注意
只有当你的反向代理配置不处理子路径服务时才需要此项。
如果你不想或无法使用反向代理来处理从 子路径 提供 Grafana 服务,可以将配置变量 server_from_sub_path
设置为 true
。
- 在
root_url
的末尾包含子路径。 - 将
serve_from_sub_path
设置为true
[server]
domain = example.com
root_url = %(protocol)s://%(domain)s:%(http_port)s/grafana/
serve_from_sub_path = true