背景
Angular 的单页应用中因为有用到锚点,所以只能用 browser router。假设目标地址为 app.com/target1。
问题
在访问 app.com/target1 时,路由跳转到 app.com/target1/target2, 刷新页面后 404。
相关配置如下:
server {
listen 4300;
server_name business;
location / {
try_files $uri $uri/ /index.html;
root tempPath/dist/;
}
}
server {
listen 80;
server_name entry;
root tempPath/;
location ^~ /business {
proxy_pass http://localhost:4300;
}
}
尝试的解决方法
-
关键字搜了
browser router with nginx,看到的都是用try_files $uri $uri/ /index.html。我试着把最后一个参数改成app.com/target1所在的物理路径,也就是tempPath/dist/,错误日志会说:rewrite or internal redirection cycle while internally redirecting to "/index.html" 。后来想想,逻辑上确实是循环,此路不通。 -
从页面物理层面解决。就是每个路由都写个 html,但这真的很麻烦...
希望获得帮助的点
希望能在 1 的方向上给点建议。看上去只要在匹配到 /business 时停下来,直接转发到 html 入口就能解决了...