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路由

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

相关推荐
用户059540174468 分钟前
Playwright测试AI记忆存储踩坑实录:这个时序问题让我排查了6小时
前端·css
IT_陈寒10 分钟前
Redis踩了个大坑,原来DEL命令也会卡住整个实例
前端·人工智能·后端
AlbertS23 分钟前
Let‘s Encrypt 证书自动续期并自动应用到Nginx
运维·nginx·ssl·cerbot·renew·续期
一个水瓶座程序猿.28 分钟前
基于Spring AI RAG 的AI知识库前端交互实现
前端·人工智能·spring
breeze jiang37 分钟前
React + TypeScript 编辑表单:为什么要区分 name 和 editingName
前端·typescript
米码收割机1 小时前
【移动】线上购物移动端网站(源码+文档)【独一无二】
java·开发语言·前端·python·django
To_OC1 小时前
对接大模型流式接口,我被一个 ReadableStream 卡了半小时
前端·node.js·llm
fthux7 小时前
RenoPit 能为普通业主做什么?看懂图纸、审查合同,提前发现装修坑
javascript·人工智能·ai·开源·github·chrome扩展·open source·edge扩展·firefox扩展
仿生狮子8 小时前
✂️ Nuxt 最简单的字体裁剪工具:Fontize
javascript·vue.js·nuxt.js
石小石Orz11 小时前
我发现了开发者AI产品营收的新方向
前端·虚拟现实