0
votes

I have followed the instructions provided by Grafana (https://grafana.com/tutorials/run-grafana-behind-a-proxy/#1), but continue to not be successful. Below is my nginx config file. The problem is when I go to http://www.example.com/grafana it redirects me to www.example.com/login which returns 404. Thank you for any assistance!

NGINX Configuration File

server {
    listen       80;
    listen  [::]:80;
    server_name  localhost;

    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }

    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

    # Portainer
    location /portainer/ {
      proxy_http_version 1.1;
      proxy_set_header Connection "";
      proxy_pass http://www.example.com:9000/;
    }
    location /portainer/ws/ {
      proxy_set_header Upgrade $http_upgrade;
      proxy_set_header Connection "upgrade";
      proxy_http_version 1.1;
      proxy_pass http://www.example.com:9000/ws/;
    }

    # Grafana
    location /grafana/ {
        proxy_pass   http://www.example.com:3000/;
    }
}

Grafana Configuration File

#################################### 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 = example.com

# 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/grafana/

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

# Log web requests
;router_logging = false

# the path relative working path
;static_root_path = public

# enable gzip
;enable_gzip = false

# https certs  key file
;cert_file =
;cert_key =

# Unix socket path
;socket =
3
It seems the issue is on the grafana side. Check grafana's access log and output of "curl -v http : // www.example.com:3000/"Alex C
Sorry I tried to figure out how to access the "access logs" but couldn't find anything. In Portainer it outputs this when I try and access grafana. t=2020-10-11T19:50:38+0000 lvl=info msg="Request Completed" logger=context userId=0 orgId=0 uname= method=GET path=/grafana/ status=302 remote_addr=172.17.0.1 time_ms=0 size=29 referer=Matt L
Are you able to curl the server on port 3000? If so, grafana is running, and we can focus on just nginx issues. To test, try running: curl -sS localhost:3000/login This should return a large block of text (which would normally render the login screen in a browser) – user2863294 11 mins agouser2863294

3 Answers

0
votes

Looks like in your Grafana configuration file the root_url and serve_from_sub_path are in comment, just try to remove the ; and reload the NGINX

-1
votes

I think you must add "grafana" to the proxy_pass url:

# Grafana
location /grafana/ {
    proxy_pass   http://www.example.com:3000/grafana/;
}
-1
votes

If grafana is running behing a reverse proxy on a subpath, in grafana config file the root url needs to be updated accordingly.

See the Relevant documenttion

This is the full URL used to access Grafana from a web browser. This is important if you use Google or GitHub OAuth authentication (for the callback URL to be correct).

Note: This setting is also important if you have a reverse proxy in front of Grafana that exposes it through a subpath. In that case add the subpath to the end of this URL setting.