在我的应用程序中,我希望位置“/”返回一个静态index.html文件,我想要“/静态”来提供文件夹中的静态文件,我希望所有其他请求返回404 NOT FOUND。 稍后,我将把所有其他请求redirect到WSGI服务器。
这是目前我的configuration:
# Dev server. server { listen 1337; charset UTF-8; location / { rewrite ^ static/index_debug.html break; } location /static { alias /home/tomas/projects/streamcore/static; } }
静态文件夹工作正常,但“/”返回404未find。 我也试过了:
alias /home/tomas/projects/streamcore/static/index_debug.html;
在位置块中,但返回500内部服务器错误。 好像alias不喜欢单个文件。 另外我试过了:
try_files static/index_debug.html;
但是,这会阻止服务器从错误“try_files指令中的参数数量无效”开始。 显然try_files实际上需要你尝试多个文件,这不是我正在寻找的行为。
所以我的问题是: 如何configuration一个位置块,始终返回一个静态文件?
编辑:我从其他答案看到alias应该确实接受一个文件作为参数,所以我试过:
location = / { alias /home/tomas/projects/streamcore/static/index_debug.html; }
但我仍然只能得到500内部服务器错误。 “/”请求的错误日志说:
[alert] 28112#0:* 7“/home/tomas/projects/streamcore/static/index_debug.htmlindex.html”不是目录
为什么要打开“index_debug.htmlindex.html”? 我没有在任何地方使用index指令。
刚刚testing过,它适用于我:
server { root /tmp/root; server { listen 8080; location /static { try_files $uri =404; } location / { rewrite ^ /static/index_debug.html break; } } }
文件/tmp/root/static/index_debug.html当然存在:)
我可以打任何url,我只是得到静态页面。