CentOS 上 Jenkins 网络配置注意事项
一 基础连通性与 DNS
- 确认服务器具备外网访问能力,先用 ping 测试连通性,例如:ping www.baidu.com。
- 若域名解析失败,检查 /etc/resolv.conf,添加可靠 DNS,例如:nameserver 8.8.8.8、nameserver 8.8.4.4。
- 在云环境需同步开放云厂商安全组/ACL 对应端口,避免仅在本机防火墙放行仍无法访问。
二 防火墙与端口策略
- 查看防火墙状态:systemctl status firewalld。
- 放行 Jenkins 端口(默认 8080/TCP):
- 放行指定端口:firewall-cmd --zone=public --add-port=8080/tcp --permanent
- 或放行服务(若已启用 http/https 服务):firewall-cmd --permanent --add-service=http、firewall-cmd --permanent --add-service=https
- 使配置生效:firewall-cmd --reload
- 不建议长期关闭防火墙;如用于排障可临时关闭,验证后务必恢复。
三 监听地址与端口配置
- 修改监听地址以允许外部访问:编辑 /etc/sysconfig/jenkins,设置 JENKINS_LISTEN_ADDRESS=0.0.0.0(或设置 Jenkins.model.Jenkins.bindAddress=0.0.0.0)。
- 修改默认端口:在 /etc/sysconfig/jenkins 中设置 JENKINS_PORT=8080(或自定义端口),随后重启服务。
- 变更端口后需同步在防火墙放行新端口,并重启 Jenkins 生效。
四 反向代理与 HTTPS 部署
- 使用 Nginx 反向代理示例:
- 安装:yum install -y epel-release nginx && yum install -y nginx
- 配置 /etc/nginx/conf.d/jenkins.conf:
- 监听 80,反向代理到 http://127.0.0.1:8080
- 设置请求头:Host $host; X-Real-IP $remote_addr; X-Forwarded-For $proxy_add_x_forwarded_for; X-Forwarded-Proto $scheme;
- 启动:systemctl restart nginx
- 启用 HTTPS(两种方式):
- 方式 A(推荐):由 Nginx 终止 TLS,对外提供 443,后端 Jenkins 仍用 8080。
- 方式 B:在 Jenkins 内启用 HTTPS/8443,示例(systemd 环境):
- 生成 PKCS#12 证书:openssl pkcs12 -inkey key.pem -in jenkins.pem -export -out jenkins.p12
- 创建覆盖配置 /etc/systemd/system/jenkins.service.d/override.conf:
- Environment=“JENKINS_HTTPS_PORT=8443”
- Environment=“JENKINS_HTTPS_KEYSTORE=/var/lib/jenkins/jenkins.p12”
- Environment=“JENKINS_HTTPS_KEYSTORE_PASSWORD=your_password”
- 生效并重启:systemctl daemon-reload && systemctl restart jenkins
- 如经代理访问,请在 Jenkins 管理界面正确设置 Jenkins URL 与 反向代理相关头部,确保生成的链接与重定向正常。
五 名称解析、代理与性能优化
- 内网/受限网络需要设置 HTTP/HTTPS 代理 供 Jenkins 下载插件与依赖:
- 全局环境变量:export http_proxy=http://proxy_ip:port、export https_proxy=http://proxy_ip:port;必要时配置 no_proxy。
- 插件安装加速:更换为国内镜像源(如清华源),提升下载速度与成功率。
- 性能与稳定性:
- 内核网络参数:在 /etc/sysctl.conf 调整如 net.core.rmem_max、net.core.wmem_max 等缓冲区。
- 文件描述符限制:在 /etc/security/limits.conf 提升 nofile 限制以支持更多并发连接。
- 必要时使用 tc 做流量控制,避免构建/拉取占满带宽。
- 构建节点与跨网访问:通过 “管理 Jenkins → 管理节点” 添加 代理节点,分担负载并优化跨机房/跨地域任务执行。