现在前端vue多采用hash模式,使用 URL 的 hash 来模拟一个完整的 URL,于是当 URL 改变时,页面不会重新加载。当你使用 history 模式时,URL 就像正常的 url,例如 http://yoursite.com/user/id
,也好看!但是使用这种模式时,后端也要进行配置。
在配置之前,首先要将前端的Vue Route的base改为和nginx将要配置的location保持一致,比如说前端base: '/demo/history/',则nginx的location也要为 location /demo/history{},如下所示:
location /demo/history {
try_files $uri $uri/ /demo/history/index.html;
alias <前端打包路径>;
index index.html;
}
注意点:前端的Vue Route的base改为和nginx将要配置的location保持一致。但是base结尾比location多一个/。