最近在部署vue项目是遇到,Uncaught SyntaxError: Unexpected token ‘<’ 路由问题,项目能够正常访问,但是每次刷新之后,会显示空白页面。最后经过分析,是路由问题,项目配置在根目录下是没有问题;配置在其他目录下,也可以正常访问,但是这里会有一个问题,就是每次刷新后会是空白页面,在分析问题过程中发现,每次刷新后的路径并不是走我们配置的路径,而是走了根目录的配置,从而与我们配置的路径产生冲突,最终导致vue router不知道要走哪个路由,产生冲突如下实例:

nginx配置的url: http://yxp.xinos.vip/admin/dashboard/workplace
刷新后实际的url: http://yxp.xinos.vip/dashboard/workplace

在这里插入图片描述
解决方法:
增加nginx配置:

location @fallback {
   rewrite ^(.+)$ /admin/index.html last;
}

完整配置

    location /admin {
        alias /www/wwwroot/topfus_research/research_ui/;
        try_files $uri $uri/ @fallback;
        index index.html;
    }

    location @fallback {
        rewrite ^(.+)$ /admin/index.html last;
    }
    
    location / {
        root /www/wwwroot/topfus_research/research_web/;
        try_files $uri $uri/ @router;
        index index.html;
    }
   
    location @router{
       rewrite ^(.+)$ /index.html last;
    }

这里需要注意一点是根木必须配置为 root 路径,其他配置为 alias 路径

关于alias和root的区别:
root和alias是系统文件路径的设置。
root用来设置根目录,而alias用来重置当前文件的目录。

Logo

前往低代码交流专区

更多推荐