一、背景
首先有两台服务器AB,其中A无外网,B有外网。A配置(内网IP172.19.58.202\系统CentOS7.4),B配置(内网IP172.19.58.201\系统CentOS7.4)。现在A服务器是无法访问外网的,需要使用代理的方法让A通过有外网的B访问外网。
该操作一共可以分为两个部分:一、配置A代理 二、在服务器B上安装代理程序。
二、配置A代理
① 配置环境变量即可,可以在/etc/profile下配置,这边采用在/etc/profile.d文件夹下配置环境变量。
# cd到/etc/profile.d/目录下
# 新建proxy.sh,命名无所谓
[root@localhost /] cd /etc/profile.d/
[root@localhost profile.d] vi proxy.sh
② proxy.sh里面内容
#IP为要连接的代理服务器B,端口是要代理的端口,如下的意思该服务器要通过172.19.58.201服务器的端口10991的代理来访问外网
export http_proxy=http://172.19.58.201:10991
#如果要设置https代理,应该添加如下配置,暂未尝试过#
export https_proxy=http://172.19.58.201:10991
#设置不代理的IP或者网址,如下配置,这些请求不会被代理,不支持模糊匹配(也可以不要)
export no_proxy="127.0.0.1, localhost, 172.19.58.202,172.19.58.201"
③ 然后source /etc/priofile,再使用指令echo $http_proxy,如果能打印出相关代理信息说明操作成功。
[root@localhost profile.d] echo $http_proxy
http://172.19.58.201:10991
二、在服务器B上安装代理程序(有外网)
①安装squid
#安装squid,这里采用yum的安装方式
[root@localhost ~] yum install -y squid#cd到配置文件目录下
[root@localhost ~] cd /etc/squid/
#备份原始配置文件
[root@localhost ~] cp squid.conf squid.conf_bak
#修改配置文件
[root@localhost squid]# vi squid.conf
② squid.conf配置文件修改内容如下
#将http_access deny all注释修改为http_access allow all#http_access deny all
http_access allow all
# 修改端口为代理的端口
http_port 10991
③ 启动
#检查语法是否错误[root@localhost squid] squid -k parse
#初始化缓存空间[root@localhost squid] squid -z
[root@localhost squid] 2018/12/05 13:58:56 kid1| Set Current Directory to /var/spool/squid
2018/12/05 13:58:56 kid1| Creating missing swap directories
2018/12/13:58:56 kid1| No cache_dir stores are configured.
#启动squid
[root@localhost squid] service squid start
Redirecting to /bin/systemctl start squid.service
#检查端口是否开启成功
[root@localhost squid]# netstat -an | grep 10991
tcp6 0 0 :::10991 :::* LISTEN
④ 测试,使用wget baidu.com或curl www.baidu.com 测试内网服务器能否访问外网
以上就完成了使用squid的方式将无外网的服务器配置为有外网!