Paste full file contents here.
Make sure backticks stay on their own lines,
and the post looks nice in the preview pane.
d. My complete Caddyfile or JSON config:
Paste config here, replacing this text.
Use `caddy fmt` to make it readable.
DO NOT REDACT anything except credentials.
LEAVE DOMAIN NAMES INTACT.
Make sure the backticks stay on their own lines.
3. The problem I’m having:
trying to match on a path prefix like /version/a/b/c which will choose the appropriate reverse proxy but then forward the request without the prefix version… so the request will just be /a/b/c
Make sure your { match all the closing }. You have an extra one now at the end of the reverse_proxy line.
That’s not a problem with Caddy. You need to configure your upstream app to be aware of the subpath you’re proxying to, since it constructs the HTML which contain the script tags.
Using handle_path, Caddy will only handle requests which have /version1* in the path. Other requests will go unhandled, which results in an empty 200 response. You either need to configure Caddy to handle those other requests with another handle block, or configure your upstream app to avoid needing to do the handle_path in Caddy.
In this common scenrio, what is needed is for the application to be unaware of the proxy.
To do that:
match on the prefix /versionNumber
strip the prefix so that the request is proxied to the application as norml but port based on /versionNumber (e.g. versionNumber = 1000, reverse_proxy localhost:1000)
rewrite the root for ALL urls found in the responses to the client to put back the /versionNumber prefix
rewrite only rewrites the URI of the incoming request. It doesn’t manipulate responses. Manipulating the request during handle_response doesn’t do anything, because the request has already been used/copied to the proxy upstream.
I don’t understand your goal here. I think you’re approaching this incorrectly. What exactly is your goal, and why are you trying to do this?
Also, please run caddy fmt --overwrite Caddyfile to fix your config’s formatting. It’s messy and hard to read.
have multiple “versions” of an application running… where each version is based on the build number
each version will be running on a port corresponding to the build number: e.g. build 10000 running on port 10000
to access the application using same URI request that the application would normally expect but modified to add/embed the the version number into the path to do the correct reverse_proxy
normal URL: host.acme.com:originalPort/originalPath --> **127.0.0.1:originalPort**/originalPath
modified URL: host.acme.com:originalPort/**versionNumber**/originalPath --> **127.0.0.1:versionNumber**/originalPath
where the application is UNAWARE of any of the path manipulation
Can you let me know what configuration I can use to achieve this?
after running caddy fmt the configuration file currently is:
Why does it have to be unaware? That makes it significantly harder to do this.
If you can just have a base path parameter (environment variable maybe) for that deployment, or even make the base path dynamic based on some HTTP header during that app’s handling (that you could pass with header_up in Caddy, like X-Base-Path: /10000), then you wouldn’t have an issue.
Unfortunately we do not control the source for the application… need to test different versions independently…
This seems to be a very common need/scenario… where a component/prefix of the path is stripped to do the proxy to a specific port… and the response should be rewritten to include the stripped prefix/component on all URLs in the response
It’s not, actually. Most upstream apps will have some config like basePath to control how it builds URLs.
This isn’t something Caddy can magically fix. As the article I linked above explains, you’d need to manipulate the response body, which is non-trivial. You’d have to use regular expressions to update any URLs in the content, but that’s very error-prone, because there’s no fool-proof way to detect URLs in the response body. If you try to replace anything that starts with / or whatever, then you’ll replace too many things, etc. Also, response body replacement is not built-in, you’d need to install a plugin to do it. And I strongly recommend against taking this approach, you’ll cause yourself more headaches.