Newsletter

Want to stay on top of Grafana and Observability news? Sign up for our newsletter.

Email*
Subscribe

By Grafana Labs Team

Last update on July 18, 2023

Advanced

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.domain to the domain name you’ll be using:
bashCopy

 
[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_path to true.
bashCopy

 
[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 http section, add the following:
nginxCopy

 
Expand code
# 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:

nginxCopy

 
Expand code
# 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:

Copy
 rewrite  ^/grafana/(.*)  /$1 break;

Note: If Grafana is being served from behind a NGINX proxy with TLS termination enabled, then the root_url should be set accordingly. For example, if Grafana is being served from https://example.com/grafana then the root_url should be set to https://example.com/grafana/ or https://%(domain)s/grafana/ (and the corresponding domain should be set to example.com) in the server section of the Grafana configuration file. The protocol setting should be set to http, because the TLS handshake is being handled by NGINX.

Configure HAProxy

To configure HAProxy to serve Grafana under a sub path:

bashCopy

 
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 case checkbox
  • rewrite URL set to http://localhost:3000/{R:2}
  • check the Append query string checkbox
  • check the Stop processing of subsequent rules checkbox

This is the rewrite rule that is generated in the web.config:

xmlCopy

 
  <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.

yamlCopy

 
labels:
traefik.http.routers.grafana.rule: Host(`grafana.example.com`)
traefik.http.services.grafana.loadbalancer.server.port: 3000

To deploy on a sub path

yamlCopy

 
labels:
traefik.http.routers.grafana.rule: Host(`example.com`) && PathPrefix(`/grafana`)
traefik.http.services.grafana.loadbalancer.server.port: 3000

Examples using the file provider.

yamlCopy

 
http:
routers:
grafana:
rule: Host(`grafana.example.com`)
service: grafana
services:
grafana:
loadBalancer:
servers:
- url: http://192.168.30.10:3000
yamlCopy

 
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的更多相关文章

  1. Nginx应用-Location路由反向代理及重写策略 请求转发-URL匹配规则 NGINX Reverse Proxy

    NGINX Docs | NGINX Reverse Proxy https://docs.nginx.com/nginx/admin-guide/web-server/reverse-proxy/ ...

  2. Master Nginx(5) - Reverse Proxy Advanced Topics

    Security through separtion Encrypting traffic with SSL Authenticating clients using SSL Blocking tra ...

  3. Forward Proxy & Reverse Proxy | 正向代理 和 反向代理

    对请求和响应内容不做修改的转发的服务器,被称为代理服务器.代理服务器分为两种类型:正向代理 和 反向代理. 正向代理:面向互联网,从更广范围获取信息的代理. 反向代理:面向内部,一般用于某企业的网站的 ...

  4. 反向代理(Reverse Proxy)

    反向代理(Reverse Proxy)方式是指以代理服务器来接受internet上的连接请求,然后将请求转发给内部网络上的服务器,并将从服务器上得到的结果返回给internet上请求连接的客户端,此时 ...

  5. 正向代理 forward proxy、反向代理 reverse proxy、透明代理 transparent proxy nginx反向代理原理和配置讲解 防止外部客户机获取内部内容服务器的重定向 URL 缓存命中

    [大型网站技术实践]初级篇:借助Nginx搭建反向代理服务器 - Edison Chou - 博客园http://www.cnblogs.com/edisonchou/p/4126742.html 图 ...

  6. 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 ...

  7. Forward Proxy vs Reverse Proxy

    Overview We've talked about reverse proxy servers and how they can really be good at protecting the ...

  8. 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 ...

  9. Nginx & Reverse Proxy

    Nginx & Reverse Proxy https://docs.nginx.com/nginx/admin-guide/web-server/reverse-proxy/ https:/ ...

  10. 反向代理服务器(Reverse Proxy)

    反向代理服务器(Reverse Proxy)   普通代理服务器是帮助内部网络的计算机访问外部网络.通常,代理服务器同时连接内网和外网.首先内网的计算机需要设置代理服务器地址和端口,然后将HTTP请求 ...

随机推荐

  1. Python——第四章:闭包(Closure)、装饰器(Decorators)

    闭包: 本质, 内层函数对外层函数的局部变量的使用. 此时内层函数被称为闭包函数    1. 可以让一个变量常驻与内存,可随时被外层函数调用.    2. 可以避免全局变量被修改.被污染.更安全.(通 ...

  2. C语言之检验三边能否构成三角形

    要求: 编写程序检验由三边能否构成三角形,检验方法是任意两边和均要大于第三边. 输出格式:若结果可以构成,输出YES,否则输出ERROR DATA 其实很简单,运用与逻辑运算符&&判断 ...

  3. 平衡树——AVL算法

    平衡树--AVL算法 平衡树建立在二叉搜索树的基础上,加入了两侧子树大小相对平衡的特性而避免了很多情况下的算法退化.这里AVL算法实现的AVL树就是平衡树的一种. 1.二叉搜索树 在说平衡树之前我们得 ...

  4. Cesium中用到的图形技术——Horizon Culling

    译者注:本文翻译自Cesium官方博文<Horizon Culling>,by KEVIN RING. 目录 地平线针对球体剔除一个点 平面测试 圆锥测试 推广到椭球 代码 预览 在开发像 ...

  5. three.js中帧缓存的使用

    目录 1. 概述 2. 示例 2.1. 代码 2.2. 解析 3. 参考 1. 概述 在网上查阅了一下three.js关于帧缓存的使用,感觉很多都是关于three.js中后处理通道的使用的.后处理通道 ...

  6. 云图说|“真人?机器?傻傻分不清!” WAF Bot管理,带你慧眼辨“精”!

    黑产SEO,恶意竞争,交易欺诈--企业业务频受恶意Bot流量困扰? 华为云Web应用防火墙Bot管理功能,帮助您轻松甄别并拦截恶意Bot流量,规避恶意 Bot 行为带来的业务风险. 企业主机安全(Ho ...

  7. 分布式缓存服务DCS:企业版性能更强,稳定性更高

    摘要:企业版性能指标达到业界TOP1,行业领先30%,内核态实现真正多线程. 一.背景介绍 近年来,随着各行业业务需求急速增加,数据量和并发访问量呈指数级增长,原来只能依附于关系型数据库的传统&quo ...

  8. 一文详解TensorFlow模型迁移及模型训练实操步骤

    摘要:本文介绍将TensorFlow网络模型迁移到昇腾AI平台,并执行训练的全流程.然后以TensorFlow 1.15训练脚本为例,详细介绍了自动迁移.手工迁移以及模型训练的操作步骤. 本文分享自华 ...

  9. 常见的6种MySQL约束

    摘要:一篇文章带你彻底了解MySQL各种约束 MySQL约束 <1> 概念 是一种限制,它是对表的行和列的数据做出约束,确保表中数据的完整性和唯一性. <2> 使用场景 创建表 ...

  10. 冠军斩获10万奖金!首届“域见杯”医检AI开发者大赛精彩落幕

    摘要:首届"域见杯"医检AI开发者大赛精彩落幕. 8月24日,由广州市科学技术局指导,金域医学和华为云共同打造的中国第三方医检行业首个开发者大赛--"域见杯"医 ...