Nginx反向代理搭建内网Yum仓库全流程
使用背景说明
在企业内网环境中,出于安全考虑,大部分服务器被禁止直接连接Internet。然而,为了保持与外部网络的时间同步或获取必要的软件更新,通常会预留少量具备公网访问权限的节点。本方案的核心思路正是利用这些具备公网能力的服务器充当代理网关,为整个内网构建一个可用的本地Yum软件仓库。
环境准备要求
建议配置内网DNS服务以简化域名解析(非强制,若直接使用IP地址可省略此步)。
需要一台能够正常访问Internet的服务器,标记为服务器A。
所有无法直接访问外网的内网服务器必须能够与服务器A进行网络通信。
Nginx安装步骤
在具备外网访问权限的服务器A上执行安装操作,部署Nginx服务。
yum install nginx
Nginx配置方法
登录服务器A,进入Nginx配置目录并创建相应的代理配置文件。
$ cd /etc/nginx/conf.d
$ vim proxy.conf
server {
listen 80;
#listen [::]:80;
server_name mirrors.yourdomain.com;
index index.html index.htm index.php default.html default.htm default.php;
root /home/wwwroot/html;
location /ubuntu/ {
proxy_pass http://mirrors.aliyun.com/ubuntu/ ;
}
location /centos/ {
proxy_pass http://mirrors.aliyun.com/centos/ ;
}
location /epel/ {
proxy_pass http://mirrors.aliyun.com/epel/ ;
}
}
Yum Repo源配置
修改那些无法连接外网的主机B上的Yum仓库配置文件,使其指向内部代理地址。
$ cat /etc/yum.repos.d/CentOS-7.repo
[base]
name=CentOS-$releasever - Base - mirrors.yourdomain.com
failovermethod=priority
baseurl=http://mirrors.yourdomain.com/centos/$releasever/os/$basearch/
http://mirrors.yourdomain.com/centos/$releasever/os/$basearch/
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=os
gpgcheck=1
gpgkey=http://mirrors.yourdomain.com/centos/RPM-GPG-KEY-CentOS-7
#released updates
[updates]
name=CentOS-$releasever - Updates - mirrors.yourdomain.com
failovermethod=priority
baseurl=http://mirrors.yourdomain.com/centos/$releasever/updates/$basearch/
http://mirrors.yourdomain.com/centos/$releasever/updates/$basearch/
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=updates
gpgcheck=1
gpgkey=http://mirrors.yourdomain.com/centos/RPM-GPG-KEY-CentOS-7
#additional packages that may be useful
[extras]
name=CentOS-$releasever - Extras - mirrors.yourdomain.com
failovermethod=priority
baseurl=http://mirrors.yourdomain.com/centos/$releasever/extras/$basearch/
http://mirrors.yourdomain.com/centos/$releasever/extras/$basearch/
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=extras
gpgcheck=1
gpgkey=http://mirrors.yourdomain.com/centos/RPM-GPG-KEY-CentOS-7
#additional packages that extend functionality of existing packages
[centosplus]
name=CentOS-$releasever - Plus - mirrors.yourdomain.com
failovermethod=priority
baseurl=http://mirrors.yourdomain.com/centos/$releasever/centosplus/$basearch/
http://mirrors.yourdomain.com/centos/$releasever/centosplus/$basearch/
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=centosplus
gpgcheck=1
enabled=0
gpgkey=http://mirrors.yourdomain.com/centos/RPM-GPG-KEY-CentOS-7
#contrib - packages by Centos Users
[contrib]
name=CentOS-$releasever - Contrib - mirrors.yourdomain.com
failovermethod=priority
baseurl=http://mirrors.yourdomain.com/centos/$releasever/contrib/$basearch/
http://mirrors.yourdomain.com/centos/$releasever/contrib/$basearch/
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=contrib
gpgcheck=1
enabled=0
gpgkey=http://mirrors.yourdomain.com/centos/RPM-GPG-KEY-CentOS-7
Hosts文件配置
$ cat /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.1.193 mirrors.yourdomain.com
# 确保A 主机IP 和后面的反向代理地址
检查并配置本地Hosts映射,确保域名解析正确。
ping mirrors.yourdomain.com
#报错 没有到主机的路由
此时查看B主机中的iptables信息,发现无法访问80,可以在最前添加一条规则。
$ iptables -nvL
8155 28M ACCEPT all -- * * 0.0.0.0/0 0.0.0.0/0 ctstate RELATED,ESTABLISHED
0 0 ACCEPT all -- lo * 0.0.0.0/0 0.0.0.0/0
11761 985K INPUT_direct all -- * * 0.0.0.0/0 0.0.0.0/0
11761 985K INPUT_ZONES_SOURCE all -- * * 0.0.0.0/0 0.0.0.0/0
11761 985K INPUT_ZONES all -- * * 0.0.0.0/0 0.0.0.0/0
0 0 DROP all -- * * 0.0.0.0/0 0.0.0.0/0 ctstate INVALID
11756 985K REJECT all -- * * 0.0.0.0/0 0.0.0.0/0 reject-with icmp-host-prohibited
$ iptables -I INPUT -p tcp --dport 80 -j ACCEPT
检查主机B的iptables状态时,可能会发现对80端口的访问被拦截。此时需要在防火墙规则列表的最前方添加一条允许TCP协议80端口入站的规则。
成功性测试
$ yum clean all
$ yum makecache
总结回顾
上述即为基于Nginx反向代理构建内网Yum源的完整实施过程。希望该指南能为读者在内网环境下管理软件包提供有价值的参考。如有任何疑问或需要进一步探讨,欢迎留言交流,感谢支持。