vue2 webpack 部署二级目录、根目录nginx配置及打包配置调整

二级目录abc , 打包vue.config.js中 的 publicPath需设置为 "/abc/"

复制代码
server {
        listen       8303;
        server_name  localhost;


	location /abc{
	    alias dist-abc-lc/;  # 项目打包后的目录  【# 20250722 二级目录部署的前端 】  对应代码 publicPath " /abc/ "
	    try_files $uri $uri/ /abc/index.html;
	}

	location /prod-api
       {
         proxy_pass http://localhost:8003;
       }	

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }

根目录 nginx配置, 打包vue.config.js中 的 publicPath需设置为 "/"

复制代码
server {
        listen       8304;
        server_name  localhost;

	location /{
	    add_header Cache-control "no-cache, no-store";
            root  dist-lc;  # 项目打包后的目录  【 20250722 跟目录部署的前端 】 对应代码 publicPath " / "
            try_files $uri $uri/ /index.html;
         }

	location /prod-api
       {
         proxy_pass http://localhost:8003;
       }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }

其它代码调整细节:

1、首先是路由配置 base: process.env.BASE_URL,

复制代码
export default new Router({
//mode: 'history', // 去掉url中的#
  scrollBehavior: () => ({ y: 0 }),
  base: process.env.BASE_URL, // 这个就是上面的publicPath
  routes: constantRoutes
})

2、index.html中引入的静态资源 ,需使用相对目录 ./ ,如下示例引入jquery:

复制代码
<script src="./js/jquery-3.4.1.min.js" type="text/javascript"></script>

Nginx 配置中root和alias的区别分析_root alias 区别-CSDN博客

相关推荐
崔庆才丨静觅1 天前
hCaptcha 验证码图像识别 API 对接教程
前端
passerby60611 天前
完成前端时间处理的另一块版图
前端·github·web components
掘了1 天前
「2025 年终总结」在所有失去的人中,我最怀念我自己
前端·后端·年终总结
崔庆才丨静觅1 天前
实用免费的 Short URL 短链接 API 对接说明
前端
崔庆才丨静觅1 天前
5分钟快速搭建 AI 平台并用它赚钱!
前端
崔庆才丨静觅1 天前
比官方便宜一半以上!Midjourney API 申请及使用
前端
Moment1 天前
富文本编辑器在 AI 时代为什么这么受欢迎
前端·javascript·后端
崔庆才丨静觅1 天前
刷屏全网的“nano-banana”API接入指南!0.1元/张量产高清创意图,开发者必藏
前端
剪刀石头布啊1 天前
jwt介绍
前端
爱敲代码的小鱼1 天前
AJAX(异步交互的技术来实现从服务端中获取数据):
前端·javascript·ajax