原因是:这个问题是因为 Vue 的路由模式默认是 hash 模式,只改变 URL 的 hash 部分。但是 Nginx 会把 URL 中的 hash 部分去掉。
解决:在nginx 的server 中添加以下代码:

1
2
3
4
5
6
7
8
location / {
try_files $uri $uri/ /index.html;
}

location ~ ^/.*\.(ico|css|js|gif|jpe?g|png|svg|woff2?|ttf)$ {
add_header Cache-Control "public";
expires 1w;
}

这个配置会正确保留 URL 中的 hash,所以 Vue 路由可以正常工作。