Node.js前端程序通过Nginx部署后刷新出现404问题的解决办法


方案一 (这种方式容易被第三方劫持)

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;
    }
}

参考以下两位大神的博客:

https://blog.csdn.net/u011025083/article/details/80352301
https://www.jianshu.com/p/02ad1f919471

  


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM