【DevOps】基于Nexus部署内网pypi代理镜像仓库操作手册
下面是使用 Nexus 配置 PyPI 代理和缓存的详细步骤。
Nexus 方案架构
我们将创建三种类型的仓库,并将它们组合成一个 Group 仓库,作为内网用户的唯一入口。
+----------------+ +--------------------------------------------------------+ +--------------------------+
| 内网机器 (无公网) | | Nexus Repository (内网) | | 公网 |
| | | | | |
| pip install |----->| pypi-all-group (Group) | | Aliyun PyPI Mirror |
| (index-url -> | | | | | (mirrors.aliyun.com) |
| nexus group) | | |--> pypi-internal-hosted (Hosted) [用于私有包] | | |
| |<-----| |--> pypi-aliyun-proxy (Proxy) [代理和缓存] ---------->|----->| |
+----------------+ +--------------------------------------------------------+ +--------------------------+
部署步骤(在 Nexus 管理界面操作)
1. 创建 PyPI Proxy 仓库
这是核心,用于代理阿里云 PyPI 镜像并缓存下载的包。
- 登录 Nexus 管理界面。
- 点击顶部菜单栏的齿轮图标 ⚙️(设置)。
- 在左侧菜单中选择 Repository -> Repositories。
- 点击 Create repository 按钮。
- 选择 pypi (proxy)。
- 配置仓库:
- Name:
pypi-proxy
(给一个清晰的名字) - Remote storage: ``https://pypi.tuna.tsinghua.edu.cn`
- Name:
- 点击页面底部的 Create repository。
- 返回列表页,等待Status转换为“Online - Ready to Connect”
2. 创建 PyPI Hosted 仓库 (可选,未部署!)
这个仓库用于存放你们公司内部开发的私有 Python 包。即使现在没有,创建它也是一个好的实践。
- 再次点击 Create repository。
- 选择 pypi (hosted)。
- 配置仓库:
- Name:
pypi-internal-hosted
- Storage: 选择一个 Blob Store。
- Name:
- 点击 Create repository。
3. 创建 PyPI Group 仓库 (统一入口)
这是内网开发者将要使用的统一地址。它会按顺序从其成员仓库中查找包。
- 再次点击 Create repository。
- 选择 pypi (group)。
- 配置仓库:
- Name:
pypi-all-group
- Storage: 选择一个 Blob Store。
- Group: 在左侧的
Available
列表中,将你刚刚创建的两个仓库(pypi-internal-hosted
和pypi-proxy
)移动到右侧的Members
列表中。 - 重要: 调整顺序。将
pypi-internal-hosted
放在pypi-proxy
的上面。这样 Nexus 会优先从你的私有仓库查找包,找不到时再去代理仓库查找。
- Name:
- 点击 Create repository。
现在,你的仓库列表里就有了这三个仓库,其中 pypi-all-group
就是我们需要的最终地址。
4. 配置内网机器的 pip.conf
修改内网机器的 ~/.pip/pip.conf
文件,将 index-url
指向 Nexus 的 Group 仓库。
mkdir ~/.pip
vim ~/.pip/pip.conf
[global]
index-url = http://10.2.0.100:8081/nexus/repository/pypi-proxy/simple[install]
trusted-host = 10.2.0.100
5. 测试和验证
第一次安装(从上游下载并缓存):
在内网机器上执行:
[root@10-2-0.2 ~]# pip install requests
WARNING: Running pip install with root privileges is generally not a good idea. Try `pip install --user` instead.
Looking in indexes: http://10.2.0.100:8081/nexus/repository/pypi-proxy/simple
Collecting requestsDownloading http://10.2.0.100:8081/nexus/repository/pypi-proxy/packages/requests/2.32.5/requests-2.32.5-py3-none-any.whl (64 kB)|████████████████████████████████| 64 kB 4.7 MB/s
Collecting charset_normalizer<4,>=2Downloading http://10.2.0.100:8081/nexus/repository/pypi-proxy/packages/charset-normalizer/3.4.4/charset_normalizer-3.4.4-cp39-cp39-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl (153 kB)|████████████████████████████████| 153 kB 63.5 MB/s
Collecting urllib3<3,>=1.21.1Downloading http://10.2.0.100:8081/nexus/repository/pypi-proxy/packages/urllib3/2.5.0/urllib3-2.5.0-py3-none-any.whl (129 kB)|████████████████████████████████| 129 kB 59.3 MB/s
Requirement already satisfied: idna<4,>=2.5 in /usr/lib/python3.9/site-packages (from requests) (3.2)
Collecting certifi>=2017.4.17Downloading http://10.2.0.100:8081/nexus/repository/pypi-proxy/packages/certifi/2025.10.5/certifi-2025.10.5-py3-none-any.whl (163 kB)|████████████████████████████████| 163 kB 82.4 MB/s
Installing collected packages: urllib3, charset-normalizer, certifi, requests
Successfully installed certifi-2025.10.5 charset-normalizer-3.4.4 requests-2.32.5 urllib3-2.5.0
WARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv
此时,Nexus 的 pypi-aliyun-proxy
仓库会从阿里云下载 requests
包,并缓存到其 Blob Store 中。
验证缓存:
你可以登录 Nexus UI,进入 pypi-aliyun-proxy
仓库的页面,在 Browse
或 Search
组件中搜索 requests
,你就能看到它已经被成功缓存了。
第二次安装(从本地缓存加载):
pip uninstall -y requests
pip install requests
这次安装速度会非常快,因为 pip
直接从 Nexus 的缓存中获取文件,无需访问公网。