我正在尝试配置nginx以使用支持pushState的应用。我之所以大部分时间都在工作,但我所缺少的是在不存在的文档上返回404。我怎样才能做到这一点?我的nginx.conf的相关部分:
location / {
root /foo;
index index.html;
try_files $uri /index.html;
}
我已经尝试了try_files $uri /index.html =404;
,但这显然不起作用,因为index.html
存在。
我也试过......
if (!-e $request_filename) {
return 404;
}
...在server
和location
块中。两者都没有。
答案 0 :(得分:0)
试试这个。如果文件存在,则重定向到index.html。否则返回404。
location = /index.html {}
location / {
if (!-f $request_filename) {
return 404;
}
rewrite ^/(.*) /index.html redirect;
}