Add a header specifying the upstream path taken by a request?

Hi, I have lots of domains and the general configuration looks like this.

    {
                      "match": [
                        {
                          "host": [
                            "git.ishanjain.me"
                          ]
                        }
                      ],
                      "handle": [
                        {
                          "handler": "reverse_proxy",
                          "upstreams": [
                            {
                              "dial": "ip2:443"
                            },
                            {
                              "dial": "10.0.50.3:443"
                            }
                          ],
                          "load_balancing": {
                            "selection_policy": {
                              "policy": "first"
                            },
                            "retries": 2,
                            "try_duration": "3s",
                            "try_interval": "10ms"
                          },
                          "transport": {
                            "protocol": "http",
                            "tls": {
                              "server_name": "git.ishanjain.me"
                            },
                            "compression": true
                          },
                          "health_checks": {
                            "active": {
                              "uri": "/",
                              "port": 9001,
                              "interval": "10s",
                              "timeout": "10s",
                              "expect_status": 200
                            }
                          }
                        }
                      ],
                      "terminal": true
                    }

Is it possible to add a header specifying the path/upstream address used to serve a request?

I think you could use header_up from reverse_proxy (Caddyfile directive) — Caddy Documentation if I understand your request properly.

Cite:

  • header_up sets, adds (with the + prefix), deletes (with the - prefix), or performs a replacement (by using two arguments, a search and replacement) in a request header going upstream to the backend.

I can use header_up to set headers, yes. but how do you set the value based on the upstream that served the request?

:thinking:
You mean you want to set the My-Upstream-Header to a value per upstream?

For example for Upstream 10.0.50.3 My-Upstream-Header: 01 and for Upstream ip2 My-Upstream-Header: 02 , right?

I don’t thinks that’s possible for now, AFAIK.

At least can’t I see anything in the code for that.

You can use the placeholder {http.reverse_proxy.upstream.hostport}. It’s not clear if you want the value to be sent down to the client or up to the upstream. If the former, then use header_down. If the latter, use header_up. Either way, if, for example, you’re sending it down to client, it’ll look like this:

header_down X-Acting-Upstream {http.reverse_proxy.upstream.hostport}

Next time, please fill out the template.

3 Likes

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.