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——第五章:Traceback模块

    traceback 模块提供了在程序中处理和分析异常时的工具,帮助开发人员更好地理解程序出现问题的原因. 使用 traceback.format_exc() 函数可以获取当前异常的堆栈信息.可以把错误 ...

  2. 技本功|Hive优化之监控(三)

    Hive是大数据领域常用的组件之一,主要是大数据离线数仓的运算,关于Hive的性能调优在日常工作和面试中是经常涉及的一个点,因此掌握一些Hive调优是必不可少的技能.影响Hive效率的主要有数据倾斜. ...

  3. Kotlin委托属性(1)

    在Kotlin中,委托属性(Delegated Properties)是一种强大的语言特性,允许你将属性的 getter 和 setter 方法的实现委托给其他对象.这使得你能够通过委托来重用代码.将 ...

  4. K8s和声明式编程

    转载:原文链接 认识k8s之后,他的操作模式对我来说是一种很不错的体验.他提供了更接近现实世界的面向对象接口. 什么是k8s? Kubernetes(K8s)是一种开源容器编排平台,用于自动化部署.扩 ...

  5. pwd详解

    linux下pwd 命令详解 Linux中用 pwd 命令来查看"当前工作目录"的完整路径. 简单得说,每当你在终端进行操作时,你都会有一个当前工作目录. 在不太确定当前位置时,就 ...

  6. libGDX游戏开发之文理打包(八)

    libGDX游戏开发之文理打包(八) libGDX系列,游戏开发有unity3D巴拉巴拉的,为啥还用java开发?因为我是Java程序员emm-国内用libgdx比较少,多数情况需要去官网和googl ...

  7. LFS - Linux From Scratch 从零开始全记录 - 1 准备篇

    上次折腾 LFS 已经是好几年前了,只记得最后用 VMware 引导成功了,好多技术细节已经不记得了.趁着最近有空,再来折腾一次.这一次用的一台新的 Win10 PC,目标是在一个 U 盘上构建 LF ...

  8. [Python急救站]定时关机程序

    收到朋友的请求,让我帮他做一个电脑关机程序,其实非常简单.代码如下: import tkinter as tk # 导入tkinter模块 from tkinter import ttk # 导入tt ...

  9. 记一次uboot编译/经验/教训

    记一次uboot编译/经验/教训 - 最近学习uboot移植有关的知识,在了解原理后自己编译uboot,但是出现了好多各式各样的错误,最后换了几次系统环境之后才找到错误来源 1.准备源码与交叉编译工具 ...

  10. AutomaticKeepAliveClientMixin 缓存PageView页面

    一旦页面滑出屏幕它就会被销毁 ,实际项目开发中对页面进行缓存是很常见的一个需求,下面我们就看看如何使用AutomaticKeepAliveClientMixin 缓存页面. 注意:使用时一定要注意是否 ...