vue3 + vite + nginx
在服務(wù)器上部署后打開首頁(yè)都沒問題,打開其他路徑全部 404。
nginx 報(bào)錯(cuò)日志:No such file or directory
其實(shí)查看 build 后的
dist文件夾可以發(fā)現(xiàn),只有一個(gè)index.html,當(dāng)你訪問別的路徑時(shí)nignx查找不到所以就報(bào)錯(cuò)了
解決方案:
在 nginx.conf 中添加: try_files $uri $uri/ /index.html;
server {
listen 80;
server_name localhost;
location / {
root /dist;
index index.html index.htm;
# 在配置文件的此處加上這句話
try_files $uri $uri/ /index.html;
}
}
PS:
其實(shí)上述改動(dòng)就是告訴 nignx 找不到文件的時(shí)候就訪問 index.html 就可以了。
究其原因其實(shí)就是是 vue3 的 router 使用了history模式,該模式與之前hash模式的具體區(qū)別可以自行百度一下,不在此贅述。