方案一 (这种方式容易被第三方劫持)
location / { root /data/nginx/html; index index.html index.htm; error_page 404 /index.html; }
方案二
location / { root /data/nginx/html; index index.html index.htm; if (!-e $request_filename) { rewrite ^/(.*) /index.html last; break; } }
方案三 (vue.js官方教程里提到的https://router.vuejs.org/zh-cn/essentials/history-mode.html)
server { listen 80; server_name localhost; root /data/wwwroot/dist; location / { try_files $uri $uri/ @router; index index.html index.htm; } location @router { rewrite ^.*$ /index.html last; } }
或者
location / { try_files $uri $uri/ /index.html; }
参考以下两位大神的博客:
https://blog.csdn.net/u011025083/article/details/80352301 https://www.jianshu.com/p/02ad1f919471