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 天前
Harmony os 静态卡片(ArkTS + FormLink)详细介绍
前端·microsoft·harmonyos·鸿蒙
用户47949283569151 天前
Bun 卖身 Anthropic!尤雨溪神吐槽:OpenAI 你需要工具链吗?
前端·openai·bun
p***43481 天前
前端在移动端中的网络请求优化
前端
g***B7381 天前
前端在移动端中的Ionic
前端
2***s6721 天前
Failed to restart nginx.service Unit nginx.service not found
运维·nginx
拿破轮1 天前
使用通义灵码解决复杂正则表达式替换字符串的问题.
java·服务器·前端
whltaoin1 天前
【 Web认证 】Cookie、Session 与 JWT Token:Web 认证机制的原理、实现与对比
前端·web·jwt·cookie·session·认证机制
Aerelin1 天前
爬虫playwright入门讲解
前端·javascript·html·playwright
5***o5001 天前
前端在移动端中的NativeBase
前端
灵魂学者1 天前
Vue3.x —— 父子通信
前端·javascript·vue.js·github