Running Grafana behind a reverse proxy

It should be straight forward to get Grafana up and running behind a reverse proxy. But here are some things that you might run into.

Links and redirects will not be rendered correctly unless you set the server.domain setting.

  1. [server]
  2. domain = foo.bar

To use sub path ex http://foo.bar/grafana make sure to include /grafana in the end of root_url. Otherwise Grafana will not behave correctly. See example below.

Examples

Here are some example configurations for running Grafana behind a reverse proxy.

Grafana configuration (ex http://foo.bar)

  1. [server]
  2. domain = foo.bar

Nginx configuration

  1. server {
  2. listen 80;
  3. root /usr/share/nginx/www;
  4. index index.html index.htm;
  5. location / {
  6. proxy_pass http://localhost:3000/;
  7. }
  8. }

Examples with sub path (ex http://foo.bar/grafana)

Grafana configuration with sub path

  1. [server]
  2. domain = foo.bar
  3. root_url = %(protocol)s://%(domain)s/grafana/

Nginx configuration with sub path

  1. server {
  2. listen 80;
  3. root /usr/share/nginx/www;
  4. index index.html index.htm;
  5. location /grafana/ {
  6. proxy_pass http://localhost:3000/;
  7. }
  8. }

HAProxy configuration with sub path

  1. frontend http-in
  2. bind *:80
  3. use_backend grafana_backend if { path /grafana } or { path_beg /grafana/ }
  4. backend grafana_backend
  5. # Requires haproxy >= 1.6
  6. http-request set-path %[path,regsub(^/grafana/?,/)]
  7. # Works for haproxy < 1.6
  8. # reqrep ^([^\ ]*\ /)grafana[/]?(.*) \1\2
  9. server grafana localhost:3000

IIS URL Rewrite Rule (Windows) with Subpath

IIS requires that the URL Rewrite module is installed.

Given:

  1. [server]
  2. domain = localhost:8080
  3. root_url = %(protocol)s://%(domain)s/grafana/

Create an Inbound Rule for the parent website (localhost:8080 in this example) 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 checkboxThis is the rewrite rule that is generated in the web.config:
  1. <rewrite>
  2. <rules>
  3. <rule name="Grafana" enabled="true" stopProcessing="true">
  4. <match url="grafana(/)?(.*)" />
  5. <action type="Rewrite" url="http://localhost:3000/{R:2}" logRewrittenUrl="false" />
  6. </rule>
  7. </rules>
  8. </rewrite>

See the tutorial on IIS Url Rewrites for more in-depth instructions.