Nginx reverse proxy with SSL leads to failed to load application files

Hi everyone, I’ve been trying to set up Grafana 6.3.5 to run on my server in a different domain using Nginx but I just can’t get it to work. I’ve spent so many hours trying to get this working it’s starting to drive me nuts.

I’ve installed Grafana and it works by default from the servers IP address and domain at example.com:3000

but I want to run it from a different domain on the same server at
grafana.foo.com

This server has multiple Nginx configurations running multiple different domains, so I assume this shouldn’t be a problem since Nginx handles the traffic anyway and it works with other setups running python the same way.

Nginx

server {
        listen 443 ssl http2;
        listen [::]:443 ssl http2;

        server_name grafana.foo.com;
        root /var/www/grafana.foo.com/public;

        # SSL
        ssl_certificate /etc/letsencrypt/live/grafana.foo.com/fullchain.pem;
        ssl_certificate_key /etc/letsencrypt/live/grafana.foo.com/privkey.pem;
        ssl_trusted_certificate /etc/letsencrypt/live/grafana.foo.com/chain.pem;

        # security
#       include nginxconfig.io/security.conf;

        # logging
        error_log /var/log/nginx/grafana.foo.com.error.log warn;

        # reverse proxy
        location / {
                proxy_pass http://localhost:3000;
        }

        # additional config
        include nginxconfig.io/general.conf;
}

# HTTP redirect
server {
        listen 80;
        listen [::]:80;

        server_name grafana.foo.com;

        include nginxconfig.io/letsencrypt.conf;

        location / {
                return 301 https://grafana.foo.com$request_uri;
        }
}

note: I’ve disabled the security to see if it has any effect on results. Nginx base config is Generated using nginxconfig.io

grafana.ini

[server]
# Protocol (http, https, 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 = grafana.foo.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 = http://localhost:3000

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

Based on the tutorial I’ve only changed the domain and it should work but it results in

**If you're seeing this Grafana has failed to load its application files**  

1. This could be caused by your reverse proxy settings.

2. If you host grafana under subpath make sure your grafana.ini root_url setting includes subpath

3. If you have a local dev build make sure you build frontend using: yarn start, yarn start:hot, or yarn build

4. Sometimes restarting grafana-server can help

I’ve also tried a ton of other combinations I could find from both Nginx and Grafana’s side but none of them work any better.

for example I’ve tried to change root_url = grafana.foo.com and root_url = grafana.foo.com/grafana/ with sub path enabled. I’ve also tried things like enabling most of the options and using wildcards like %(protocol)s://%(domain)s:%(http_port)s but none of these made any difference.

When checking the status of grafana it also points out that the config file is read from /etc/grafana/grafana.ini so it should be correct.

Any ideas how to get this to work?

You need to set root_url = https://grafana.foo.com/

Yeah I tried that one too, unfortunately it doesn’t work

I have solved the issue, but I’m not sure if it is the correct solution.

The problem was caused by the root path in Nginx settings, I didn’t expect this actually to affect Grafana but act more like a fallback directory if the service is not running.

I changed this
root /var/www/grafana.foo.com/public;
to
root /usr/share/grafana/;

so now it can find the files it’s looking for and works as expected.

1 Like