Skip to content

Instantly share code, notes, and snippets.

@Visionchen
Last active January 26, 2019 09:19
Show Gist options
  • Select an option

  • Save Visionchen/20f63c8afa6f6e53d51174cdf37ce2c6 to your computer and use it in GitHub Desktop.

Select an option

Save Visionchen/20f63c8afa6f6e53d51174cdf37ce2c6 to your computer and use it in GitHub Desktop.
spa
### 1.单页面应用上传到服务器后刷新出现404的问题
```language
//nginx保证2点就行:
1.刷新时要保证url路径不变。。。言外之意就是不能用重定向去访问index.html,因为url会变。变了js框架就没法路由。因为不知道你上次是哪个url了。。。
2.服务器响应的内容一定要是index.html,因为index这里加载了js框架和初始化一些东西(这里说的不一定对哈,我是搞后端开发的,前端框架没了解过,只是猜测!~~)。
总结:不改变url的情况下响应index.html资源的内容。   好办! 用nginx配置下就好了嘛。
location /demo/ {
rewrite .* /index.html break;
root /data/build;
}
location /static {
alias /data/build/static;
index index.html;
}
或者
server{
listen 80;
server_name www.domain.com domain.com;
location ~* \.js$ {
root /home/hard/Project/game/web-client/build/js;
}
location / {
root /home/hard/Project/game/web-client/build/html/;
}
location ~* html {
rewrite .* /index.html break; //rewrite解决
root /home/hard/Project/game/web-client/build/html/;
}
}
这里有个很重要的点:break 
break 并不是重定向说告诉浏览器你重新去访问XXX.html 。。而是说内部去找  /index.html页面。去 /data/build 目前下找就好。找到就返回页面内容(这不就是我们要的index.html的内容啊),浏览器是无感知的。这样就保证了在url不变的情况 下固定返回index.html内容啦。。如果在/data/build中没找到index.html就会404了
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment