Grafana under a sub path 404 error

Hello,

I followed documentation on how to configure NGINX to serve Grafana under sub-path. But I am facing 404 error.

nginx.conf

<..>

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

    upstream grafana {
        server localhost:3000;
    }

    server {
        <..>

        location /grafana/ {
            proxy_set_header Host $http_host;
            proxy_pass http://grafana;

            rewrite  ^/grafana/(.*)  /$1 break;

            auth_basic "Restricted access";
            auth_basic_user_file /etc/nginx/.htpasswd;
        }
    }
}

docker-compose.yml:

grafana:
  image: grafana/grafana-enterprise
  container_name: grafana
  restart: unless-stopped
  volumes:
    - ./grafana.ini:/etc/grafana/grafana.ini
    - ./datasources:/etc/grafana/provisioning/datasources
  networks:
    - proxyNetwork

grafana.ini:

root_url = %(protocol)s://%(domain)s:%(http_port)s/grafana
serve_from_sub_path = true

Now visiting http://localhost/grafana/ I get 404 error.

In the logs I get: [error] 22#22: *6 connect() failed (111: Connection refused) while connecting to upstream. How to make sure I can enter Grafana only via the provided link. Thanks.

Is nginx running in the container?

Yes, my Nginx running as the frontend container:

frontend:
    image: frontend
    container_name: frontend
    restart: unless-stopped
    ports:
      - "80:80"
      - "443:443"
    depends_on:
      - backend
    volumes:
      - ./nginx.conf:/etc/nginx/nginx.conf
      - ./.htpasswd:/etc/nginx/.htpasswd
    networks:
      - proxyNetwork

Each container has own networking = also own “localhost”, so this is nginx’s localhost (where Grafana is not listening of course, because it is running in different container):

    upstream grafana {
        server localhost:3000;
    }

Point it to Grafana container:

    upstream grafana {
        server grafana:3000;
    }

Read some Docker doc about networking for more details.

1 Like

Yes, thank you, but now do I need this and exactly for what reason?

rewrite  ^/grafana/(.*)  /$1 break;

Because with this when I visit localhost/grafana I get redirected to localhost:3000/grafana and I get 404, since I do not expose the port in nginx config. WIthout this it works, but after i prompted to enter username/pass, it refreshes and asks again for credentials.

Configure root_url in Grafana config - url which is used to reach Nginx and

    upstream grafana {
        server grafana:3000/grafana/;
    }

Hi, as I understood I had to change grafana root_url to

root_url = %(protocol)s://%(domain)s:%(http_port)s

since this is where nginx is reached at or am I misunderstanding your comment? I get 404.

In logs I get “nginx: [emerg] invalid host in upstream "grafana:3000/grafana/" in /etc/nginx/nginx.conf:23” which points to:

 server grafana:3000/grafana/;

Maybe you’ve got a working example somewhere?

I tried different ways via :%(http_port)s and :%(http_port)s/grafana, also doing as previously, but I get same error or it’s 404.

Update:

nginx.conf

upstream grafana {
    server grafana:3000;
}

server {
location /grafana/ {
    proxy_set_header Host $http_host;
    proxy_pass http://grafana;

    rewrite  ^/grafana/(.*)  /$1 break;

    # auth_basic "Restricted access";
    # auth_basic_user_file /etc/nginx/.htpasswd;
}

grafana.ini

root_url = %(protocol)s://%(domain)s/grafana

Uncommenting auth_basic asks for password, entering correct one refreshes the page and asks again - as it was discussed before.

I have the same problem with auth_basic. If you solve it, please let me know how.