我正在尝试构建一个利用 HTML5 应用程序缓存的单页应用程序,它将为每个不同的 URL 缓存应用程序的全新版本,因此我必须将每个人重定向到/
并让我的应用程序随后路由它们(这是使用的解决方案devdocs.io http://devdocs.io).
这是我的 nginx 配置。我希望所有请求发送文件(如果存在),重定向到我的 API:/auth
and /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;
}
该循环消息表明 /files/whatever/public/index.html 不存在,因此当 location / 中的 try_files 等于时找不到 $uri/index.html
,因此 try_files 始终在内部将这些请求重定向到执行外部重定向的 @ 位置。
除非您的设置比您所概述的更复杂,否则我认为您不需要做那么多。对于单文件 js 应用程序,您不需要外部重定向(甚至内部重定向)或服务器端 cookie 发送。 app 和 api 的正则表达式匹配也不太正确。
root /files/whatever/public;
index index.html;
location / {
try_files $uri /index.html =404;
}
# Proxy requests to "/auth" and "/api" to the server.
location ~ ^/(auth|api) {
proxy_pass http://application_upstream;
proxy_redirect off;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)