hey folks, I have a Jenkins instance running with multi branch builds. The Jenkins builds generate static website folders for each project in different folders. These projects are React apps which need to have some sort of try_files {path} /index.html to work correctly
For example, here are the paths where Jenkins store all the static files + index.html after each build on each branch:
Not sure what I’m doing wrong but the React routing is not working in any of the cases above and for all the /app1/* and /app2/* Caddy is unable to even do the browse showing the folders there.
If anyone can guide me on the best way to achieve this
I think the issue is that root * will also match and will override any other roots. Instead, you should either use handle to wrap each of your root directives to make them mutually exclusive, or you can use one route to order them in a specific way (root * should be first so it acts as the default to be overwritten by the rest)
root essentially just sets a global context variable on a per-request basis that subsequent handlers read from. route is ordered before file_server, so that should work fine, but try_files and rewrite are ordered before route, so I think you should put try_files at the end of the route block.
I believe I found the issue and I believe I’ll have to use a path_regexp matcher to have this working and add the match group as part of my try_files {path} {http.regexp.static.1}/index.html.
Ahhh yeah that does make sense. Because your URLs have /app1/ in them, if you set the root to /opt/jenkins/static/app1, it’ll actually be looking inside of /opt/jenkins/static/app1/{path} which would be like /opt/jenkins/static/app1/app1/foo.html.
I think you should strip the prefix from the URI.
Maybe do this:
root * /opt/jenkins/static/global
root /app1/* /opt/jenkins/static/app1
uri /app1/* strip_prefix /app1
root /app2/* /opt/jenkins/static/app2
uri /app2/* strip_prefix /app2
try_files {path} /index.html
actually, handle_path is not working… it is doing a rewrite to / after it matches the routing, instead of keeping the /app1/ in the uri… expected behaviour!
with route I’m forcing the strip_prefix before setting root. If I change the order, setting root before strip_prefix, it rewrites to /. Does that make any sense?
To be clear, the root directive doesn’t add anything to the path. It just matches an incoming request and sets a variable in the handler chain. The path is unmodified. If you want to rewrite the request you have to do that explicitly. Caddy shouldn’t just change requests willy-nilly without the user explicitly saying it should be different than it is.
yup… bad expression… I meant to say that I didn’t realize /app1 was part of the path for the root directive and wrongly assumed root was stripping the prefix by itself.