Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reverse proxy to a service while re-writing the request path #3209

Closed
anoadragon453 opened this issue Mar 31, 2020 · 10 comments
Closed

Reverse proxy to a service while re-writing the request path #3209

anoadragon453 opened this issue Mar 31, 2020 · 10 comments
Labels
question ❔ Help is being requested

Comments

@anoadragon453
Copy link

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

@mholt
Copy link
Member

mholt commented Mar 31, 2020

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!

@mholt mholt closed this as completed Mar 31, 2020
@mholt mholt added the question ❔ Help is being requested label Mar 31, 2020
@anoadragon453
Copy link
Author

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?

@mholt
Copy link
Member

mholt commented Mar 31, 2020

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.

@mholt
Copy link
Member

mholt commented Apr 1, 2020

Although, that particular result is a bug... hmm 🤔

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

mholt added a commit that referenced this issue Apr 1, 2020
If a placeholder in the path component injects a query string such as
the {http.request.uri} placeholder is wont to do, we need to separate it
out from the path.
@mholt
Copy link
Member

mholt commented Apr 1, 2020

@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.)

@anoadragon453
Copy link
Author

@mholt That commit works great, thank you!

@tckb
Copy link

tckb commented Jun 26, 2020

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

@migueldinacode
Copy link

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

Did you finally achieve that? I am struggling with the same problem.

@anoadragon453
Copy link
Author

@tckb @migueldinacode I would probably file a new issue with some details, as the original case here as been fixed.

@francislavoie
Copy link
Member

As Matt wrote above, please ask your usage questions on our community forums https://caddy.community/. This issue is closed and resolved, therefore the not right place to ask.

@caddyserver caddyserver locked as resolved and limited conversation to collaborators Nov 17, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
question ❔ Help is being requested
Projects
None yet
Development

No branches or pull requests

5 participants