2016-10-21 04:01:34 -05:00
+++
title = "Running Grafana behind a reverse proxy"
description = "Guide for running Grafana behind a reverse proxy"
keywords = ["grafana", "nginx", "documentation", "haproxy", "reverse"]
type = "docs"
[menu.docs]
name = "Running Grafana behind a reverse proxy"
parent = "tutorials"
weight = 1
+++
2016-09-12 07:47:02 -05:00
# Running Grafana behind a reverse proxy
2016-10-21 04:01:34 -05:00
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.
2016-09-12 07:47:02 -05:00
2016-10-21 04:01:34 -05:00
Links and redirects will not be rendered correctly unless you set the server.domain setting.
2017-10-05 12:01:03 -05:00
```bash
2016-09-12 07:47:02 -05:00
[server]
domain = foo.bar
```
2016-10-21 04:01:34 -05:00
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.
2016-09-12 07:47:02 -05:00
2017-02-20 05:11:22 -06:00
## Examples
2016-09-12 07:47:02 -05:00
Here are some example configurations for running Grafana behind a reverse proxy.
2018-07-02 11:01:42 -05:00
### Grafana configuration (ex http://foo.bar)
2017-02-20 05:11:22 -06:00
2017-10-05 12:01:03 -05:00
```bash
2016-09-12 07:47:02 -05:00
[server]
domain = foo.bar
```
2017-02-20 05:11:22 -06:00
### Nginx configuration
2019-08-06 02:47:08 -05:00
Nginx is a high performance load balancer, web server and reverse proxy: https://www.nginx.com/
2020-02-11 10:55:02 -06:00
#### Nginx configuration with HTTP and Reverse Proxy enabled
2017-10-05 12:01:03 -05:00
```bash
2016-09-12 07:47:02 -05:00
server {
listen 80;
2020-02-11 10:55:02 -06:00
root /usr/share/nginx/html;
index index.html index.htm;
location / {
proxy_pass http://localhost:3000/;
}
}
```
### Grafana configuration with hosting HTTPS in Nginx (ex https://foo.bar)
```bash
[server]
domain = foo.bar
root_url = https://foo.bar
```
#### Nginx configuration with HTTPS, Reverse Proxy, HTTP to HTTPS redirect and URL re-writes enabled
Instead of http://foo.bar:3000/?orgId=1, this configuration will redirect all HTTP requests to HTTPS and re-write the URL so that port 3000 isn't visible and will result in https://foo.bar/?orgId=1
```bash
server {
listen 80;
server_name foo.bar;
return 301 https://foo.bar$request_uri;
}
server {
listen 443 ssl http2;
server_name foo.bar;
root /usr/share/nginx/html;
2016-09-12 07:47:02 -05:00
index index.html index.htm;
2020-02-11 10:55:02 -06:00
ssl_certificate /etc/nginx/certs/foo_bar.crt;
ssl_certificate_key /etc/nginx/certs/foo_bar_decrypted.key;
ssl_protocols TLSv1.2;
ssl_ciphers HIGH:!aNULL:!MD5;
2016-09-12 07:47:02 -05:00
location / {
2020-02-11 10:55:02 -06:00
rewrite /(.*) /$1 break;
2016-09-12 07:47:02 -05:00
proxy_pass http://localhost:3000/;
2020-02-11 10:55:02 -06:00
proxy_redirect off;
proxy_set_header Host $host;
2016-09-12 07:47:02 -05:00
}
}
```
2018-07-02 11:01:42 -05:00
### Examples with **sub path** (ex http://foo.bar/grafana)
2016-09-12 07:47:02 -05:00
2017-02-20 05:11:22 -06:00
#### Grafana configuration with sub path
2017-10-05 12:01:03 -05:00
```bash
2016-09-12 07:47:02 -05:00
[server]
domain = foo.bar
2018-05-07 07:18:21 -05:00
root_url = %(protocol)s://%(domain)s/grafana/
2016-09-12 07:47:02 -05:00
```
2017-02-20 05:11:22 -06:00
#### Nginx configuration with sub path
2017-10-05 12:01:03 -05:00
```bash
2016-09-12 07:47:02 -05:00
server {
listen 80;
root /usr/share/nginx/www;
index index.html index.htm;
location /grafana/ {
proxy_pass http://localhost:3000/;
}
}
```
2018-01-23 10:42:53 -06:00
#### HAProxy configuration with sub path
```bash
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
```
2018-01-12 02:41:36 -06:00
### IIS URL Rewrite Rule (Windows) with Subpath
IIS requires that the URL Rewrite module is installed.
Given:
- subpath `grafana`
- Grafana installed on `http://localhost:3000`
- server config:
2018-01-16 02:49:12 -06:00
2018-01-12 02:41:36 -06:00
```bash
[server]
domain = localhost:8080
2018-05-07 07:18:21 -05:00
root_url = %(protocol)s://%(domain)s/grafana/
2018-01-12 02:41:36 -06:00
```
2018-01-16 02:49:12 -06:00
Create an Inbound Rule for the parent website (localhost:8080 in this example) in IIS Manager with the following settings:
2018-01-12 02:41:36 -06:00
- pattern: `grafana(/)?(.*)`
- check the `Ignore case` checkbox
2020-02-14 10:11:08 -06:00
- rewrite URL set to `http://localhost:3000/{R:2}`
2018-01-12 02:41:36 -06:00
- check the `Append query string` checkbox
- check the `Stop processing of subsequent rules` checkbox
2018-01-16 02:49:12 -06:00
This is the rewrite rule that is generated in the `web.config` :
2018-01-12 02:41:36 -06:00
```xml
< 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 >
```
2018-01-16 02:49:12 -06:00
2020-02-14 10:11:08 -06:00
See the [tutorial on IIS URL Rewrites ](http://docs.grafana.org/tutorials/iis/ ) for more in-depth instructions.