【发布时间】:2016-06-08 20:39:26
【问题描述】:
我有这个文件夹结构:
/document/root/
|-- main
`-- wishlist
我想让我的 nginx 像这样工作:如果我将浏览器指向 example.com/wishlist,它将在 wishlist 文件夹中显示 index.html。如果我将浏览器指向example.com,我希望它回退到main/index.html(当然还有相关的main/style.css 和主目录中的其他文件)。
我不想为我根目录下的每个文件夹编写位置规则,所以我希望它尽可能通用。我找到了this 问题,它帮助我完成了大部分工作,但有些东西不起作用:如果我将浏览器指向wishlist/index.html,它会完美运行。但是,如果我删除 index.html 并将其指向 example.com/wishlist,浏览器将返回 404。我当前的 Nginx 配置如下。有人可以指出我正确的方向吗?谢谢。
server {
listen 80;
listen [::]:80;
server_name example.com www.example.com;
root /document/root/main;
location ~ ^/([^/]+)(/.+)?$ {
if (!-d "$document_root/$1") {
return 404;
}
try_files /$1$2 /main$2 =404;
}
}
【问题讨论】: