Reverse-proxy not rewriting Location URL on redirect

1. The problem I’m having:

I am setting up a reverse proxy in dev environment to direct all calls to localhost:9000/services/* to localhost:8080/services/*. The upstream host redirects occasionally to another URL on the same upstream host (say call to localhost:8080/services/A redirects to localhost:8080/services/B). The response code is 503 and the URL is in Location header. Based on my understanding the Location URL is supposed to be rewritten to point to the reverse proxy hostport (ocalhost:8080/services/B to localhost:9000/services/B) automatically. That is not happening. Contents of Location (localhost:8080/services/B) is making it to the client as is. What am I doing wrong? If it needs to be explicitly overwritten, how do I do it?

2. Error messages and/or full log output:

There is no error message but the Location header is not overwritten

"Location": ["http://localhost:8080/services/B"],

3. Caddy version:

v2.8.4

4. How I installed and ran Caddy:

Downloaded Windows executable from Caddy downloads.

a. System environment:

Windows

b. Command:

Caddyfile is in the same folder as the caddy executable

caddy run

c. Service/unit/compose file:

d. My complete Caddy config:

:9000 {
    reverse_proxy /services/* :8080
}

5. Links to relevant resources:

Howdy @RogerT, welcome to the Caddy community.

Can you elaborate on how you came to this understanding?

I’m not aware of any automated behaviour like this. With very few exceptions, Caddy attempts to be as transparent of a proxy as it possibly can. That means, generally, passing all responses from the upstream server as faithfully as possible to the client - including possibly-incorrect Location headers.

To configure the reverse proxy module to rewrite headers from upstream before passing them to the client, you’d want to use header_down and specify a regex replacement: reverse_proxy (Caddyfile directive) — Caddy Documentation

1 Like

Thanks @Whitestrake for pointing in the right direction. I came to that understanding based on a defect discussion on Caddy Github. Probably not the right source for this kind of information.

I am able to rewrite the Location header now. Thanks!

2 Likes

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