1. 为什么需要私有Nexus仓库

在日常开发中,我们经常需要依赖各种第三方库和组件。直接从公共仓库下载不仅速度慢,还存在安全风险。私有Nexus仓库就像是你家里的私人图书馆,所有常用的书籍都整齐摆放,随时取用,既快速又安全。

我遇到过不少团队还在用原始方式管理依赖包,每次新成员加入都要花半天时间配置环境。有了Nexus后,这个问题彻底解决。它不仅支持Maven、npm、Docker等多种格式的制品管理,还能缓存公共仓库的内容,显著提升构建速度。

私有仓库最大的优势在于可控性。你可以精确控制谁可以访问什么内容,审计所有下载记录,甚至设置自动清理策略。对于金融、医疗等对安全性要求高的行业,这是刚需。我在某次安全审计中就靠Nexus的日志功能快速定位了异常下载行为。

2. 环境准备与Docker Compose部署

2.1 基础环境配置

在开始前,确保你的服务器满足这些条件:至少4核CPU、8GB内存(Nexus比较吃资源)、50GB以上磁盘空间。我推荐使用Ubuntu 22.04 LTS或CentOS 7+系统,这些系统对Docker的支持最稳定。

安装Docker和Docker Compose只需几条命令:

# Ubuntu示例
sudo apt update
sudo apt install -y docker.io docker-compose-plugin
sudo systemctl enable --now docker

验证安装是否成功:

docker --version
docker compose version

注意:新版Docker已集成Compose插件,推荐使用docker compose命令而非旧的docker-compose

2.2 编写Compose文件

这是我优化过的docker-compose-nexus.yml,相比基础版本增加了健康检查、资源限制等生产级配置:

version: "3.8"
services:
  nexus:
    container_name: my-nexus
    image: sonatype/nexus3:3.67.1
    hostname: my-nexus
    ports:
      - "8081:8081"
    deploy:
      resources:
        limits:
          cpus: '2'
          memory: 4096M
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:8081"]
      interval: 30s
      timeout: 10s
      retries: 3
    volumes:
      - "/data/nexus:/nexus-data"
      - "/etc/localtime:/etc/localtime:ro"
    restart: unless-stopped

关键配置说明:

  • volumes将容器内的/nexus-data映射到宿主机,确保数据持久化
  • healthcheck自动监控服务状态
  • restart: unless-stopped保证异常退出后自动重启
  • 资源限制防止Nexus占用过多系统资源

2.3 启动与初始化

执行部署命令:

mkdir -p /data/nexus && chown -R 200:200 /data/nexus
docker compose -f docker-compose-nexus.yml up -d

首次启动需要1-2分钟初始化。查看日志确认状态:

docker logs -f my-nexus

当看到"Started Sonatype Nexus"日志时,访问http://服务器IP:8081。初始密码在这里:

cat /data/nexus/admin.password

登录后立即修改密码,建议启用MFA双因素认证。在"Security > Users"中创建专属账号,避免长期使用admin账户。

3. HTTPS安全加固实战

3.1 SSL证书准备

生产环境建议使用Let's Encrypt免费证书或企业购买的商业证书。测试环境可以用OpenSSL自签证书:

mkdir -p /config/nginx/conf.d/certs
openssl req -x509 -nodes -days 365 -newkey rsa:2048 \
  -keyout /config/nginx/conf.d/certs/server.key \
  -out /config/nginx/conf.d/certs/server.crt \
  -subj "/CN=my-nexus.example.com"

将生成的server.crt和server.key放到/config/nginx/conf.d/certs目录。真实环境中记得将CA证书加入系统信任链。

3.2 Nginx反向代理配置

创建docker-compose-nginx.yml:

version: "3.8"
services:
  nginx:
    image: nginx:1.25.3
    container_name: my-nginx
    ports:
      - "443:443"
      - "80:80"
    volumes:
      - "/config/nginx/nginx.conf:/etc/nginx/nginx.conf"
      - "/config/nginx/conf.d:/etc/nginx/conf.d"
      - "/logs/nginx:/var/log/nginx"
    restart: unless-stopped

