我想要构建一个单页面应用,利用HTML5应用程序缓存,为每个不同的URL缓存一个全新版本的应用程序,因此我必须将所有人重定向到/
,然后让我的应用程序路由它们(这是在 devdocs.io 上使用的解决方案)。
以下是我的nginx配置。我希望所有请求都发送文件(如果存在),重定向到我的API /auth
和/api
,并将所有其他请求重定向到index.html。为什么以下配置会导致浏览器显示有一个重定向循环?如果用户命中了位置块#2,并且他的路由与静态文件不匹配,他将被发送到位置块#3,该位置块将重定向他到“/”,应该命中位置块#1并提供index.html,对吗?这里是什么原因导致重定向循环?有更好的方法来实现这个吗?
root /files/whatever/public;
index index.html;
# If the location is exactly "/", send index.html.
location = / {
try_files $uri /index.html;
}
location / {
try_files $uri @redirectToIndex;
}
# Set the cookie of the initialPath and redirect to "/".
location @redirectToIndex {
add_header Set-Cookie "initialPath=$request_uri; path=/";
return 302 $scheme://$host/;
}
# Proxy requests to "/auth" and "/api" to the server.
location ~* (^\/auth)|(^\/api) {
proxy_pass http://application_upstream;
proxy_redirect off;
}
root
指令和index.html
文件?请检查 error.log。 - Alexey Tencurl
(或者wget
)检查了吗?也许是你的浏览器缓存了错误的重定向... - Alexey Tencurl
给了我一些 nginx HTML<center><h1>302 Found</h1></center><hr><center>nginx/1.8.0</center>
而wget
则显示 "超过20次重定向"。这里没有缓存问题。 - winduptoy