一、前言
在 CentOS 系统中,配置 HTTP 代理(http_proxy
)通常是为了使系统或应用程序通过代理服务器进行网络访问。HTTP 代理在网络限制、节省带宽、提高安全性等方面具有重要作用,可以通过该方式使得centos访问外网,原理是将本机中的包转发到对应的代理主机上,由代理主机处理数据包的路由转发
二、配置HTTP 代理
系统代理
这种配置会对所有在用户登录后启动的进程生效,但不会对 systemd 启动的系统服务生效
编辑环境变量文件
vi /etc/profile
#如果需要验证身份则配置以下配置http_proxy = http://username:password@proxyserver:port
export http_proxy="http://proxyserver:port"
export https_proxy="https://proxyserver:port"
#no_proxy:无需代理的主机或域名;可以使用通配符,多个时使用","号分隔
export no_proxy="localhost,127.0.0.1"
执行以下命令使配置生效
source /etc/profile
对特定服务进行代理
systemd 服务在启动时不会读取用户的 profile
文件。因此,需要单独为 systemd 服务配置环境变量
这里使用docker举例
更改service文件
vi /usr/lib/systemd/system/docker.service
[Service]
Type=notify
# the default is not to use systemd for cgroups because the delegate issues still
# exists and systemd currently does not support the cgroup feature set required
# for containers run by docker
Environment='http_proxy=10.1.13.252:7897' #配置网络代理
Environment='https_proxy=10.1.13.252:7897'
Environment='no_proxy=127.0.0.1,10.1.60.0/24' #配置不需要网络代理的网段
重载配置
systemctl daemon-reload
重启docker
systemctl restart docker