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. Linux驱动开发笔记(六):用户层与内核层进行数据传递的原理和Demo

    前言   驱动作为桥梁,用户层调用预定义名称的系统函数与系统内核交互,而用户层与系统层不能直接进行数据传递,进行本篇主要就是理解清楚驱动如何让用户编程来实现与内核的数据交互传递.   温故知新 设备节 ...

  2. [VMware]ESXI下硬盘的两种直通方式

    文章来自:https://rmbz.net/archives/vmware-esxi-passthrough 最近再搞ESXI,把原来的"黑群晖"改成ESXI:因为群晖里有数据,为 ...

  3. Redis的五大数据类型(简单使用)

    1:Redis的简单介绍? Redis(Remote Dictionary Server ):即远程字典服务 是一个开源的使用ANSI C语言编写.支持网络.可基于内存亦可持久化的日志型Key-Val ...

  4. .NET开源、强大的Web报表统计系统

    前言 今天分享一个.NET开源.强大的Web报表统计系统:CellReport. 项目官方介绍 CellReport 诞生的初衷是为了解决日常快速制作统计报表的需要. CellReport 是一个为复 ...

  5. 在Mac上打开Emoji键盘 ⌨️

    因为想要输入Emoji字符,以前总上到网上搜,如 emojipedia.org ,然后复制.最近,输入Emoji有点多,就想查查有没有什么简便方法. 果然有,我用的是macOS,只要按下键盘上的 co ...

  6. 这是你没见过的MindSpore 2.0.0 for Windows GPU版

    摘要:一文带你看看MindSpore 2.0.0 for Windows GPU版. 本文分享自华为云社区<MindSpore 2.0.0 for Windows GPU泄漏版尝鲜>,作者 ...

  7. AI专家一席谈:复用算法、模型、案例,AI Gallery带你快速上手应用开发

    摘要: 华为云社区邀请到了AI Gallery的负责人严博,听他谈一谈AI Gallery的设计初衷.经典案例以及未来规划. 本文分享自华为云社区<AI专家一席谈:复用算法.模型.案例,AI G ...

  8. 1024程序员节献礼,火山引擎ByteHouse带来三重产品福利

    更多技术交流.求职机会,欢迎关注字节跳动数据平台微信公众号,回复[1]进入官方交流.   随着信息技术飞速发展,互联网.Web3.物联网.人工智能相继出现. 在这近三十年的高速发展中,"程序 ...

  9. 电商运营该如何做 AB 测试

    更多技术交流.求职机会,欢迎关注字节跳动数据平台微信公众号,回复[1]进入官方交流群 近年,电商行业进入了一个新的发展阶段,一方面电商市场规模持续扩大,另一方面直播电商.即时零售.社区团购等新兴电商业 ...

  10. 火山引擎 DataTester:抖音的设计团队是如何用 A/B 测试实现高效优化的?

    更多技术交流.求职机会,欢迎关注字节跳动数据平台微信公众号,回复[1]进入官方交流群 对 C 端产品而言,产品的每一个细节设置都或多或少影响着用户的产品体验,本文介绍字节跳动的 A/B 实验文化的同时 ...