title: Nginx reverse proxy for grafana using dashboard as subpath
body: <p>I would like to use <code>dashboard</code> as my nginx location for my grafana install.</p>
  
  <p>The problems is grafana uses dashboard in some of it url's like <code>https://example.com/grafana/dashboard/new?orgId=1</code>, where I would like it to be <code>https://example.com/dashboard/dashboard/new?orgId=1</code> and I think my nginx location is rewriting to <code>https://example.com/dashboard/new?orgId=1</code>.</p>
  
  <p>When I have it setup to use <code>grafana</code> as the subpath it all work as expected;</p>
  
  <p>grafana.ini:</p>
  
  <pre><code>[server]
  http_addr = 127.0.0.1
  domain = example.com
  root_url = %(protocol)s://%(domain)s/grafana/
  </code></pre>
  
  <p>nginx config:</p>
  
  <pre><code># Upstream Servers
  upstream grafana_server {
      server localhost:3000;
  }
  
  server {
      listen 80;
      listen [::]:80;
  
      server_name example.com www.example.com;
  
      return 301 https://$host$request_uri;
  }
  
  server {
      listen 443 ssl default_server;
      listen [::]:443 ssl default_server;
  
      include snippets/ssl-example.com.conf;
      include snippets/ssl-params.conf;
  
      root /var/www/example.com/html;
  
      index index.html index.htm;
  
      server_name example.com www.example.com;
  
      location /grafana/ {
          proxy_pass http://grafana_server/;
          proxy_set_header Host $host;
      }
  }
  </code></pre>
  
  <p>But changing it to <code>dashboard</code> and navigating to <code>https://example.com/dashboard/dashboard/new?orgId=1</code> results in the url been rewritten to <code>https://example.com/dashboard/new?orgId=1</code></p>
  
  <p>grafana.ini:</p>
  
  <pre><code>[server]
  http_addr = 127.0.0.1
  domain = example.com
  root_url = %(protocol)s://%(domain)s/dashboard/
  </code></pre>
  
  <p>nginx config:</p>
  
  <pre><code># Upstream Servers
  upstream grafana_server {
      server localhost:3000;
  }
  
  server {
      listen 80;
      listen [::]:80;
  
      server_name example.com www.example.com;
  
      return 301 https://$host$request_uri;
  }
  
  server {
      listen 443 ssl default_server;
      listen [::]:443 ssl default_server;
  
      include snippets/ssl-example.com.conf;
      include snippets/ssl-params.conf;
  
      root /var/www/example.com/html;
  
      index index.html index.htm;
  
      server_name example.com www.example.com;
  
      location /dashboard/ {
          proxy_pass http://grafana_server/;
          proxy_set_header Host $host;
      }
  }
  </code></pre>
  
  <p>so I have tried a to do a rewrite in the nginx location but can't get it to work as required (really have no clue what to do here)</p>
  
  <pre><code>location ~ (\/dashboard\/) {
      proxy_pass http://grafana_server$1;
      proxy_set_header Host $host;
  }
  
  location ~ /dashboard/ {
      rewrite ^ /dashboard/$1;
      proxy_pass http://grafana_server;
      proxy_set_header Host $host;
  }
  </code></pre>
  
  <p>Any help would be much appreciated.</p>
  
  <p>Regards,</p>