Vue項目打包之後,靜態文件部署到nginx之後,我們訪問項目一些子頁面的時候,刷新會報404,這是因爲刷新頁面時訪問的資源在服務端找不到,因爲vue-router設置的路徑不是真實存在的路徑。
針對這種問題,我們只需在nginx配置文件中加一些簡單配置就可以,配置如下:
server {
listen 80;
server_name localhost;
index index.html index.htm index.php;
root /html/dist; #你打包項目存放的位置
location / {
try_files $uri $uri/ @router; #檢測文件存在性重定向到首頁目錄 防止404
index index.html;
}
location @router {
rewrite ^.*$ /index.html last;
}
}