Grafana and nginx - easy solution

There are lots of posts in the forum about difficulties displaying grafana charts in sitemaps, when openHAB is being run through a nginx reverse proxy. A number of possible solutions are discussed but, unless I am missing something, none were very easy or straightforward.

This seems to be the easiest way.

  1. add an nginx location block to catch public urls starting ‘grafana’. The below example shows my standard openahb location block followed by the new grafana block:
location / {
		proxy_pass                            http://localhost:8080/;
		proxy_set_header Host                 $http_host;
		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    "https";
		auth_basic                            "Unauthorised access prohibited";
		auth_basic_user_file                  /etc/nginx/.htpasswd;  

	}

 	location /grafana/ {
                proxy_pass                            http://localhost:3001/;
                proxy_set_header Host                 $http_host;
                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    "https";
                auth_basic                            "Unauthorised access prohibited";
				auth_basic_user_file                  /etc/nginx/.htpasswd;  
        }

I have openHAB running on port 8080 and grafana on port 3001, both on the same PC as nginx.

  1. Then when grafana generates a url like this:
http://localhost:3001/render/d-solo/XTnqdJOmz/climate-control?panelId=15&orgId=1&from=now-31d&to=now&width=380&height=190

I can display it in the sitemap as simply as:

Webview height=6 url="/grafana/render/d-solo/XTnqdJOmz/climate-control?panelId=15&orgId=1&from=now-31d&to=now&width=380&height=190"

Important that in the grafana.ini file, root_url is not set (despite the instructions) and anonymous access is permitted.

10 Likes

Hi

your ideas seem to be awesome.

i want to access remote to the front tail log , to nodered and to grafana

i have added the section below to the the file default under /etc/nginx/sites-enabled

server {
	listen                          80;
	server_name                     mydomain.ddns.net;
	location / {
		proxy_pass                            http://localhost:8080/;
		proxy_buffering                       off;
		proxy_set_header Host                 $http_host;
		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;
                auth_basic                            "Username and Password Required";
		auth_basic_user_file                  /etc/nginx/.htpasswd;
                
	}


 	location /grafana/ {
                proxy_pass                            http://localhost:3000/;
                proxy_set_header Host                 $http_host;
                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    "https";
                auth_basic                            "Unauthorised access prohibited";
		auth_basic_user_file                  /etc/nginx/.htpasswd;  
        }
		
	location /openhablog/ {
                proxy_pass                            http://localhost:9001/;
                proxy_set_header Host                 $http_host;
                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    "https";
                auth_basic                            "Unauthorised access prohibited";
		auth_basic_user_file                  /etc/nginx/.htpasswd;  
        }
		
	location /rednode/ {
                proxy_pass                            http://localhost:1880/;
                proxy_set_header Host                 $http_host;
                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    "https";
                auth_basic                            "Unauthorised access prohibited";
		auth_basic_user_file                  /etc/nginx/.htpasswd;  
        }
		
}

i have updated my sitemap :

Webview height=6 url="/rednode"
Webview height=6 url="/openhablog"
Webview height=6 url="/grafana"```

BUT IT DO NOT WORK

is your url “/grafana” or is it the full local grafana url with “/grafana/” at the start, as in my example?

like in your example

sorry, I’m at a loss for what the problem might be - I’m a beginner with this stuff myself…