github.com/thanos-io/thanos@v0.32.5/docs/operating/reverse-proxy.md (about) 1 # Running Thanos behind a reverse proxy 2 3 There are many reasons to use a [reverse proxy](https://www.nginx.com/resources/glossary/reverse-proxy-server/) in front of Thanos, for example, TLS termination (serve Thanos over HTTPS) and basic authentication. This small guide will tell you how to do that. 4 5 ## External Prefix and Route Prefix 6 7 Before continuing, let's first take a look at two CLI flags provided by Thanos, `--web.external-prefix` and `--web.route-prefix`. The external prefix is useful when you want to prefix all requests from the UI with some path. Normally, the web UI would load all static assets from `/static/...`, do API calls on `/api/v1/...` etc. But if we use `--web.external-prefix="thanos"` the UI would prefix each request with `/thanos`. It would try to load all assets from `/thanos/static/...`, do API calls on `/thanos/api/v1/...` and so on. One thing to note here is that `--web.external-prefix` only prefixes the requests and redirects with the specified value, but Thanos is still listening on the root, not the specified sub-path i.e. the API is still accessible at `/api/v1/...` and not at `/thanos/api/v1`. This is where `--web.route-prefix` comes in. If you set `--web.route-prefix="thanos"` every route would get prefixed with the specified value. For example, the API will be accessible on `/thanos/api/v1`. As this is the most common use case when using the `--web.external-prefix`, the default value of `--web.route-prefix` is the value of `--web.external-prefix`. 8 9 Note: Using different values for `--web.external-prefix` and `--web.route-prefix` can lead to the web UI not working properly if it is accessed directly (without a reverse proxy). 10 11 ## Examples 12 13 Let's look into some example scenarios. All examples are using nginx as a reverse proxy here. 14 15 ### Serving Thanos on a subdomain 16 17 Serving a Thanos component on the root of a subdomain is pretty straight-forward. Let's say you want to run Thanos Querier behind a nginx reverse proxy, accessible on domain `thanos.example.com`. A basic nginx configuration would look like this: 18 19 ``` 20 http { 21 server { 22 listen 80; 23 server_name thanos.example.com; 24 25 location / { 26 proxy_pass http://localhost:10902/; 27 } 28 } 29 } 30 ``` 31 32 ### Serving Thanos on a sub-path 33 34 Things become a little tricky when you want to serve Thanos on a sub-path. Let's say, you want to run Thanos Querier behind an nginx server, accessible on the URL `http://example.com/thanos`. The Thanos web UI depends on it being accessed on the same URL as Thanos itself is listening. This is because the UI needs to know the URL from where to load static assets and what URL to use in links or redirects. If Thanos is behind a reverse proxy, particularly one where Thanos is not at the root, this doesn't work so well. 35 36 To tackle this problem, Thanos provides a flag `--web.external-prefix`. 37 38 Let's say we have Thanos Querier running on the usual port, we need nginx running with the following configuration: 39 40 ``` 41 http { 42 server { 43 listen 80; 44 server_name example.com; 45 46 location /thanos/ { 47 proxy_pass http://localhost:10902/thanos/; 48 } 49 } 50 } 51 ``` 52 53 With this configuration, you can access Thanos Querier on `http://example.com/thanos`. Notice that because we are using `http://localhost:10902/thanos/` as the reverse proxy target, every request path will be prefixed with `/thanos`. To make this work we need to run Thanos Querier like this: 54 55 ``` 56 thanos query --web.external-prefix="thanos" 57 ``` 58 59 It should be noted that now the `/thanos` path prefix would be required for all HTTP access to Thanos. For example, the `/metrics` endpoint would be accessible at `http://localhost:10902/thanos/metrics`.