如何在 nginx 中提供 html 文件而不在此别名设置中显示扩展名
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15451191/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to serve html files in nginx without showing the extension in this alias setup
提问by askmike
I'm having a lot of trouble setting up this alias inside nginx to display my website correctly.
我在 nginx 中设置这个别名以正确显示我的网站时遇到了很多麻烦。
The website I'm concerned with should be accessible from mywebsite.com/mr
and is different from the site located at mywebsite.com/
. The website is located at /fullpath
(shortened for simplicity) The site needs to serve three kinds of content:
我关注的网站应该可以从mywebsite.com/mr
位于mywebsite.com/
. 该网站位于/fullpath
(为简单起见缩短)该网站需要提供三种内容:
- The index file located in
/fullpath/index.html
. - Other html files (without showing the
.html
extension in the browser). - Static assets (js/css/img) located in
/fullpath
and subdirectories.
- 位于
/fullpath/index.html
. - 其他 html 文件(不在
.html
浏览器中显示扩展名)。 - 位于
/fullpath
和 子目录中的静态资产 (js/css/img) 。
I've tried changing around the order of matches in the try_files
and found situations where they all worked, just not at the same time:
我尝试在 中更改匹配顺序try_files
并发现它们都起作用的情况,只是不同时:
location /mr {
default_type "text/html";
alias /fullpath;
# with this one 1 and 3 work
# try_files $uri/index.html $uri.html $uri;
# with this one 2 and 3 work
# try_files $uri $uri.html $uri/index.html;
# with this one 1 and 2 work
try_files $uri.html $uri/index.html $uri;
}
When one doesn't work it 404's. Does anybody know how I can serve all kinds of files correctly?
当一个不起作用时,它是 404 的。有人知道我如何正确提供各种文件吗?
回答by Danack
Apparently alias and try_files don't work together. However, I don't think you need to use alias.
显然 alias 和 try_files不能一起工作。但是,我认为您不需要使用别名。
location /mr {
default_type "text/html";
try_files /fullpath/$uri /fullpath/$uri.html /fullpath/$uri/index.html /fullpath/index.html;
}
Which would try:
哪个会尝试:
- Exact file.
- File with .html added.
- Index in the path.
- Default index.
- 确切的文件。
- 添加了 .html 的文件。
- 路径中的索引。
- 默认索引。
I think the root directive does work with try files but am unable to test.
我认为 root 指令确实适用于 try 文件,但无法测试。
server{
location /mr {
root /home/mysite/fullpath;
default_type "text/html";
try_files $uri $uri.html $uri/index.html index.html;
}
}
回答by Troy Grosfield
I used a combo of what @Danack posted which led me to the result I was looking for (serve the html file directly):
我使用了@Danack 发布的内容的组合,这使我得到了我正在寻找的结果(直接提供 html 文件):
location /health-check {
default_type "text/html";
alias /path/to/my/file.html;
}