在 CentOS 上为 Jenkins 配置 SSL(HTTPS),常见有 三种方式,从推荐到简单依次为:
yum install -y nginx
systemctl enable nginx
systemctl start nginx
yum install -y certbot python2-certbot-nginx
certbot --nginx -d jenkins.example.com
或手动指定证书:
/etc/nginx/ssl/jenkins.crt
/etc/nginx/ssl/jenkins.key
编辑:
vi /etc/nginx/conf.d/jenkins.conf
示例配置:
server {
listen 80;
server_name jenkins.example.com;
return 301 https://$host$request_uri;
}
server {
listen 443 ssl;
server_name jenkins.example.com;
ssl_certificate /etc/nginx/ssl/jenkins.crt;
ssl_certificate_key /etc/nginx/ssl/jenkins.key;
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;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
vi /etc/sysconfig/jenkins
修改:
JENKINS_ARGS="--httpListenAddress=127.0.0.1 --httpPort=8080"
重启:
systemctl restart jenkins
systemctl restart nginx
访问:
https://jenkins.example.com
openssl pkcs12 -export \
-in jenkins.crt \
-inkey jenkins.key \
-out jenkins.p12 \
-name jenkins
vi /etc/sysconfig/jenkins
JENKINS_ARGS="--httpsPort=8443 --httpsKeyStore=/path/jenkins.p12 --httpsKeyStorePassword=密码"
重启:
systemctl restart jenkins
⚠️ 缺点:
如果你更熟悉 Apache:
yum install httpd mod_ssl
使用 ProxyPass 做反向代理,配置类似 Nginx。
在 Jenkins → 系统配置 中设置:
Jenkins URL = https://jenkins.example.com/
firewall-cmd --add-port=443/tcp --permanent
firewall-cmd --reload
| 方案 | 推荐度 | 说明 |
|---|---|---|
| Nginx + SSL | ⭐⭐⭐⭐⭐ | 最稳定、最通用 |
| Jenkins 自带 SSL | ⭐⭐ | 仅测试 |
| Apache 反向代理 | ⭐⭐⭐⭐ | 可选 |
如果你愿意,可以告诉我:
我可以直接给你 可用配置文件。