Grafana被反向代理及Iframe嵌入

2023-01-18  本文已影响0人  heichong

Grafana如果被网关(类似Nginx、Apisix等)反向代理以后,如果反向代理路径含有前缀(比如grafana实际地址为:http://10.3.23.191:9912,而通过反向代理的地址为:http://10.3.23.191:9906/grafana/),那就需要修改Grafana配置。

这里以APISIX为例,来反向代理Grafana

APISIX配置

以下配置均可以从APISIX-DASHBOARD查看

{
  "nodes": [
    {
      "host": "10.3.23.191",
      "port": 9912,
      "weight": 1
    }
  ],
  "timeout": {
    "connect": 6,
    "send": 6,
    "read": 6
  },
  "type": "roundrobin",
  "scheme": "http",
  "pass_host": "pass",
  "name": "grafana",
  "desc": "grafana",
  "keepalive_pool": {
    "idle_timeout": 60,
    "requests": 1000,
    "size": 320
  }
}
{
  "uri": "/grafana/**",
  "name": "grafana",
  "methods": [
    "GET",
    "POST",
    "PUT",
    "DELETE",
    "PATCH",
    "HEAD",
    "OPTIONS",
    "CONNECT",
    "TRACE",
    "PURGE"
  ],
  "plugins": {
    "proxy-rewrite": {
      "regex_uri": [
        "^/grafana/(.*)",
        "/$1"
      ]
    }
  },
  "upstream_id": "443580032573506244",
  "status": 1
}

可以看到,通过网关访问时,多了/grafana前缀

Grafana配置

我这里以docker-compose方式部署的grafana,其配置文件已经被映射至宿主机data/grafana/grafana.ini
主要修改以下几个地方:

[server]
# The full public facing url you use in browser, used for redirects and emails
# If you use reverse proxy and sub path specify full url (with sub path)
root_url = %(protocol)s://%(domain)s:%(http_port)s/grafana/

image.png
[security]

# set to true if you want to allow browsers to render Grafana in a <frame>, <iframe>, <embed> or <object>. default is false.
allow_embedding = true
image.png
[auth.anonymous]
# enable anonymous access
enabled = true

# specify organization name that should be used for unauthenticated users
org_name = Main Org.

# specify role for unauthenticated users
org_role = Viewer

org_name = Main Org.这是配置只有Main Org.这个组织可以匿名访问
org_role = Viewer配置匿名访问时,只能查看
这里配置的是匿名访问,不会影响登录以后的访问权限。

image.png

重启Grafana

反向代理访问Grafana

http://10.3.23.191:9906/grafana/

image.png image.png

我们可以看到,在没有登录的情况下,已经可以进入到Grafana的页面。但只能查看,无法编辑

分享Dashboard

先登录Grafana,创建或导入一个Dashboard。这里我已经导入过了,不再详解导入过程


image.png

点击分享按钮:


image.png

copy这个link,直接访问,或放入到Iframe中即可匿名访问。
http://10.3.23.191:9906/grafana/d/bLlNuRLWz/apache-apisix?orgId=1&refresh=5s&from=1674007889225&to=1674009689225

image.png

另外还有两种模式

测试

通过APISIX访问Grafana:

上一篇下一篇

猜你喜欢

热点阅读