I am trying to have nginx serve an index.html (if present) from inside a specific (sub-)subfolder. The problem is that this kind of works but when requesting the folder without a slash at the end will serve the index.html from that subfolder but subsequently break any linked files from that index.
My nginx config looks like this:
location ^~ /folder {
try_files $uri $uri/index.html $uri.html =404;
}
location / {
...do regular website stuff
}
/folder does not contain any files but a couple of subdirectories and these all have an index.html.
Things that work fine are:
- mywebsite.com: I get served the regular website
- mywebsite.com/folder: returns a 404
- mywebsite.com/folder/subfolder/: returns the index.html from that subfolder and all linked files (stylesheets, script, images, ...) are fine
However, what does not work correctly is: mywebsite.com/folder/subfolder (without the slash at the end). Then I get served the index.html from the subfolder but all linked files are broken because they point to /folder. So my best guess is that I need to rewrite the url to include a subsequent slash but my skill level in nginx configurations is not up to that task.
/as explained by Richard Smith, this did the trick:
try_files $uri $uri/ $uri.html =404;
question from:
https://stackoverflow.com/questions/65919487/serving-index-html-from-nginx-but-keep-file-links-intact-through-rewriting 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…