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博客

相关推荐
Silver〄line几秒前
前端图像视频实时检测
前端·目标检测·canva可画
三月的一天3 分钟前
React+threejs两种3D多场景渲染方案
前端·react.js·前端框架
拾光拾趣录3 分钟前
为什么浏览器那条“假进度”救不了我们?
前端·javascript·浏览器
香菜狗9 分钟前
vue3响应式数据(ref,reactive)详解
前端·javascript·vue.js
拾光拾趣录16 分钟前
老板突然要看“代码当量 KPI”
前端·node.js
拾光拾趣录23 分钟前
为什么我们要亲手“捏”一个 Vue 项目?
前端·vue.js·性能优化
油丶酸萝卜别吃1 小时前
SSE与Websocket有什么区别?
前端·javascript·网络·网络协议
0wioiw01 小时前
Flutter基础(前端教程①⑨-margin-padding)
前端
rzl022 小时前
SpringBoot6-10(黑马)
linux·前端·javascript
YGY Webgis糕手之路2 小时前
OpenLayers 快速入门(六)Interaction 对象
前端·vue.js·经验分享·笔记·web