Caddy: Reverse proxy to a service while re-writing the request path

Created on 1 Apr 2020  路  7Comments  路  Source: caddyserver/caddy

In caddy's documentation, it's stated that:

Additionally, schemes cannot contain paths or query strings, as that would imply simultaneous rewriting the request while proxying, which behavior is not defined or supported.

I have a setup which requires something along the lines of this. I have users coming to a URL:

https://bla.example.com/login?token=xxxx

And a service running on the same box that I'm reverse proxying to that must have a prefix before any paths it receives. Essentially, it's expecting:

GET /service/login?token=xxxx

So... I essentially need to prefix /service to the query path of requests, but as I understand it caddy doesn't support this while reverse proxying.

Is there another way around this situation? This can be done in other webservers but I want to use Caddy :D

question

All 7 comments

Of course:

route {
     rewrite * /service{uri}
     reverse_proxy ...
}

should do what you need. You might not even need route { ... } depending on the rest of your config.

as I understand it caddy doesn't support this while reverse proxying.

So, that's correct. Do it before reverse proxying.

If you have any further questions, please ask on our forums. Thanks!

Works great, thanks! Though you'll want to take off the {uri} bit else you end up with:

/service/%3Flogin%3Dxxx?login=xxx

Also, is that bit from the documentation incorrect or am I just reading it wrong? Should it point to the route/redir docs page?

Sure, customize to suit your needs. Again, you might not even need route if all your requests go to the proxy anyway.

Also, is that bit from the documentation incorrect or am I just reading it wrong?

Which bit, the quoted bit? It's correct. The proxy does not rewrite requests. That's what the rewrite directive is for.

Although, that particular result is a bug... hmm 馃

In the meantime, a rewrite to /service{path}?{query} should suffice as a workaround.

@anoadragon453 I believe I have fixed the bug that caused the weird results you saw, in 809e727. Please build from that commit or download one of the CI artifacts and let me know if it works for you! (You should be able to use {uri} again to preserve the original URI without problems.)

@mholt That commit works great, thank you!

Hi @mholt i have the same requirement but in my case it鈥檚 an external url i.e., a proxy pass.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

mikolysz picture mikolysz  路  3Comments

lorddaedra picture lorddaedra  路  3Comments

aeroxy picture aeroxy  路  3Comments

klaasel picture klaasel  路  3Comments

whs picture whs  路  3Comments