title: 配置nginx代理yum源及DNS #标题tags: 配置代理yum源 #标签
date: 2020-05-16
categories: nginx # 分类
需求:内网环境,需要yum安装很多东西,故需要一台可以连接外网的nginx作为代理,内网主机通过nginx代理yum安装包。
代理yum源
环境描述
两台主机,192.168.20.3为内网主机,无法连接外网,需要连接外网进行yum安装。
192.168.20.2为nginx代理,可以连接外网,并可以和内网主机通信。
修改内网主机的yum配置文件
$ tail -1 /etc/yum.conf # 修改yum配置文件,添加该行,指定nginx监听地址
proxy=http://192.168.20.2:1102
# 注,这里仅支持支持端口,而不支持这样的路径匹配,如:http://192.168.20.2:1102/repo
# 备份原有.repo文件
$ mkdir /etc/yum.repos.d/bak
$ mv /etc/yum.repos.d/*.repo /etc/yum.repos.d/bak/
$ ls /etc/yum.repos.d/bak/ # 确认文件已备份
CentOS-Base.repo CentOS-fasttrack.repo CentOS-Vault.repo
CentOS-CR.repo CentOS-Media.repo epel.repo
CentOS-Debuginfo.repo CentOS-Sources.repo
# 编写新的repo文件如下
$ cat /etc/yum.repos.d/Centos-7.repo
# CentOS-Base.repo
#
# The mirror system uses the connecting IP address of the client and the
# update status of each mirror to pick mirrors that are updated to and
# geographically close to the client. You should use this for CentOS updates
# unless you are manually picking other mirrors.
#
# If the mirrorlist= does not work for you, as a fall back you can try the
# remarked out baseurl= line instead.
#
#
[base]
name=CentOS-$releasever - Base - mirrors.aliyun.com
failovermethod=priority
baseurl=http://mirrors.aliyun.com/centos/$releasever/os/$basearch/
http://mirrors.aliyuncs.com/centos/$releasever/os/$basearch/
http://mirrors.cloud.aliyuncs.com/centos/$releasever/os/$basearch/
gpgcheck=1
gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7
#released updates
[updates]
name=CentOS-$releasever - Updates - mirrors.aliyun.com
failovermethod=priority
baseurl=http://mirrors.aliyun.com/centos/$releasever/updates/$basearch/
http://mirrors.aliyuncs.com/centos/$releasever/updates/$basearch/
http://mirrors.cloud.aliyuncs.com/centos/$releasever/updates/$basearch/
gpgcheck=1
gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7
#additional packages that may be useful
[extras]
name=CentOS-$releasever - Extras - mirrors.aliyun.com
failovermethod=priority
baseurl=http://mirrors.aliyun.com/centos/$releasever/extras/$basearch/
http://mirrors.aliyuncs.com/centos/$releasever/extras/$basearch/
http://mirrors.cloud.aliyuncs.com/centos/$releasever/extras/$basearch/
gpgcheck=1
gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7
#additional packages that extend functionality of existing packages
[centosplus]
name=CentOS-$releasever - Plus - mirrors.aliyun.com
failovermethod=priority
baseurl=http://mirrors.aliyun.com/centos/$releasever/centosplus/$basearch/
http://mirrors.aliyuncs.com/centos/$releasever/centosplus/$basearch/
http://mirrors.cloud.aliyuncs.com/centos/$releasever/centosplus/$basearch/
gpgcheck=1
enabled=0
gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7
#contrib - packages by Centos Users
[contrib]
name=CentOS-$releasever - Contrib - mirrors.aliyun.com
failovermethod=priority
baseurl=http://mirrors.aliyun.com/centos/$releasever/contrib/$basearch/
http://mirrors.aliyuncs.com/centos/$releasever/contrib/$basearch/
http://mirrors.cloud.aliyuncs.com/centos/$releasever/contrib/$basearch/
gpgcheck=1
enabled=0
gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7
#[epel]
#name=Extra Packages for Enterprise Linux 7 - $basearch
#baseurl=http://mirrors.aliyun.com/epel/7/$basearch
#failovermethod=priority
#enabled=1
#gpgcheck=0
#gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7
[epel-debuginfo]
name=Extra Packages for Enterprise Linux 7 - $basearch - Debug
baseurl=http://mirrors.aliyun.com/epel/7/$basearch/debug
failovermethod=priority
enabled=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7
gpgcheck=0
[epel-source]
name=Extra Packages for Enterprise Linux 7 - $basearch - Source
baseurl=http://mirrors.aliyun.com/epel/7/SRPMS
failovermethod=priority
enabled=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7
gpgcheck=0
配置nginx代理
修改配置文件
192.168.20.2为nginx代理主机,需修改配置文件。
$ vim /etc/nginx/nginx.conf # 以下只是部分nginx配置文件
server {
listen 1102 default_server; # 修改监听端口和client端指定的一致
location / {
proxy_pass http://mirrors.ustc.edu.cn; # 指定此url
}
}
启动nginx
$ nginx
$ ss -lnpt | grep 1102 # 确定端口在监听
LISTEN 0 128 *:1102 *:* users:(("nginx",pid=2413,fd=6),("nginx",pid=2412,fd=6))
至此,那个纯内网环境的主机即可通过yum来进行安装了。
yum安装测试
$ yum clean all
$ yum makecache
$ yum install nginx
# 如果yum报错,可以尝试下面的指令
$ yum clean all
$ rpm --rebuilddb
代理DNS
nginx配置如下即可充当DNS:
stream {
server {
listen 53 udp reuseport;
proxy_timeout 20s;
proxy_pass 114.114.114.114:53;
}
}
代理mysql等端口
stream {
server {
listen 3306;
proxy_timeout 20s;
proxy_pass 192.168.20.2:3306;
}
}