0


使用nginx做代理软件源(yum/apt)仓库的步骤

使用nginx做代理网络软件源【yum/apt】仓库的步骤

运行环境

nginx是唯一具有公网的机器,通过nginx的反向代理使与nginx互通的内网机器可以使用yum仓库
实际环境,nginx具有两个网卡,一个公网,一个内网,如内网机器只具有只有一个内网网卡,与nginx内网在同一网段。

配置方法

nginx配置yum代理的方法相对简单,只需要配置一个nginx反向代理和在内网的机器上配置nginx的地址即可。
1.nginx配置,在nginx服务器上配置

[root@xxx conf.d]# cat package.conf 
server {
resolver 114.114.114.114;
listen 192.168.6.9:6688;
location / {
proxy_pass http://$http_host$request_uri;
}
}

这是一个简单的nginx反向代理配置
下面是对这个配置文件中各个部分的详细解释:
resolver 114.114.114.114;:这一行指定了Nginx使用的DNS解析器的地址。在这个配置中,Nginx将使用114.114.114.114作为DNS解析器来解析域名。
listen 192.168.6.9:6688;:这一行指定了Nginx监听的网络接口和端口号。在这个配置中,Nginx将监听在IP地址为192.168.6.9,端口号为6688的网络接口上。
location / { … }:这一部分定义了一个location块,它用于匹配所有传入的HTTP请求。
proxy_pass http://$http_host$request_uri;:这一行指定了Nginx将传入的HTTP请求转发到目标服务器的地址。在这个例子中,Nginx将使用请求中的Host头和请求URI来构造目标服务器的地址,并将请求转发到该地址。
总之,这个Nginx配置文件定义了一个简单的HTTP代理服务器,它可以将传入的HTTP请求转发到目标服务器

2.软件源代理的配置

2.1. 针对red-hat/centos系统:

  • 2.1.1.yum代理配置,这个配置在需要使用yum仓库的机器上,需要配置有两项 修改/etc/yum.conf这个文件,文件内容:
[main]
proxy=http://192.168.6.9:6688

这一项的配置就会让yum通过nginx代理的端口去访问网络

  • 2.1.2.repo文件配置,位置在/etc/yum.repos.d/下,这个文件根据系统进行配置,centos,openEuler,Ubuntu等系统使用公网的yum仓库不一致,百度搜索下就可以,我使用的是阿里云的centos的yum仓库,如下:
# 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

2.2.针对ubuntu系统

  • 2.2.1. apt代理的配置 配置文件路径:/etc/apt/apt.conf 在文件中添加以下内容
Acquire::http::Proxy "http://yourproxyaddress:proxyport/";

替换"yourproxyaddress"和"proxyport"为实际的代理地址和端口号。

和centos系统不同,Ubuntu系统的包管理器使用的是apt,“yum"的全称是"Yellowdog Updater Modified”,它是基于RPM包管理系统的包管理器。它主要用于Red Hat系列操作系统(如CentOS、Fedora等)以及其他一些衍生版。全称"yum"是用于旧版本中的缩写,现在的全称是"DNF(Dandified YUM)"。
“apt"的全称是"Advanced Package Tool”,它是基于Debian/Ubuntu操作系统的包管理器。它使用.deb格式的软件包,并通过"apt-get"和"apt"命令进行操作。除了Debian和Ubuntu主要发行版外,还有很多其他Linux发行版也支持使用apt工具

  • 2.2.2. sources.list文件配置 在Ubuntu系统中,不需要像在CentOS或Red Hat Linux中那样配置yum repo文件,它使用源列表文件来确定从哪里下载软件包。默认情况下,Ubuntu使用的源列表文件位于/etc/apt/sources.list。默认ubantu系统使用的是官方的镜像源,但有时候可能因为网络原因下载慢或者访问不到,可以更换为国内的镜像源,以下是国内常用的镜像源
  • 中科大镜像站:http://mirrors.ustc.edu.cn/ubuntu/> 清华大学镜像站:http://mirrors.tuna.tsinghua.edu.cn/ubuntu/> 阿里云镜像站:http://mirrors.aliyun.com/ubuntu/> 网易镜像站:http://mirrors.163.com/ubuntu/> 你可以根据自己的需要选择一个镜像源并将其添加到

  • 完整配置可以参考以下方法配置
#1.备份原有镜像源
cp /etc/apt/sources.list     /etc/apt/sources.list.backup
#2.修改sources.list中的内容为以下内容
deb http://mirrors.163.com/ubuntu/ focal main restricted universe multiverse
deb http://mirrors.163.com/ubuntu/ focal-security main restricted universe multiverse
deb http://mirrors.163.com/ubuntu/ focal-updates main restricted universe multiverse
deb http://mirrors.163.com/ubuntu/ focal-proposed main restricted universe multiverse
deb http://mirrors.163.com/ubuntu/ focal-backports main restricted universe multiverse
deb-src http://mirrors.163.com/ubuntu/ focal main restricted universe multiverse
deb-src http://mirrors.163.com/ubuntu/ focal-security main restricted universe multiverse
deb-src http://mirrors.163.com/ubuntu/ focal-updates main restricted universe multiverse
deb-src http://mirrors.163.com/ubuntu/ focal-proposed main restricted universe multiverse
deb-src http://mirrors.163.com/ubuntu/ focal-backports main restricted universe multiverse
#3,使用下面的命令更新仓库
apt-get   update

注意事项

  • 1.nginx的selinux要关闭
  • 2.防火墙要针对后端机器放开代理的端口
  • 3.使用nginx代理转发需要开启系统的转发功能,开启方法
# 修改/etc/sysctl.conf文件并添加以下内容:
net.ipv4.ip_forward = 1
net.ipv6.conf.all.forwarding = 1
#在修改完/etc/sysctl.conf文件后,你需要执行sysctl -p命令来应用更改
标签: nginx 运维

本文转载自: https://blog.csdn.net/m0_73698480/article/details/131596155
版权归原作者 爱吃辣确胃不好的江 所有, 如有侵权,请联系我们删除。

“使用nginx做代理软件源(yum/apt)仓库的步骤”的评论:

还没有评论