nginx - 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:
- 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;
解决方案
推荐阅读
- javascript - React setState()不适用于无线电字段handleInput方法
- javascript - Firestore 导出 Cron 作业未运行:错误代码 204
- azure - 错误:C:\Program Files\IIS\Microsoft Web Deploy V3\msdeploy.exe 失败,返回码:4294967295
- python - 无法使用 python 包 web.py 提供本地静态 CSS 文件,仅 HTML 显示
- vb.net - 将数据源绑定到gridview后如何在Gridview中显示时隐藏列
- visual-studio-code - VS Code 在键入时不显示 Salesforce 对象或类符号。代码完成不起作用
- javascript - 从对象数组中删除按对象属性小于其他对象的元素
- php - PHP AdWords API v201809 - 如何使用 LocationCriterionService 从 GEO_PERFORMANCE_REPORT 获取城市名称?
- sql - 多连接和处理速度
- c# - C# 中 Nuget 包中的 AWS 凭证