温馨提示×

centos jenkins如何代理

小樊
58
2026-09-09 07:58:45
栏目: 智能运维

在 CentOS 上使用 Jenkins 配置代理(Proxy),通常有两种常见场景,我先说明,再给具体做法:


一、Jenkins 通过代理访问外网(HTTP/HTTPS Proxy)

适用于:

  • Jenkins 服务器在内网
  • 需要通过公司代理访问互联网(如下载插件、拉取代码)

方法 1:通过 Jenkins Web 界面配置(推荐)

  1. 登录 Jenkins
  2. 进入
    Manage Jenkins → System
  3. 找到 HTTP Proxy Configuration
  4. 填写:
    • Proxy Host
    • Proxy Port
    • 如有账号:
      • Proxy UserName
      • Proxy Password
  5. 可设置 No Proxy Host(如 localhost,127.0.0.1)
  6. 保存

方法 2:修改 Jenkins 启动参数(CentOS 系统)

适用于 yum / systemd 安装的 Jenkins

1. 编辑配置文件

sudo vi /etc/sysconfig/jenkins

2. 增加 JVM 代理参数

JENKINS_JAVA_OPTIONS="-Dhttp.proxyHost=proxy.example.com \
-Dhttp.proxyPort=8080 \
-Dhttps.proxyHost=proxy.example.com \
-Dhttps.proxyPort=8080 \
-Dhttp.nonProxyHosts=localhost|127.0.0.1"

3. 重启 Jenkins

sudo systemctl restart jenkins

方法 3:只代理 git / maven / npm(不代理 Jenkins)

Git

git config --global http.proxy http://proxy:8080
git config --global https.proxy http://proxy:8080

Maven(settings.xml)

<proxy>
  <id>proxy</id>
  <active>true</active>
  <protocol>http</protocol>
  <host>proxy.example.com</host>
  <port>8080</port>
</proxy>

二、Jenkins Agent(节点)通过代理连接主控(反向代理)

适用于:

  • Jenkins Master 在外网
  • Agent 在内网
  • 使用 Nginx 反向代理 Jenkins

Nginx 示例(CentOS)

server {
    listen 80;
    server_name jenkins.example.com;

    location / {
        proxy_pass http://127.0.0.1:8080;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
}

然后:

systemctl restart nginx

三、常见问题排查

  • 插件无法下载 → 检查 Proxy 是否填错
  • Agent 无法连接 → 检查防火墙 / 端口
  • 代理账号有特殊字符 → URL 编码

如果你能具体说明:

  • Jenkins 版本
  • CentOS 版本
  • 是 出网代理 还是 Agent 连接代理
  • 使用 yum 还是 docker 安装

我可以给你更精确的配置示例。

0 踩