首页 > 解决方案 > Serving index.html from nginx but keep file links intact through rewriting(?)

问题描述

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:

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;

标签: nginxurl-rewriting

解决方案


推荐阅读