系列文章目录
提示:这里可以添加系列文章的所有文章的目录,目录需要自己手动添加
nginx 代理grafana
nginx 代理grafana(8.5.3)解决了反向代理的问题
@TOC
前言
在上一篇中使用的8.5.2的版本的grafana,不定期会出现nginx的反向代理出错误,无法保存的错误或ws无法代理的问题。查了无数的资料,最后在老外的blob上看到,这个是个bug(8.5.2)前都有这个问题。然后我升级了新版的8.5.3,发现这个bug解决了。
提示:以下是本篇文章正文内容,下面案例可供参考
一、准备环境
我准备了两台机器,一台是我的法宝。群辉N920+ 。在这里装个docker,一台是一个mini小主机做部署测试用。
N920:192.168.1.122
mini:192.168.1.120
二、拉镜像
请参考官方的文旦:https://grafana.com/docs/grafana/latest/installation/docker/#migrate-to-v51-or-later
在上面满屏的说明中有用的就几部分:
1 grafana/grafana-enterprise —这个是企业版的,如果你不想付钱,就去docker找grafana/grafana 这个版本。
2 告诉public的开源源在grafana/grafana-oss上。
3 告诉你怎么运行。
4 告诉你各个目录在哪里找,这个后面有需要
5 要注意的是User ID这部分。因为默认的grafana用户是没有权限来修改配置文件的,所有的配置文件都是root身份。这也是为什么我生产两次容器,来获取这个配置文件,如果你懒得挂接, 那就直接用-u root来启动,然后根据文档改文件组属性吧
1. grafana的镜像
提示: -v /volume6/linux_mount/grafana122.ini:/etc/grafana/grafana.ini , 这部分在第一run的时候是没有的。需要在第一次启动后把它copy出来编辑,然后再第二次挂接时带上
root@Home_nas:/volume6/linux_mount# docker run -idt --name grafana -p 3000:3000 -v /volume6/linux_mount/grafana122.ini:/etc/grafana/grafana.ini grafana/grafana /bin/bash
这个镜像我在122 和120上分别部署了两次,用来测试不同的效果。
2. nginx镜像
docker run -itd --name nginx1 -p 1340:80 -p 1341:8081 nginx /bin/bash
3 . 在122上登录grafana
没问题,系统正常启动,没有任何的错误。
为了省事,我把grafana的数据库指向了我本地的mysql数据库,修改红色的部分
4. 在nginx的conf.d中添加一个granfana的代理(重点在这里)
这个是官方的文档:https://grafana.com/tutorials/run-grafana-behind-a-proxy/
1 ) 需要修改grafna的配置文件:
2) 修改nginx的部分
注意:ws 的部分需要使用到的是 (api/live)这段。如果你是1级代理域名(就是IP或域名后面直接 “”/“” 的,直接copy过去),然后将两个localhost 改成你的域名或IP
我的配置如下:在这里我分别指向了我的两台机器122/120 都可以正常代理
map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}
server {
listen 8081;
listen [::]:8081;
server_name 192.168.1.122;
access_log /var/log/nginx/host.access.log main;
error_log /var/log/nginx/host.error.log warn;
location / {
proxy_set_header Host $http_host;
proxy_pass http://192.168.1.122:3000/;
}
# Proxy Grafana Live WebSocket connections.
location /api/live {
rewrite ^/(.*) /$1 break;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
proxy_set_header Host $http_host;
proxy_pass http://192.168.1.122:3000/;
}
}
5. 在说一下cdn的部分
这里 需要修改的部分如下:
1 ) http_port (这里是你启动这个机器的port)
2 ) domain(这个是你在浏览器的doman或地址)
3 ) root_url(这个显示在浏览器的内容,实际上就是 proxy_set_header Host $http_host; 这部分)
5 ) serce_from_sub_path 这个支持二级域名的部分,如果是一级解析,开不开无所谓
6 ) cdn的配置部分
7 ) 当你使用exec命令进入grafana的容器的时候,看到的内容如下:
root@Home_nas:/volume6/linux_mount/grafana-oss/8.5.3# docker exec -it grafana /bin/bash
bash-5.1$ ls
LICENSE NOTICE.md README.md VERSION bin conf plugins-bundled public scripts
bash-5.1$ pwd
/usr/share/grafana
bash-5.1$
其中public的部分就是你要放到cdn中的部分。
#################################### Server ####################################
[server]
# Protocol (http, https, h2, socket)
;protocol = http
# The ip address to bind to, empty will bind to all interfaces
;http_addr =
# The http port to use
http_port = 3000
# The public facing domain name used to access grafana from a browser
domain = 192.168.1.120
# Redirect to correct domain if host header does not match domain
# Prevents DNS rebinding attacks
;enforce_domain = false
# 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/
# Serve Grafana from subpath specified in `root_url` setting. By default it is set to `false` for compatibility reasons.
serve_from_sub_path = true
# Log web requests
;router_logging = false
# the path relative working path
;static_root_path = public
# enable gzip
;enable_gzip = false
# https certs & key file
;cert_file =
;cert_key =
# Unix socket path
;socket =
# CDN Url
cdn_url =http://192.168.1.122:1341/
如果你看到了下面的图片,就是你的cdn配置生效了。 注意红色的框的部分。这是你要在cdn上建立的目录,不要错了。
/volume6/linux_mount/grafana-oss
root@Home_nas:/volume6/linux_mount/grafana-oss# ls
8.5.2 8.5.3
root@Home_nas:/volume6/linux_mount/grafana-oss#
这是启动后