一个专业运
维技术分享!

局域网服务器如何设置代理访问公网

背景

首先有两台服务器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@ali2 /] cd /etc/profile.d/
[root@ali2 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/profile,再使用指令echo $http_proxy,如果能打印出相关代理信息说明操作成功。

[root@ali2 profile.d] echo $http_proxy
http://172.19.58.201:10991

在服务器B上安装代理程序
方式一:使用squid

#安装squid,这里采用yum的安装方式
[root@ali1 ~] yum install -y squid
#cd到配置文件目录下
[root@ali1 ~] cd /etc/squid/
#备份原始配置文件
[root@ali1 ~] cp squid.conf squid.conf_bak
#修改配置文件
[root@ali1 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@ali1 squid] squid -k parse
#初始化缓存空间
[root@ali1 squid] squid -z
[root@ali1 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@ali1 squid] service squid start
Redirecting to /bin/systemctl start squid.service
#检查端口是否开启成功
[root@ali1 squid]# netstat -an | grep 10991
tcp6 0 0 :::10991 :::* LISTEN

测试,使用wget baidu.com或curl www.baidu.com 测试内网服务器能否访问外网

[root@ali2 ~] wget baidu.com
--2018-12-05 14:07:48-- http://baidu.com/
Connecting to 172.19.58.201:10991... connected.
Proxy request sent, awaiting response... 200 OK
Length: 81 [text/html]
Saving to: ‘index.html.1’
100%[=================================================================================================================================>] 81 --.-K/s in 0s 
2018-12-05 14:07:48 (20.7 MB/s) - ‘index.html.1’ saved [81/81]

方式二:使用nginx

暂未写
步骤总结

服务器A

cd /etc/profile.d/
vi proxy.sh
#####################
export http_proxy=http://172.19.58.201:10991
export no_proxy="127.0.0.1, localhost, 172.19.58.202,172.19.58.201"
#####################
source /etc/profile

服务器B(squid方法)

yum install -y squid
cd /etc/squid/
cp squid.conf squid.conf_bak
vi squid.conf
#########################################
#将http_access deny all注释修改为http_access allow all,如果觉得不安全可以自行搜索配置白名单
#http_access deny all
http_access allow all
# 修改端口为代理的端口
http_port 10991
#############################################
service squid start
结束:开始测试

#因为只设置了http代理所以ping是不起作用的
curl www.baidu.com


微信扫描下方的二维码阅读本文

赞(1) 打赏
本站资源仅供个人学习交流,请于下载后24小时内删除,不允许用于商业用途,否则法律问题自行承担。小柳实验室 » 局域网服务器如何设置代理访问公网

相关推荐

  • 暂无文章

觉得文章有用就打赏一下文章作者

非常感谢你的打赏,我们将继续给力更多优质内容,让我们一起创建更加美好的网络世界!

支付宝扫一扫打赏

微信扫一扫打赏