搬瓦工 VPS 搭建 Nexus 制品仓库管理教程
Sonatype Nexus Repository Manager 是业界最广泛使用的制品仓库管理系统,支持 Maven、npm、PyPI、Docker、NuGet、Go、Helm 等 20 多种包格式。Nexus 既可以作为私有制品仓库存储团队的内部包,也可以作为代理仓库缓存公共源的依赖包,加速构建并减少外部网络依赖。本教程将介绍如何在搬瓦工 VPS 上使用 Docker 部署 Nexus OSS(开源版)。部署前请确保已安装好 Docker 和 Docker Compose。
一、Nexus 仓库类型
- Hosted(宿主仓库):存储团队内部发布的私有制品。
- Proxy(代理仓库):代理远程公共仓库(如 Maven Central、npmjs.com),缓存已下载的制品。
- Group(分组仓库):将多个 Hosted 和 Proxy 仓库聚合为一个统一的访问入口。
二、Docker Compose 部署
mkdir -p /opt/nexus && cd /opt/nexus
cat > docker-compose.yml <<'EOF'
version: '3.8'
services:
nexus:
image: sonatype/nexus3:latest
container_name: nexus
restart: always
ports:
- "127.0.0.1:8081:8081"
- "127.0.0.1:8082:8082"
environment:
INSTALL4J_ADD_VM_PARAMS: >-
-Xms1024m -Xmx2048m
-XX:MaxDirectMemorySize=2g
volumes:
- nexus_data:/nexus-data
volumes:
nexus_data:
EOF
docker compose up -d
2.1 获取初始密码
# 等待 Nexus 启动完成(约 2-3 分钟)
docker logs -f nexus 2>&1 | grep "Started Sonatype Nexus"
# 获取初始管理员密码
docker exec nexus cat /nexus-data/admin.password
通过 SSH 隧道或 Nginx 反向代理访问 8081 端口,使用 admin 和获取的密码登录。首次登录后会引导修改密码和配置匿名访问。
三、配置 Maven 仓库
Nexus 默认已创建 Maven 仓库,主要包括:
maven-central:代理 Maven Central 的 Proxy 仓库maven-releases:存储正式发布版本的 Hosted 仓库maven-snapshots:存储快照版本的 Hosted 仓库maven-public:聚合以上仓库的 Group 仓库
# Maven settings.xml 配置
# ~/.m2/settings.xml
<settings>
<mirrors>
<mirror>
<id>nexus</id>
<mirrorOf>*</mirrorOf>
<url>https://nexus.example.com/repository/maven-public/</url>
</mirror>
</mirrors>
<servers>
<server>
<id>nexus-releases</id>
<username>deployer</username>
<password>deployer_pass_2026</password>
</server>
</servers>
</settings>
四、配置 npm 仓库
在 Nexus Web 界面中创建:
- npm (proxy) - 代理 npmjs.org
- npm (hosted) - 私有 npm 包
- npm (group) - 聚合以上两个仓库
# 配置 npm 使用 Nexus 仓库
npm config set registry https://nexus.example.com/repository/npm-group/
# 发布私有包
npm login --registry=https://nexus.example.com/repository/npm-hosted/
npm publish --registry=https://nexus.example.com/repository/npm-hosted/
五、配置 Docker 仓库
# 在 Nexus 中创建 Docker (hosted) 仓库
# HTTP 连接器端口设置为 8082
# Docker 客户端配置
cat > /etc/docker/daemon.json <<'EOF'
{
"insecure-registries": ["nexus.example.com:8082"]
}
EOF
systemctl restart docker
# 推送镜像
docker login nexus.example.com:8082 -u admin
docker tag myapp:latest nexus.example.com:8082/myapp:v1.0
docker push nexus.example.com:8082/myapp:v1.0
六、配置 PyPI 仓库
# pip 配置使用 Nexus 代理
pip install --index-url https://nexus.example.com/repository/pypi-proxy/simple/ package-name
# 或在 pip.conf 中配置
cat > ~/.pip/pip.conf <<'EOF'
[global]
index-url = https://nexus.example.com/repository/pypi-proxy/simple/
trusted-host = nexus.example.com
EOF
七、Nginx 反向代理
cat > /etc/nginx/conf.d/nexus.conf <<'EOF'
server {
listen 443 ssl http2;
server_name nexus.example.com;
ssl_certificate /etc/letsencrypt/live/nexus.example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/nexus.example.com/privkey.pem;
client_max_body_size 1G;
location / {
proxy_pass http://127.0.0.1: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;
}
}
EOF
nginx -t && systemctl reload nginx
八、存储管理与清理
# 在 Nexus Web 界面配置清理策略
# 管理 > 仓库 > 清理策略
# 创建策略:删除 30 天未使用的 SNAPSHOT 版本
# 创建定时任务执行清理
# 管理 > 系统 > 任务 > 创建任务
# 类型: Admin - Compact blob store
# 频率: 每周日凌晨 4 点
九、常见问题
内存不足
Nexus 默认 JVM 堆内存较大,可以调整环境变量中的 -Xms 和 -Xmx 参数。最低建议 1GB 堆内存。
上传制品失败
检查用户权限是否包含对目标仓库的 write 权限,以及 Nginx 的 client_max_body_size 是否足够。
总结
Nexus 是管理各类构建制品的统一平台,Docker 镜像管理也可以使用更专业的 Harbor。代码质量分析可以搭配 SonarQube,构建流程使用 Woodpecker CI 自动化。选购搬瓦工 VPS 请参考 全部方案,购买时使用优惠码 NODESEEK2026 可享受 6.77% 的折扣,购买链接:bwh81.net。