【发布时间】:2015-12-12 03:54:37
【问题描述】:
我正在尝试构建一个使用 HTML5 App Cache 的单页应用程序,该应用程序将为每个不同的 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 -
是的。已更新问题以包含它。
-
我的错误日志中没有任何内容。
-
您是否与
curl(或wget)核对过?可能是你的浏览器缓存了错误的重定向…… -
curl给了我一些 nginx HTML<center><h1>302 Found</h1></center><hr><center>nginx/1.8.0</center>和wget说“超过 20 个重定向”。这里没有缓存问题。
标签: redirect nginx single-page-application html5-appcache