• Nginx部署Vue项目动态路由刷新404


    前言

    • 记录下Nginx部署Vue项目刷新404的解决方案,遇到了两次,route用的是history路由模式,动态路由:
    {
      path: '/article/:blogId',
      name: 'blog',
      component: blog
    }
    
    • 然后部署nginx
    location / {
    	root   /usr/local/nginx/html/blog/;
        index  index.html index.htm;
    }
    
    • 然后刷新页面

    在这里插入图片描述


    • what happend?

    在这里插入图片描述

    • 后请教了度娘,度娘回答
     location / {
        root   /usr/local/blog/;
        index  index.html index.htm;
        try_files  $uri $uri/ /index.html;
    }
    
    • 再然后

    在这里插入图片描述


    • what happend?

    在这里插入图片描述
    在这里插入图片描述

    好吧,记录下两次的解决方案,先行感谢两位大佬给的启发


    第一次

    网站没有申请二级域名,部署了多项目,所以想的是添加项目前缀'/blog'访问,比如这个:

    https://www.coisini.club/blog
    
    • router.js
    mode: 'history',
    routes: [
      {
        path: '/blog', // 博客首页
        component: home,
        children: [
          {
            path: '/',
            component: index
          },
          {
            path: '/blog/article/:blogId',
            name: 'blog',
            component: blog
          },
         ....
    
    • build.js
    出错打包配置
    assetsPublicPath: './',
    
    正确打包配置
    assetsPublicPath: '/blog/',
    
    • 就是这个assetsPublicPath资源路径忘记配置惹了N多麻烦, 留下了不学无数的眼泪,如果有遇到同样场景的同学记得核实

    在这里插入图片描述

    • nginx.conf
    有两种配置方式,均验证有效
    方式一:
    location /blog {
        root   /usr/local/nginx/html/;
        index  index.html index.htm;
        try_files  $uri $uri/ /blog/index.html;
    }
    
    方式二:
    location /blog {
        root   /usr/local/nginx/html/;
        index  index.html index.htm;
        try_files $uri $uri/ @rewrites;
    }
    
    location @rewrites {
        rewrite ^/(blog)/(.+)$ /$1/index.html last;
    }
    
    • LATER

    在这里插入图片描述


    第二次

    网站申请了二级域名,之前的配置都要修改了,比如这样:

    https://blog.coisini.club/
    
    • router.js
    mode: 'history',
    routes: [
      {
        path: '/',
        component: index
      },
      {
        path: '/article/:blogId',
        name: 'blog',
        component: blog
      },
      ....
    
    • build.js
    assetsPublicPath: './',
    
    • nginx.conf
    server {
        listen       443 ssl;
        server_name  blog.coisini.club;
        
        location / {
            root   /usr/local/blog/;
            index  index.html index.htm;
        }
    }
    
    • 然后部署

    在这里插入图片描述
    在这里插入图片描述

    • 然后照例请教度娘,度娘说try_files $uri $uri/ /index.html;
     location / {
        root   /usr/local/blog/;
        index  index.html index.htm;
        try_files  $uri $uri/ /index.html;
    }
    
    • 然后

    在这里插入图片描述

    在这里插入图片描述

    • 然后,看了无数篇一毛一样的博客后,找到了这位空虚公子vue部署在nginx后刷新404,虽然你肾虚,但我还是
      在这里插入图片描述

    • 正确的写法:

    • build.js

    assetsPublicPath: '/',
    
    • nginx.conf
    location / {
        root   /usr/local/blog/;
        index  index.html index.htm;
        try_files  $uri $uri/ /index.html =404;
        autoindex  on;
    }
    
    • LATER

    在这里插入图片描述



    - End -
    - 个人笔记 -
    - 仅供参考 -

  • 相关阅读:
    sass和compass的配置
    MAC apache配置
    js库
    Tomcat7 配置 ssl
    同一对象内的嵌套方法调用AOP失效原因分析
    Spring Boot文件无法下载问题排查过程记录
    Apache、Spring、Cglib的beancopy效率对比
    使用in作为查询条件优化SQL并使用set筛选结果集
    Java使用foreach遍历集和时不能add/remove的原因剖析
    Python爬虫实践——爬取网站文章
  • 原文地址:https://www.cnblogs.com/maggieq8324/p/15961379.html
Copyright © 2020-2023  润新知