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