前端项目jenkins发布及nginx路由策略分析

前端项目jenkins发布及nginx路由策略分析


jenkins发布成功日志:
scooterConsole/css/3209.1ac55f9.css 0.15 kb 0.14 kb
scooterConsole/css/6603.31d6cfe.css 0.00 kb 0.02 kb

图片以及其他类型的资源不显示

构建完成, 可以去构建目录 scooterConsole 查看
Done in 132.97s.
+ ssh XX.XX.22.212 'rm -rf /html/system/scooterConsole'
+ scp -r scooterConsole XX.XX.22.212:/html/system/
Finished: SUCCESS


物理目录:/html/system/scooterConsole/index.html
对应 URL:http://域名或IP/system/scooterConsole/

http://XX.XX.22.212/system/scooterConsole/
或绑了域名:https://xxx.com/system/scooterConsole/
注意:日志里是子目录部署(/system/scooterConsole),不是根目录部署。


可以直接访问:OK~
https://域名或IP/system/scooterConsole/


这个路由的打通是 Jenkins 负责“构建与投递”​ + Nginx 负责“静态拦截与反向代理”​ 共同完成的
一、 Jenkins 构建与投递链路

Jenkins 的参数与日志决定了文件的“出生”与“落点”:

构建参数映射:

SSH_SERVER: fat1 → 对应你服务器上的 fat1.域名.conf(即 XX.XX.22.212)。

remoteType: system → 决定了远程目录的上级路径为 /html/system/。

remoteProject: scooterConsole → 决定了最终项目名。

构建产出:前端打包后生成 scooterConsole 目录(包含 index.html、css、js)。

远程投递(关键):

先执行 rm -rf /html/system/scooterConsole(清理旧版本)。

再执行 scp -r scooterConsole XX.XX.22.212:/html/system/。

最终物理落点:/html/system/scooterConsole/。

二、 Nginx 路由匹配链路

当浏览器访问 https://域名或IP/system/scooterConsole/ 时,Nginx 的处理过程如下:

域名匹配:命中 fat1.域名.conf 中的 server_name 域名或IP(监听 443 端口)。

静态路由拦截(你上步加的配置):

请求 URI /system/scooterConsole/ 优先命中你添加的 location /system/scooterConsole/。

路径转换:通过 alias /html/system/scooterConsole/,将 URI 映射到物理磁盘。

文件返回:try_files 找到 index.html 返回给浏览器(页面显示“Sign In”登录页)。

兜底代理(后端接口):

如果请求的是登录接口或订单数据(如 /system/scooterConsole/order),静态目录找不到时,通常会被 location / 兜底。

location / 将请求 proxy_pass 到 http://localhost:20004/(后端 Java/Node 服务),实现前后端分离下的接口透传。

结论:页面能显示,说明 localhost:20004(你的后端服务)自身具备静态资源服务能力(如 Spring Boot 的 classpath:/static/ 或 Node 服务),它直接返回了这个登录页的 index.html。

 

 

# 找配置
nginx -t
ls /usr/local/nginx/conf/nginx.conf
grep -R "html" /usr/local/nginx/conf/nginx.conf
grep -R "system" /usr/local/nginx/conf/nginx.conf

 

最快定位法:直接看 /html 目录
ls -la /html/system/scooterConsole/

现在可以执行的 3 条命令(直接复制)
# 1. 确认目录存在
ls /html/system/scooterConsole/
css index.html js

# 2. 看主配置里 system 和 root
grep -n -E "system|root|alias" /usr/local/nginx/conf/nginx.conf

# 3. 看有没有 include 其他配置目录
grep -n "include" /usr/local/nginx/conf/nginx.conf
17: include mime.types;
55: include upstream/*.conf;
56: include vhosts/*.conf;


根目录:
/usr/local/nginx/conf


nginx.conf文件包含:
include upstream/*.conf;
include vhosts/*.conf;

然后找到/vhosts/fat1.域名.conf
fat1.域名.conf 文件中有以下这行: 【关键】 目录路径: /usr/local/nginx/conf/vhosts/

include location/*.conf;


这是官网的地址,所以不会走官网去转发一次,这个需要避坑。真相
location / {
include location/cache.rule;

proxy_pass http://localhost:20004/;

 

突破口: 【找到system.conf 这个文件】 目录路径: /usr/local/nginx/conf/location
然后找到 /location/system.conf

# 确认 system.conf 位置
find /usr/local/nginx/conf -name "system.conf"

# 看是否被 include
grep -rn "system.conf" /usr/local/nginx/conf/
没有结果: 通配符加载,不需要写具体文件名。说明 system.conf 不是被 grep 能直接找到的那种 include 方式加载的。

# 全盘搜 include 了哪些目录
grep -rn "include" /usr/local/nginx/conf/

# 看白名单
grep -n "scooterConsole" /usr/local/nginx/conf/location/system.conf


流程链路:
浏览器请求
https://域名或IP/system/scooterConsole/
↓
Nginx 匹配 server: fat1.域名.conf
↓
没命中 /tenpay/、/alipay/ 等
↓
命中正则 location ~ ^/system/?(\w*)   核心:没加 location /system/scooterConsole/ 也能访问——因为它被一个更通用的正则 location 接管了。真相
↓
$dir = scooterConsole
↓
root /html → 找 /html/system/scooterConsole/index.html
↓
文件存在(Jenkins 投的) → 直接返回

结论:
Jenkins 把文件放到 /html/system/scooterConsole/
system.conf 里的正则 location 自动把 /system/* 映射到 /html/system/*
根本不需要单独加 location /system/scooterConsole/

 


补充扩展:常规的配置思路:
server {
listen 80;
server_name 域名.com; # 或 IP

location /system/scooterConsole/ {
alias /html/system/scooterConsole/;
index index.html;
try_files $uri $uri/ /system/scooterConsole/index.html;
}
}

# 查看 Nginx 启动时实际读取了哪些文件
/usr/local/nginx/sbin/nginx -T 2>/dev/null | head -50

# 搜索 system.conf 的内容是否生效
/usr/local/nginx/sbin/nginx -T 2>/dev/null | grep -A5 "scooterConsole"

# 检查语法
/usr/local/nginx/sbin/nginx -t

# 重载配置(不中断服务)
/usr/local/nginx/sbin/nginx -s reload


Jenkins 日志表明:
代码构建后发布到 XX.XX.22.212:/html/system/scooterConsole​
浏览器访问地址是:http(s)://域名或IP/system/scooterConsole/​
页面打不开就去服务器查 Nginx 的 location /system/scooterConsole 配置。

 

 

 

 

 

posted on 2026-11-06 16:54  oktokeep  阅读(8)  评论(0)    收藏  举报