Reverse proxy

Proxy an app mounted under a subpath

3 min read

You want https://example.com/grafana/ to reach an app running on 127.0.0.1:3000. The whole behaviour hinges on one trailing slash on proxy_pass.

Strip the /grafana/ prefix (the app sees /):

location /grafana/ {
    proxy_pass http://127.0.0.1:3000/;   # <-- trailing slash strips the location prefix

    proxy_set_header Host            $host;
    proxy_set_header X-Real-IP       $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto $scheme;
}

Keep the /grafana/ prefix (the app sees /grafana/…):

location /grafana/ {
    proxy_pass http://127.0.0.1:3000;    # <-- no trailing slash, full URI passed through
    # ...same proxy_set_header lines...
}

The rule:

The real gotcha is the app’s own links. If it generates absolute URLs like /static/app.js, stripping the prefix breaks them. Most apps have a “base path” / “root URL” setting — set it to /grafana/ and use the keep-the-prefix form. Tell the app its public base with the forwarded headers above (many read X-Forwarded-Prefix):

    proxy_set_header X-Forwarded-Prefix /grafana;
sudo nginx -t && sudo nginx -s reload
Open the full version (with copy buttons) ↗

← All snippets