# Grafana behind reverse proxy (nginx) with subpath

**URL:** <https://community.grafana.com/t/grafana-behind-reverse-proxy-nginx-with-subpath/137703>\
**Category:** Configuration\
**Tags:** reverse-proxy, docker, nginx\
**Created:** [December 4, 2024, 1:20pm UTC](https://community.grafana.com/t/grafana-behind-reverse-proxy-nginx-with-subpath/137703 "2024-12-04T13:20:31Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![twolodzko](https://sea2.discourse-cdn.com/grafana/user_avatar/community.grafana.com/twolodzko/32/50758_2.png) [@twolodzko](https://community.grafana.com/u/twolodzko)\
**Post date:** [December 4, 2024, 1:20pm UTC](https://community.grafana.com/t/grafana-behind-reverse-proxy-nginx-with-subpath/137703/1 "2024-12-04T13:20:31Z")

</div>

- What Grafana version and what operating system are you using?

Latest grafana/grafana image (version 11.3.1)

- **What** are you trying to achieve?

Trying to run the example from the official tutorial with redirecting localpath/grafana to the grafana pod.

- **How** are you trying to achieve it?

I’m using the code from the tutorial.

- What happened?

I’m seeing “If you’re seeing this Grafana has failed to load its application files” or 502 errors, depending on the changes I make in nginx config.

- What did you **expect** to happen?

Access Grafana at [http://localhost:80/grafana](http://localhost:80/grafana)

- Can you copy/paste the configuration(s) that you are having problems with?

nginx.conf:

```auto
# NGINX config based on this tutorial:
# https://grafana.com/tutorials/run-grafana-behind-a-proxy/

# This is required to proxy Grafana Live WebSocket connections.
map $http_upgrade $connection_upgrade {
  default upgrade;
  '' close;
}

server {
  listen 80;
  root /usr/share/nginx/html;
  index index.html index.htm;

  location /grafana {
    proxy_set_header Host $host;
    proxy_pass http://grafana:3000;
    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 $host;
    proxy_pass http://grafana:3000;
    rewrite ^/grafana/(.*) /$1 break;
  }
}

```

docker-compose.yml:

```auto
services:
  grafana:
    image: grafana/grafana
    container_name: grafana
    restart: unless-stopped
    ports:
      - '3000:3000'
    networks:
      - network

  nginx:
    image: nginx:latest
    container_name: nginx
    volumes:
      - ./nginx.conf:/etc/nginx/conf.d/default.conf:ro
      - ./index.html:/usr/share/nginx/html/index.html
    ports:
      - '80:80'
    networks:
      - network

networks:
  network:

```

I also tried it with the grafana.ini config:

```auto
[server]
domain = localhost
root_url = %(protocol)s://%(domain)s:%(http_port)s/grafana/
serve_from_sub_path = true

```

but it does not change anything.

I also tried using

```auto
upstream grafana {
  server localhost:3000;
}

```

and changing `proxy_pass http://grafana:3000;` to `proxy_pass http://grafana;` as in the example (or with `server grafana:3000` above), also with no luck.

- Did you receive any errors in the Grafana UI or in related logs? If so, please tell us **exactly** what they were.

I’m seeing the “If you’re seeing this Grafana has failed to load its application files” page.

- Did you follow any online instructions? If so, what is the URL?

> **[Run Grafana behind a reverse proxy | Grafana Labs](https://grafana.com/tutorials/run-grafana-behind-a-proxy/)**
>
> Learn how to run Grafana behind a reverse proxy

---

<div class="post-metadata">

**Author:** ![jangaraj](https://sea2.discourse-cdn.com/grafana/user_avatar/community.grafana.com/jangaraj/32/39197_2.png) [@jangaraj](https://community.grafana.com/u/jangaraj)\
**Post date:** [December 4, 2024, 1:57pm UTC](https://community.grafana.com/t/grafana-behind-reverse-proxy-nginx-with-subpath/137703/2 "2024-12-04T13:57:33Z")

</div>

Configure Grafana for subpath and then also connect to subpath from nginx

```auto
 proxy_pass http://grafana:3000/grafana/;

```

All gears must be working with that subpath

---

<div class="post-metadata">

**Author:** ![twolodzko](https://sea2.discourse-cdn.com/grafana/user_avatar/community.grafana.com/twolodzko/32/50758_2.png) [@twolodzko](https://community.grafana.com/u/twolodzko)\
**Post date:** [December 4, 2024, 2:14pm UTC](https://community.grafana.com/t/grafana-behind-reverse-proxy-nginx-with-subpath/137703/3 "2024-12-04T14:14:56Z")

</div>

Thanks @jangaraj but changing it does not fix the issue unfortunately. I’m getting the same error page.

---

<div class="post-metadata">

**Author:** ![jangaraj](https://sea2.discourse-cdn.com/grafana/user_avatar/community.grafana.com/jangaraj/32/39197_2.png) [@jangaraj](https://community.grafana.com/u/jangaraj)\
**Post date:** [December 4, 2024, 2:37pm UTC](https://community.grafana.com/t/grafana-behind-reverse-proxy-nginx-with-subpath/137703/4 "2024-12-04T14:37:54Z")

</div>

Provide reproducible example (config) + error (e.g. screenshot, browser console requests, …) - make sure you run CTRL+F5 a few times (reload without caching any resources) in your browser - you you have 100% current config in the browser.

---

<div class="post-metadata">

**Author:** ![twolodzko](https://sea2.discourse-cdn.com/grafana/user_avatar/community.grafana.com/twolodzko/32/50758_2.png) [@twolodzko](https://community.grafana.com/u/twolodzko)\
**Post date:** [December 4, 2024, 2:41pm UTC](https://community.grafana.com/t/grafana-behind-reverse-proxy-nginx-with-subpath/137703/5 "2024-12-04T14:41:19Z")

</div>

I’m seeing this:

 ![image](https://us1.discourse-cdn.com/grafana/original/3X/b/7/b7797391b5dee39fdda76589778cc3fbd66645c9.png)

The reproducible example is provided in the code snippets. I’m re-deploying it using docker compose, so the config is always fresh because it gets re-deployed. The code snippets above are everything needed to reproduce it. The examples just miss index.html which is a dummy static hello world page.

---

<div class="post-metadata">

**Author:** ![jangaraj](https://sea2.discourse-cdn.com/grafana/user_avatar/community.grafana.com/jangaraj/32/39197_2.png) [@jangaraj](https://community.grafana.com/u/jangaraj)\
**Post date:** [December 4, 2024, 2:52pm UTC](https://community.grafana.com/t/grafana-behind-reverse-proxy-nginx-with-subpath/137703/6 "2024-12-04T14:52:59Z")

</div>

> [@twolodzko](#):
>
> I’m re-deploying it using docker compose, so the config is always fresh because it gets re-deployed.

That’s config. But files in the browser are cached and you don’t consider that a problem.  
Your browser loads js/css files, which are expecting grafana on `grafanahost:3000` and then you change config to subpath `grafanahost:3000/subath` and redeploy it with docker-compose. Your browser may still have cached old js/css files `grafanahost:3000`, so cache clearing is IMPORTANT. You will see it in the browser console where files are pointing - if it is pointing to subpath or not

---

<div class="post-metadata">

**Author:** ![twolodzko](https://sea2.discourse-cdn.com/grafana/user_avatar/community.grafana.com/twolodzko/32/50758_2.png) [@twolodzko](https://community.grafana.com/u/twolodzko)\
**Post date:** [December 4, 2024, 2:56pm UTC](https://community.grafana.com/t/grafana-behind-reverse-proxy-nginx-with-subpath/137703/7 "2024-12-04T14:56:25Z")

</div>

I also opened it in the private window after re-deploying, same result.

---

<div class="post-metadata">

**Author:** ![jangaraj](https://sea2.discourse-cdn.com/grafana/user_avatar/community.grafana.com/jangaraj/32/39197_2.png) [@jangaraj](https://community.grafana.com/u/jangaraj)\
**Post date:** [December 4, 2024, 3:01pm UTC](https://community.grafana.com/t/grafana-behind-reverse-proxy-nginx-with-subpath/137703/8 "2024-12-04T15:01:41Z")

</div>

Provide repo, where I can just run docker-compose pls

---

<div class="post-metadata">

**Author:** ![twolodzko](https://sea2.discourse-cdn.com/grafana/user_avatar/community.grafana.com/twolodzko/32/50758_2.png) [@twolodzko](https://community.grafana.com/u/twolodzko)\
**Post date:** [December 5, 2024, 8:22am UTC](https://community.grafana.com/t/grafana-behind-reverse-proxy-nginx-with-subpath/137703/9 "2024-12-05T08:22:18Z")

</div>

Here you are [Grafana with Nginx reverse proxy · GitHub](https://gist.github.com/twolodzko/cbbe037c277443167fd40e12afc19f70)

---

<div class="post-metadata">

**Author:** ![jangaraj](https://sea2.discourse-cdn.com/grafana/user_avatar/community.grafana.com/jangaraj/32/39197_2.png) [@jangaraj](https://community.grafana.com/u/jangaraj)\
**Post date:** [December 5, 2024, 10:35am UTC](https://community.grafana.com/t/grafana-behind-reverse-proxy-nginx-with-subpath/137703/10 "2024-12-05T10:35:42Z")

</div>

I asked for repo. You as a requester should make the easiest way to support you otherwise you will decrease the chance for help. What is the point of community supporters spending an hour just to setup everything and you will pay a minute to write about problem?  
There are no changes, which I suggested. Sorry, provide me a repo with exact setup, which I suggested and which didn’t work for you.

---

<div class="post-metadata">

**Author:** ![twolodzko](https://sea2.discourse-cdn.com/grafana/user_avatar/community.grafana.com/twolodzko/32/50758_2.png) [@twolodzko](https://community.grafana.com/u/twolodzko)\
**Post date:** [December 5, 2024, 10:50am UTC](https://community.grafana.com/t/grafana-behind-reverse-proxy-nginx-with-subpath/137703/11 "2024-12-05T10:50:56Z")

</div>

I moved it into a repo, but this is exactly the same files you can download with one click from gist [GitHub - twolodzko/grafana-sandbox](https://github.com/twolodzko/grafana-sandbox)

---

<div class="post-metadata">

**Author:** ![jangaraj](https://sea2.discourse-cdn.com/grafana/user_avatar/community.grafana.com/jangaraj/32/39197_2.png) [@jangaraj](https://community.grafana.com/u/jangaraj)\
**Post date:** [December 5, 2024, 10:56am UTC](https://community.grafana.com/t/grafana-behind-reverse-proxy-nginx-with-subpath/137703/12 "2024-12-05T10:56:44Z")

</div>

> [@jangaraj](#):
>
> Configure Grafana for subpath and then also connect to subpath from nginx

Yes, but there is no config for Grafana

> Configure Grafana for subpath and then also connect to subpath from nginx

---

<div class="post-metadata">

**Author:** ![twolodzko](https://sea2.discourse-cdn.com/grafana/user_avatar/community.grafana.com/twolodzko/32/50758_2.png) [@twolodzko](https://community.grafana.com/u/twolodzko)\
**Post date:** [December 5, 2024, 11:55am UTC](https://community.grafana.com/t/grafana-behind-reverse-proxy-nginx-with-subpath/137703/13 "2024-12-05T11:55:19Z")

</div>

I provided a simple, reproducible example of the issue. Could you help me with solving it?

---

<div class="post-metadata">

**Author:** ![jangaraj](https://sea2.discourse-cdn.com/grafana/user_avatar/community.grafana.com/jangaraj/32/39197_2.png) [@jangaraj](https://community.grafana.com/u/jangaraj)\
**Post date:** [December 5, 2024, 12:14pm UTC](https://community.grafana.com/t/grafana-behind-reverse-proxy-nginx-with-subpath/137703/14 "2024-12-05T12:14:35Z")

</div>

I gave you before:

> [@jangaraj](#):
>
> Configure Grafana for subpath and then also connect to subpath from nginx
> 
> ```auto
> proxy_pass http://grafana:3000/grafana/;
> 
> ```
> 
> All gears must be working with that subpath

You said that this is not working, so I asked for exactly this reproducible example.  
You provided now repo with vanilla config - there is no config for “Configure Grafana for subpath and then also connect to subpath from nginx”. So from my point of view you didn’t follow my instructions. I’m happy to help if you follow instructions.

---

<div class="post-metadata">

**Author:** ![twolodzko](https://sea2.discourse-cdn.com/grafana/user_avatar/community.grafana.com/twolodzko/32/50758_2.png) [@twolodzko](https://community.grafana.com/u/twolodzko)\
**Post date:** [December 5, 2024, 12:25pm UTC](https://community.grafana.com/t/grafana-behind-reverse-proxy-nginx-with-subpath/137703/15 "2024-12-05T12:25:23Z")

</div>

Could you then tell me what the config should be, or your idea is that I’d be making random commits until I figure out how to fix it myself…?

---

<div class="post-metadata">

**Author:** ![jangaraj](https://sea2.discourse-cdn.com/grafana/user_avatar/community.grafana.com/jangaraj/32/39197_2.png) [@jangaraj](https://community.grafana.com/u/jangaraj)\
**Post date:** [December 5, 2024, 12:48pm UTC](https://community.grafana.com/t/grafana-behind-reverse-proxy-nginx-with-subpath/137703/16 "2024-12-05T12:48:12Z")

</div>

You already said that you configured:

> [@twolodzko](#):
>
> I also tried it with the grafana.ini config:
> 
> ```auto
> [server]
> domain = localhost
> root_url = %(protocol)s://%(domain)s:%(http_port)s/grafana/
> serve_from_sub_path = true
> 
> ```

So add this to the repo. Only you know how you configured it.

---

<div class="post-metadata">

**Author:** ![twolodzko](https://sea2.discourse-cdn.com/grafana/user_avatar/community.grafana.com/twolodzko/32/50758_2.png) [@twolodzko](https://community.grafana.com/u/twolodzko)\
**Post date:** [December 5, 2024, 12:53pm UTC](https://community.grafana.com/t/grafana-behind-reverse-proxy-nginx-with-subpath/137703/17 "2024-12-05T12:53:49Z")

</div>

Ok, I added it to the repo.

---

<div class="post-metadata">

**Author:** ![jangaraj](https://sea2.discourse-cdn.com/grafana/user_avatar/community.grafana.com/jangaraj/32/39197_2.png) [@jangaraj](https://community.grafana.com/u/jangaraj)\
**Post date:** [December 5, 2024, 1:01pm UTC](https://community.grafana.com/t/grafana-behind-reverse-proxy-nginx-with-subpath/137703/18 "2024-12-05T13:01:23Z")

</div>

That’s wrong. You are adding grafana.ini to nginx container. If you did it before, then of course it didn’t work.

---

<div class="post-metadata">

**Author:** ![twolodzko](https://sea2.discourse-cdn.com/grafana/user_avatar/community.grafana.com/twolodzko/32/50758_2.png) [@twolodzko](https://community.grafana.com/u/twolodzko)\
**Post date:** [December 5, 2024, 1:36pm UTC](https://community.grafana.com/t/grafana-behind-reverse-proxy-nginx-with-subpath/137703/19 "2024-12-05T13:36:17Z")

</div>

Ok, thanks, indeed the code in the repo was wrong. But do I follow correctly from

> Configure Grafana for subpath and then also connect to subpath from nginx
> 
> ```auto
> proxy_pass http://grafana:3000/grafana/;
> 
> ```
> 
> All gears must be working with that subpath

that it is not possible to redirect /grafana → [http://grafana:3000](http://grafana:3000) (without the subpath)? Because this is what actually I want to do: to use nginx to redirect from some arbitrary path to Grafana under some address.

---

<div class="post-metadata">

**Author:** ![jangaraj](https://sea2.discourse-cdn.com/grafana/user_avatar/community.grafana.com/jangaraj/32/39197_2.png) [@jangaraj](https://community.grafana.com/u/jangaraj)\
**Post date:** [December 5, 2024, 3:19pm UTC](https://community.grafana.com/t/grafana-behind-reverse-proxy-nginx-with-subpath/137703/20 "2024-12-05T15:19:59Z")

</div>

> [@twolodzko](#):
>
> that it is not possible to redirect /grafana → [http://grafana:3000](http://grafana:3000) (without the subpath)

I guess it’s possible, but then you may need a billion rewrites/content rewrites to fix the redirect.

Your current setup is almost OK. Configure Grafana to the same port as nginx 80. So Grafana generates all redirects in the same config as nginx expect.

[Next page](https://community.grafana.com/t/grafana-behind-reverse-proxy-nginx-with-subpath/137703.md?page=2)
