Memo pour Grafana + Nginx + Reverse Proxy

mars 10, 2024

Sur mon serveur qui fait tourner Weewx , j’ai installé divers services dont Influxdb et Grafana

J’ai voulu modifier la façon dont j’attaquais mon Grafana :
au lieu d’utiliser https://mon_nom_de_domaine/ je voulais utiliser https://mon_nom_de_domaine/grafana/

Mise en oeuvre :

Modification de Grafana :

dans la partie [server]
 

domain = mon_nom_de_domaine
root_url = https://mon_nom_de_domaine/grafana/
server_from_sub_path = true

Exemple :

#################################### Server ####################################
[server]
# Protocol (http, https, h2, socket)
;protocol = http

# The ip address to bind to, empty will bind to all interfaces
;http_addr =
# The http port  to use
;http_port = 3000

# The public facing domain name used to access grafana from a browser
;domain = localhost
domain = mon_nom_de_domaine

# Redirect to correct domain if host header does not match domain
# Prevents DNS rebinding attacks
;enforce_domain = false

# The full public facing url you use in browser, used for redirects and emails
# If you use reverse proxy and sub path specify full url (with sub path)
;root_url = %(protocol)s://%(domain)s:%(http_port)s/
root_url =  https://mon_nom_de_domaine/grafana/

# Serve Grafana from subpath specified in `root_url` setting. By default it is set to `false` for compatibility reasons.
;serve_from_sub_path = false
server_from_sub_path =  true

Modification du fichier de config de Nginx

map $http_upgrade $connection_upgrade {
  default upgrade;
  '' close;
}

# this is upstream grafana. You can use dns name
upstream grafana {
  server localhost:3000;
}

Dans la partie server :


       location /grafana/ {
                proxy_set_header Host $http_host;
                proxy_pass http://grafana/ ;
                #proxy_set_header X-Real-IP $remote_addr;
                #proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                #proxy_set_header X-Forwarded-Proto $scheme;
                rewrite  ^/grafana/(.*)  /$1 break;
        }


         # 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/ ;
             rewrite  ^/grafana/(.*)  /$1 break;
        }


Toutes les infos (en anglai) trouvées ici : https://grafana.com/tutorials/run-grafana-behind-a-proxy/