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. Windows Server 2016 Standard RemoteApp应用发布配置举例

    RemoteApp 应用发布介绍 RemoteApp 是微软在Windows Server 2008 之后,在其系统中集成的一项服务功能,用户可以通过远程桌面访问远端服务器的桌面与程序,客户端本机在无 ...

  2. URL编码揭秘:为什么要进行URL编码?

    URL(Uniform Resource Locator,统一资源定位符)是互联网上资源地址的唯一标识符.在网络请求和数据传输过程中,URL编码起着至关重要的作用. URL编码解码 | 一个覆盖广泛主 ...

  3. 文心一言 VS 讯飞星火 VS chatgpt (47)-- 算法导论6.1 5题

    五.一个已排好序的数组是一个最小堆吗? 文心一言: 一个已排好序的数组不一定是一个最小堆.最小堆是一种特殊的二叉树,它满足以下性质:对于任意节点 x,其父节点 y 的值都小于等于 x 的值.而一个已排 ...

  4. 神经网络入门篇:深度学习和大脑的关联性(What does this have to do with the brain?)

    深度学习和大脑的关联性 开始讲故事(手动狗头) 深度学习和大脑有什么关联性吗? 关联不大. 那么为什么会说深度学习和大脑相关呢? 当你在实现一个神经网络的时候,那些公式是你在做的东西,你会做前向传播. ...

  5. 多模态AI开发套件HiLens Kit:超强算力彰显云上实力

    摘要:Huawei HiLens Kit是一款端云协同多模态AI开发套件,支持图像.视频.语音等多种数据分析与推理计算,可广泛用于智能监控.智能家庭.机器人.无人机.智慧工业.智慧门店等分析场景. 在 ...

  6. 保护客户代码和应用安全,CodeArts有7招

    摘要:华为CodeArts致力于各种措施与方案,确保用户研发资产的安全. 华为云有IAM统一认证.CodeArts原名"DevCloud"上每个项目均设有权限管理机制. CodeA ...

  7. SQL Server 还原 备份集中的数据库备份与现有的 'XXX' 数据库不同

    1. 介质集有 2 个介质簇,但只提供了 1 个.必须提供所有成员. 因为在备份时有两个路径,所以备份文件会放在两个文件里面,所以在还原的时候只要把两个都添加了就可以还原了. 2. 备份集中的数据库备 ...

  8. Nacos 1.2.1 集群搭建(三) Nginx 配置 集群

    配置 Nginx 可以把.conf 文件拉到本地,配置好再传上去 #gzip on; upstream cluster{ server 192.168.0.113:8848; server 192.1 ...

  9. django基本流程

    创建项目 django-admin startproject web cd web python manage.py startapp weblist 生成迁移文件 python manage.py ...

  10. [NOI2015]荷马史诗 - Huffman树

    题目描述 追逐影子的人,自己就是影子. --荷马 llison 最近迷上了文学.她喜欢在一个慵懒的午后,细细地品上一杯卡布奇诺,静静地阅读她爱不释手的<荷马史诗>.但是由<奥德赛&g ...