docs/sources/tutorials/run-grafana-behind-a-proxy/index.md
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.
server.domain to the domain name you'll be using:[server]
domain = example.com
nginx is a high performance load balancer, web server, and reverse proxy.
http section, 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 $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 $host;
proxy_pass http://grafana;
}
}
For Grafana Live which uses WebSocket connections you may have to raise the nginx value for worker_connections option which is 512 by default. The default value limits the number of possible concurrent connections with Grafana Live.
Also, be aware that the preceding configuration only works when the proxy_pass value for location / is a literal string.
If you want to use a variable here, you must instead use a rewrite rule.
For more information, refer to the GitHub issue #18299.
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 $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 $host;
proxy_pass http://grafana;
}
}
Add a rewrite rule to each location block:
rewrite ^/grafana/(.*) /$1 break;
{{< admonition type="note" >}}
If nginx is performing TLS termination, then you must set the root_url and protocol configuration accordingly.
If you're serving Grafana from https://example.com/grafana/ then the root_url is https://example.com/grafana/ or https://%(domain)s/grafana/ with the corresponding domain configuration value set to example.com in the server section of the Grafana configuration file.
Set protocol to http.
{{< /admonition >}}
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
server grafana localhost:3000
# Requires haproxy >= 1.6
http-request set-path %[path,regsub(^/grafana/?,/)]
# Works for haproxy < 1.6
# reqrep ^([^\ ]*\ /)grafana[/]?(.*) \1\2
server grafana localhost:3000
{{< admonition type="note" >}} To use IIS, you must have the URL Rewrite module installed. {{< /admonition >}}
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:
grafana(/)?(.*)Ignore case checkboxhttp://localhost:3000/{R:2}Append query string checkboxStop processing of subsequent rules checkboxThis is the rewrite rule that's 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>
For more detailed instruction, refer to the tutorial on IIS URL Rewrites.
To use Apache as a proxy, ensure its proper installation and configuration.
mod_proxy is installed and enabled. To enable, run the following commands:a2enmod proxy
a2enmod proxy_http
<VirtualHost> section, add the following code: ProxyPreserveHost on
ProxyPass / http://your_grafana_server:3000
ProxyPassReverse / http://your_grafana_server:3000
After you've restarted, navigate to your Apache server on port 80 and you will be redirected to Grafana.
To configure Grafana hosted in a sub path, replace the sub path with the following code (assuming your Grafana instance is on the sub path your_path):
ProxyPreserveHost on
ProxyPass /your_path http://your_grafana_server:3000
ProxyPassReverse /your_path http://your_grafana_server:3000
ProxyPass / http://your_grafana_server:3000/your_path
ProxyPassReverse / http://192.168.250.5:3000/your_path
Note that the lines containing your_path must come before the lines referencing root path (/) in order for this to work correctly.
Traefik Cloud Native application proxy.
Using the Docker provider and the following labels configures 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
{{< admonition type="note" >}} You only need this if you don't handle the sub path serving via your reverse proxy configuration. {{< /admonition >}}
If you don't want or can't use the reverse proxy to handle serving Grafana from a sub path, you can set the configuration variable serve_from_sub_path to true.
root_url.serve_from_sub_path to true:[server]
domain = example.com
root_url = %(protocol)s://%(domain)s:%(http_port)s/grafana/
serve_from_sub_path = true