ProjectsPhotography AboutContact ResumeHome

Note · LoadLab · 24 June 2026

Fixing a Grafana redirect loop behind an Nginx sub-path

Grafana kept redirecting to itself. The cause was one trailing path in proxy_pass.

The setup

LoadLab runs on one EC2 instance with Docker Compose. Nginx is the only container with a published port: it serves the dashboard page at /, proxies the Go backend at /api/, and puts Grafana and Prometheus under /grafana/ and /prometheus/. That keeps the security group down to port 80.

Grafana was told it lives under that sub-path:

GF_SERVER_ROOT_URL=http://<ec2-ip>/grafana/
GF_SERVER_SERVE_FROM_SUB_PATH=true

The symptom

Opening /grafana/ never reached a dashboard. The browser bounced between redirects until it gave up.

The cause

The Nginx block looked like the usual reverse-proxy example:

location /grafana/ {
    proxy_pass http://grafana:3000/;
}

When proxy_pass has a path after the host (here, the trailing /), Nginx replaces the part of the request that matched the location with that path. A request for /grafana/ reached Grafana as /.

But SERVE_FROM_SUB_PATH tells Grafana that every request will arrive with /grafana/ still on it. Seeing a bare /, it redirected the browser to /grafana/. Nginx stripped the prefix again, Grafana redirected again, and the loop never ended. Each side was doing the job of removing the prefix, so neither ever saw it.

The fix

Pass the prefix through untouched, so the app that knows its sub-path is the one that handles it:

location /grafana/ {
-   proxy_pass http://grafana:3000/;
+   proxy_pass http://grafana:3000/grafana/;
    proxy_set_header Host $host;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto $scheme;

    # Grafana Live uses a WebSocket
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
}

The forwarded headers matter too. Grafana builds its redirects and links from them, so without the real host and scheme it points the browser at addresses it can't reach from outside the Docker network.

The rule I took from it

Decide which side owns the prefix, and only let that side touch it:

Both sides handling it loops, as above. Neither handling it sends the browser to paths outside the sub-path, which 404 instead.

All projects The commit LoadLab on GitHub