I have 3 containers in my azure container instance:
caddy
serviceA (niamondo)
serviceB (niamondo2)
The Caddyfile assure that
URLs like https://nia....westeurope.azurecontainer.io/one/* are properly routed to serviceA
URLs like https://nia....westeurope.azurecontainer.io/two/* are properly routed to serviceB
but I have following routing issues (maybe all having the same root cause):
https://nia....westeurope.azurecontainer.io/one won’t get routed to serviceA. I would like that https://nia....westeurope.azurecontainer.io/one gets me to same page as https://nia....westeurope.azurecontainer.io/one/.
https://nia....westeurope.azurecontainer.io/one/worldmap doesn’t get me to the right page. I need to add a / to get me to the right page. Note that this terminating / was not needed when I access serviceA using a Caddyfile that has the reverse_proxy part without a handle_path
serviceA is returning a page with relative links but these links don’t have the /one/ subpath. So when clicking on these links I navigate to a non-existing page.
The article mentions also another solution based on using header and reverse_proxy directive.
I would like to give that approach a shot but I don’t know exactly how I must configure it. Can you help me with that ?
I am not specialist in this domain but I think that all my problems are related to redirection of pages to another page.
So in the example below you see that /one/worldmap becomes redirected to /worldmap/ while it should be redirected to /one/worldmap/
You’ll just be playing whack-a-mole with different path issues if you take that approach.
The fundamental problem is that your upstream application doesn’t know that it’s supposed to be running under a subpath, so it triggers redirects.
The best/easiest fix is to run each app under each their own subdomains, so they can use the paths they want.
Another option, if you have control of the code, then the upstream app should be modified/configured such that it knows that it’s running under a different base path. This will make sure that all URLs it creates are prefixed before being written to the response (like links in HTML, Location headers, etc)
Then you will need to carefully inspect your application, identify every instance of incorrect links, and manually configure each replacement such that Caddy corrects all the invalid asset links.
@francislavoie is not exaggerating when he says you will be playing whack-a-mole.
I’m afraid that in general, this is not the kind of problem that someone is going to be able to give you a simple answer for - unless they are exceedingly generous with their time and expertise, or you’re paying them to solve the problem for you.
I understand that caddy configuration is fairly simply in case of subdomains but the problem with that is that on azure cloud I cannot define subdomains. This means that I need to create/register my own domain, create subdomains for it and assure that it resolves to the IP address of my azure container instance. The thing is that the IP addres of my azure container instance might change as I am planning to run it only when needed. In other words to assure these subdomains work, I need to implement an automatic update of my domain name IP address that becomes at least triggered when redeploying my container instance. So I prefer not to go with the subdomain solution due to the extra effort and complexity introduced by this automatic IP address update.
I understand that for some applications it might not be an easy problem but in my case the number of redirects seem very limited and once I land on the correct page from then onwards the application is working as expected. So I am not convinced I am playing whack-a-mole.
I have read those pages several times but they don’t provide actual examples of how they can be used to fix redirection issues when using subpaths. So some help with that would be welcome - especially as this is not my domain of expertise.
I also tried to fix some of the issues through redir, but redir works for the subpaths /one and /one/docs but doesn’t work for subpaths /one/worldmap and /one/mui. It is very strange that it doesn’t work for /one/mui but does work for /one as I am redirecting to exactly the same path = /one/mui/. I don’t understand why this is not working - can you help me with that ?
That’s because there is no specific usage for fixing redirection issues with subpaths. This use case falls under the general use of those tools.
For replace-response, you need to find instances of the incorrect links in the response, and replace them with the correct link. That means: inspecting the HTML of the response, determining what the incorrect part looks like, and configuring Caddy to replace it in-flight with the correct response. The general example is sufficient for what you need.
It won’t work for location headers, so you need to fix that with reverse-proxy, using header manipulation: reverse_proxy (Caddyfile directive) — Caddy Documentation. That means the same as above, but inspecting the response headers, rather than the response body itself, and correcting the contents of the (typically) Location header. The specification in the Syntax section is what you need:
Before diving into header manipulation, I would like first to understand why the redir is not working for the /one/mui path and is working for the /one path while they both redirect to exactly the same path /one/mui/ which is also confirmed by bellow curl commands.
It seems that redir is not working in browser if the new path is just the original path with / added at the end. To fix this I have changed the original path (so instead of /one/worldmap I have taken /one/map which will be redirected to /one/worldmap/).