新建 Vue 項目后,配置路由為 history
模式后,瀏覽器刷新后,顯示404。
對於 URL:https://code.nanvon.cn/home
,刷新后瀏覽器訪問的是服務器中/home
路徑的位置,但是服務器中沒有找到該路徑,所以顯示404,需要在nginx配置文件中加上如下代碼:
location / {
try_files $uri $uri/ @router;
index index.html;
}
location @router {
rewrite ^.*$ /index.html last;
}