vue-cli build, vite build 生产部署刷新或弹窗404,页面空白修复方法

1、问题描述

vue前端生产部署后,当我们使用history模式进入页面路由后,刷新浏览器,或者输入页面路由或者弹窗会出现404 not found错误。例如我这里访问数据大屏的项目列表,就会报错如下图:

打开控制台报错如下图:

2、原因分析

这里存在两种情况:

1)应用是直接作为部署在nginx的根目录比如/var/www/html中(这种较少).

2)应用作为二级目录部署在nginx的根目录比如/var/www/html/big中(这种较常见).

一般情况下nginx 默认配置如下:

复制代码
        root /var/www/html;

        # Add index.php to the list if you are using PHP
        index index.html index.htm index.nginx-debian.html;

        location / {
                # First attempt to serve request as file, then
                # as directory, then fall back to displaying a 404.
                try_files $uri $uri/ $uri/index.html $uri/ =404;
        }

当nginx 收到、project/items这个请求时会去root配置路径/var/www/html中找project目录,然后查找该目录下是否有items这个文件,这肯定是找不到的,所以就报了个404错误。

3、解决方案

对应情况1),只需要去掉 try_files 末尾的 $uri/ =404

配置如下

复制代码
        location / {
                # First attempt to serve request as file, then
                # as directory, then fall back to displaying a 404.
                try_files $uri $uri/ $uri/index.html ;
        }

对应情况 2),建议新增代理,让找不到的页面都去访问index.html

配置如下:

复制代码
        location /big/ {                                                                                                
               try_files $uri $uri/ $uri/index.html /big/index.html;  #router = history                                 
        }   

这种情况建议最好去掉 location / ,容易被拦截了报500错误。

这种情况建议最好去掉 location / ,容易被拦截了报500错误。

这种情况建议最好去掉 location / ,容易被拦截了报500错误。
============= 相关博文 ============

vite build 发布到nginx二级目录

Ruoyi-vue-pro Vue + nginx 二级目录部署到云服务器​​​​​​​

ruoyi-vue-pro数据大屏------路由支持history,告别难看的hash路由

============= 相关博文 ============

相关推荐
一 乐3 小时前
动漫书销售商城|基于springboot + vue动漫书销售商城(源码+数据库+文档)
java·数据库·vue.js·spring boot·毕业设计
admin and root3 小时前
「AI安全篇」实战AntiDebug自动化JS逆向加解密MCP
javascript·人工智能·网络安全·自动化·漏洞挖掘·cnvd·src赏金
前端snow4 小时前
ai agent --- postgreSQL 关系型数据库
前端
卷无止境5 小时前
ECharts:把数据变成故事的可视化利器
前端
Eric_见嘉5 小时前
在职前端 Skill 和 MCP 分享
前端·后端·agent
liangshanbo12155 小时前
面试题:如何优化 Webpack 的打包速度?
前端·webpack·node.js
wengad5 小时前
从 wangEditor 迁到 wangEditor-next
前端·javascript·vue.js
行者-全栈开发6 小时前
【前端/JS框架】CVE-2026-3854:GitHub Enterprise Server X-Stat注入XSS漏洞深度剖析与紧急修复指南
前端·javascript·github
计算机魔术师7 小时前
AMD砸82亿买下李飞飞,这次不卖芯片卖什么?
前端
hzxpaipai8 小时前
杭州企业官网怎么建设?从策划到上线的5步建站方法
运维·服务器·前端·网络