在/config/nginx/conf.d/nexus.conf中添加:

server {
    listen 80;
    server_name nexus.yourdomain.com;
    return 301 https://$host$request_uri;
}

server {
    listen 443 ssl http2;
    server_name nexus.yourdomain.com;
    
    ssl_certificate /etc/nginx/conf.d/certs/server.crt;
    ssl_certificate_key /etc/nginx/conf.d/certs/server.key;
    ssl_session_timeout 1d;
    ssl_session_cache shared:MozSSL:10m;
    ssl_protocols TLSv1.2 TLSv1.3;
    ssl_ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256;
    ssl_prefer_server_ciphers on;
    
    client_max_body_size 1G;
    
    location / {
        proxy_pass http://my-nexus:8081;
        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;
    }
}

3.3 启动与验证

启动Nginx服务:

docker compose -f docker-compose-nginx.yml up -d

测试HTTPS访问:

curl -vk https://nexus.yourdomain.com

在Nexus的"Security > HTTP"设置中,勾选"Force Base URL"并填写HTTPS地址。这样所有生成的链接都会使用HTTPS协议。

4. 高级配置与优化技巧

4.1 存储空间优化

Nexus默认会不断累积快照版本,需要配置清理策略:

  1. 进入"Repository > Cleanup Policies"
  2. 创建新策略,例如设置保留最近10个快照版本
  3. 在仓库配置中关联清理策略

定期执行压缩存储任务:

docker exec my-nexus find /nexus-data -name "*.repositories" -exec rm {} \;
docker exec my-nexus nexus blobstore compact --blobstore-name default

4.2 安全加固措施

  • 在"Security > Realms"中禁用匿名访问
  • 配置IP白名单限制访问来源
  • 启用内容选择器防止敏感组件被下载
  • 设置自动封锁策略,防止暴力破解

4.3 备份与恢复

创建备份脚本/backup/nexus-backup.sh:

#!/bin/bash
BACKUP_DIR="/backup/nexus-$(date +%Y%m%d)"
mkdir -p $BACKUP_DIR
docker stop my-nexus
tar czf $BACKUP_DIR/nexus-data.tgz -C /data nexus
docker start my-nexus

恢复时只需解压备份文件到/data/nexus目录,然后重启容器即可。

4.4 性能监控

集成Prometheus监控:

  1. 在Nexus的"System > Monitoring"启用Prometheus端点
  2. 配置Prometheus抓取/metrics数据
  3. 使用Grafana展示关键指标:
    • 存储空间使用率
    • 请求响应时间
    • 并发用户数
    • 组件下载频率

5. 常见问题排查

5.1 启动失败排查

如果Nexus无法启动,首先检查:

  • 磁盘空间是否充足:df -h
  • 内存是否足够:free -m
  • 端口是否冲突:netstat -tulnp | grep 8081
  • 查看详细日志:docker logs my-nexus

5.2 性能问题优化

遇到响应慢的情况可以:

  1. 调整JVM参数:编辑/nexus-data/etc/nexus.properties
    nexus.vmoptions=-Xms2g -Xmx2g -XX:MaxDirectMemorySize=2g
    
  2. 增加Docker内存限制到8GB
  3. 使用SSD存储替代机械硬盘

5.3 HTTPS证书问题

浏览器提示证书不安全时:

  • 确保证书CN与访问域名一致
  • 检查证书链是否完整
  • 测试证书有效性:openssl verify -CAfile ca.crt server.crt

我在实际部署中发现,使用Nginx的OCSP Stapling功能可以显著提升HTTPS握手速度。在Nginx配置中添加:

ssl_stapling on;
ssl_stapling_verify on;
ssl_trusted_certificate /etc/nginx/conf.d/certs/ca.crt;

更多推荐