[转帖]Run Grafana behind a reverse proxy
Newsletter
Want to stay on top of Grafana and Observability news? Sign up for our newsletter.
By Grafana Labs Team
Last update on July 18, 2023
Introduction
In this tutorial, you’ll configure Grafana to run behind a reverse proxy.
When running Grafana behind a proxy, you need to configure the domain name to let Grafana know how to render links and redirects correctly.
- In the Grafana configuration file, change
server.domainto the domain name you’ll be using:
[server]
domain = example.com
- Restart Grafana for the new changes to take effect.
You can also serve Grafana behind a sub path, such as http://example.com/grafana.
To serve Grafana behind a sub path:
- Include the sub path at the end of the
root_url. - Set
serve_from_sub_pathtotrue.
[server]
domain = example.com
root_url = %(protocol)s://%(domain)s:%(http_port)s/grafana/
serve_from_sub_path = true
Next, you need to configure your reverse proxy.
Configure NGINX
NGINX is a high performance load balancer, web server, and reverse proxy.
- In your NGINX configuration file inside
httpsection, add the following:
# 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 $http_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 $http_host;
proxy_pass http://grafana;
}
}
- Reload the NGINX configuration.
- Navigate to port 80 on the machine NGINX is running on. You’re greeted by the Grafana login page.
For Grafana Live which uses WebSocket connections you may have to raise Nginx worker_connections option which is 512 by default – which limits the number of possible concurrent connections with Grafana Live.
Also, be aware that the above configuration will work only when the proxy_pass value for location / is a literal string. If you are using a variable here, read this GitHub issue. You will need to add an appropriate NGINX rewrite rule.
To configure NGINX to serve Grafana under a sub path, update the location block:
# 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 $http_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 $http_host;
proxy_pass http://grafana;
}
}
If your Grafana configuration does not set serve_from_sub_path to true then you need to add a rewrite rule to each location block:
rewrite ^/grafana/(.*) /$1 break;
Note: If Grafana is being served from behind a NGINX proxy with TLS termination enabled, then the
root_urlshould be set accordingly. For example, if Grafana is being served fromhttps://example.com/grafanathen theroot_urlshould be set tohttps://example.com/grafana/orhttps://%(domain)s/grafana/(and the correspondingdomainshould be set toexample.com) in theserversection of the Grafana configuration file. Theprotocolsetting should be set tohttp, because the TLS handshake is being handled by NGINX.
Configure HAProxy
To configure HAProxy to serve Grafana under a sub path:
frontend http-in
bind *:80
use_backend grafana_backend if { path /grafana } or { path_beg /grafana/ }
backend grafana_backend
# Requires haproxy >= 1.6
http-request set-path %[path,regsub(^/grafana/?,/)]
# Works for haproxy < 1.6
# reqrep ^([^\ ]*\ /)grafana[/]?(.*) \1\2
server grafana localhost:3000
Configure IIS
IIS requires that the URL Rewrite module is installed.
To configure IIS to serve Grafana under a sub path, create an Inbound Rule for the parent website in IIS Manager with the following settings:
- pattern:
grafana(/)?(.*) - check the
Ignore casecheckbox - rewrite URL set to
http://localhost:3000/{R:2} - check the
Append query stringcheckbox - check the
Stop processing of subsequent rulescheckbox
This is the rewrite rule that is generated in the web.config:
<rewrite>
<rules>
<rule name="Grafana" enabled="true" stopProcessing="true">
<match url="grafana(/)?(.*)" />
<action type="Rewrite" url="http://localhost:3000/{R:2}" logRewrittenUrl="false" />
</rule>
</rules>
</rewrite>
See the tutorial on IIS URL Rewrites for more in-depth instructions.
Configure Traefik
Traefik Cloud Native Reverse Proxy / Load Balancer / Edge Router
Using the docker provider the following labels will configure the router and service for a domain or subdomain routing.
labels:
traefik.http.routers.grafana.rule: Host(`grafana.example.com`)
traefik.http.services.grafana.loadbalancer.server.port: 3000
To deploy on a sub path
labels:
traefik.http.routers.grafana.rule: Host(`example.com`) && PathPrefix(`/grafana`)
traefik.http.services.grafana.loadbalancer.server.port: 3000
Examples using the file provider.
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
Summary
In this tutorial you learned how to run Grafana behind a reverse proxy.
[转帖]Run Grafana behind a reverse proxy的更多相关文章
- Nginx应用-Location路由反向代理及重写策略 请求转发-URL匹配规则 NGINX Reverse Proxy
NGINX Docs | NGINX Reverse Proxy https://docs.nginx.com/nginx/admin-guide/web-server/reverse-proxy/ ...
- Master Nginx(5) - Reverse Proxy Advanced Topics
Security through separtion Encrypting traffic with SSL Authenticating clients using SSL Blocking tra ...
- Forward Proxy & Reverse Proxy | 正向代理 和 反向代理
对请求和响应内容不做修改的转发的服务器,被称为代理服务器.代理服务器分为两种类型:正向代理 和 反向代理. 正向代理:面向互联网,从更广范围获取信息的代理. 反向代理:面向内部,一般用于某企业的网站的 ...
- 反向代理(Reverse Proxy)
反向代理(Reverse Proxy)方式是指以代理服务器来接受internet上的连接请求,然后将请求转发给内部网络上的服务器,并将从服务器上得到的结果返回给internet上请求连接的客户端,此时 ...
- 正向代理 forward proxy、反向代理 reverse proxy、透明代理 transparent proxy nginx反向代理原理和配置讲解 防止外部客户机获取内部内容服务器的重定向 URL 缓存命中
[大型网站技术实践]初级篇:借助Nginx搭建反向代理服务器 - Edison Chou - 博客园http://www.cnblogs.com/edisonchou/p/4126742.html 图 ...
- Differences Between 3 Types Of Proxy Servers: Normal, Transparent And Reverse Proxy
What is a Proxy Server? A Proxy server is an intermediary machine, between a client and the actual s ...
- Forward Proxy vs Reverse Proxy
Overview We've talked about reverse proxy servers and how they can really be good at protecting the ...
- an open source web server and reverse proxy
https://www.nginx.com/resources/admin-guide/ NGINX is an open source web server and reverse proxy th ...
- Nginx & Reverse Proxy
Nginx & Reverse Proxy https://docs.nginx.com/nginx/admin-guide/web-server/reverse-proxy/ https:/ ...
- 反向代理服务器(Reverse Proxy)
反向代理服务器(Reverse Proxy) 普通代理服务器是帮助内部网络的计算机访问外部网络.通常,代理服务器同时连接内网和外网.首先内网的计算机需要设置代理服务器地址和端口,然后将HTTP请求 ...
随机推荐
- 2024-01-03:用go语言,给你两个长度为 n 下标从 0 开始的整数数组 cost 和 time, 分别表示给 n 堵不同的墙刷油漆需要的开销和时间。你有两名油漆匠, 一位需要 付费 的油漆匠
2024-01-03:用go语言,给你两个长度为 n 下标从 0 开始的整数数组 cost 和 time, 分别表示给 n 堵不同的墙刷油漆需要的开销和时间.你有两名油漆匠, 一位需要 付费 的油漆匠 ...
- springboot整合hibernate(非JPA)(一)
springboot整合hibernate(非JPA)(一) springboot整合hibernate,非jpa,若是jpa就简单了,但是公司项目只有hibernate,并要求支持多数据库,因此记录 ...
- 打造 VSCode 高效 C++ 开发环境的必备插件
工欲善其事,必先利其器 C++ clangd:代码补全.跳转.clang-tidy 检查,自带 clang-format CodeLLDB:LLVM 的调试器(类比 GDB) CMake CMake ...
- vue部署项目报错导致空白页解决
在nginx上部署项目出现空白页并报错 解决方法: 在vue的vue.config.js文件中 改成:module.exports = {publicPath: './'}
- 18、Scaffold 布局组件
Scaffold 是一个非常重要的布局组件,它提供了一个常见的应用程序结构布局,包含了许多基础的 UI 元素和交互方式. Scaffold 组件主要由以下几个部分组成: AppBar:用于显示在页面顶 ...
- WMTS地图服务每一层级分辨率
目录 1. 概述 2. 详论 2.1. Web墨卡托 2.2. 大地经纬度 3. 参考 1. 概述 WMTS地图服务每一层级的分辨率是多少?关于这个问题以前推算过,但总是忘记了.网上查询又是一堆废话, ...
- Go语言实现GoF设计模式:备忘录模式的实践探索
本文分享自华为云社区<[Go实现]实践GoF的23种设计模式:备忘录模式>,作者:元闰子. 简介 相对于代理模式.工厂模式等设计模式,备忘录模式(Memento)在我们日常开发中出镜率并不 ...
- 共赴元宇宙新纪元,华为云VR开发应用大赛总决赛倒计时7天!
摘要:"第三届华为云VR开发应用暨沈阳元宇宙开发应用大赛"总决赛即将于2月13日在沈阳市和平区揭幕. 由沈阳市和平区人民政府.华为技术有限公司共同主办的"第三届华为云VR ...
- 源码详解数据结构Linked List
摘要:java.util.LinkedList 是 Java 集合框架中的成员之一,底层是基于双向链表实现,集合容量可动态变化的. 本文分享自华为云社区<LinkedList 源码分析>, ...
- linux 账号管理 添加用户与修改用户权限与密码
添加用户 useradd zhoulujun 修改用户密码 passwd zhoulujun 修改用户组 usermod -g root zhoulujun 记住这个几个就够了--