温馨提示×

ubuntu jenkins怎样配置 nginx

小樊
37
2026-10-13 01:51:47
栏目: 智能运维

在 Ubuntu 上把 Jenkins 放到 Nginx 后面(反向代理)是常见做法,常用于:

  • 用 80/443 端口访问 Jenkins
  • 启用 HTTPS
  • 绑定域名(如 jenkins.example.com)

下面是从 0 到可用 的标准配置流程。


一、前提条件

1. 已安装 Jenkins

sudo apt update
sudo apt install jenkins

Jenkins 默认监听:

http://localhost:8080

2. 已安装 Nginx

sudo apt install nginx

二、Nginx 反向代理配置(HTTP)

1. 创建 Nginx 配置文件

sudo nano /etc/nginx/sites-available/jenkins

2. 写入以下配置

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

    location / {
        proxy_pass         http://127.0.0.1:8080;
        proxy_read_timeout 60s;

        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;
    }
}

⚠️ server_name 改成你的域名或服务器 IP

3. 启用站点

sudo ln -s /etc/nginx/sites-available/jenkins /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl reload nginx

三、Jenkins 关键配置(非常重要)

1. 修改 Jenkins 启动参数

编辑:

sudo nano /etc/default/jenkins

找到 JAVA_ARGS,添加:

JAVA_ARGS="-Djenkins.model.JenkinsLocationConfiguration.url=http://jenkins.example.com/"

或更常见方式(系统属性):

JAVA_ARGS="-Djenkins.install.runSetupWizard=false"

如果你使用 systemd(Ubuntu 16.04+),改这个文件:

sudo systemctl edit jenkins

写入:

[Service]
Environment="JENKINS_ARGS=--httpListenAddress=127.0.0.1 --httpPort=8080"

2. 重启 Jenkins

sudo systemctl restart jenkins

四、配置 HTTPS(推荐)

1. 安装 Certbot

sudo apt install certbot python3-certbot-nginx

2. 自动申请并配置 SSL

sudo certbot --nginx -d jenkins.example.com

Certbot 会自动修改 Nginx 配置,加上:

  • 80 → 443 跳转
  • SSL 证书
  • 安全 Header

五、Jenkins Web 设置(UI 中)

登录 Jenkins:

http://jenkins.example.com

进入:

Manage Jenkins → System

设置:

  • Jenkins URL
https://jenkins.example.com/

✅ 保存


六、常见问题排查

1. 页面能打开但一直加载

检查:

proxy_set_header X-Forwarded-Proto $scheme;

必须有(HTTPS 下尤其重要)


2. WebSocket 失败(Blue Ocean / 构建日志)

Nginx 需支持 WebSocket:

location / {
    proxy_pass http://127.0.0.1:8080;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
}

3. 502 Bad Gateway

检查:

systemctl status jenkins
curl http://127.0.0.1:8080

七、最小可用 Nginx 模板(含 HTTPS + WebSocket)

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/letsencrypt/live/jenkins.example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/jenkins.example.com/privkey.pem;

    location / {
        proxy_pass http://127.0.0.1:8080;
        proxy_http_version 1.1;
        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 https;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
    }
}

如果你愿意,可以告诉我:

  • Ubuntu 版本
  • Jenkins 安装方式(apt / docker)
  • 是否用 Docker + Nginx

我可以给你 完全对应的配置。

0 踩