内网服务器通过代理服务器访问外网

背景

首先有两台服务器183和184,其中184无外网,183有外网。现在184服务器是无法访问外网的,需要使用代理的方法让184通过有外网的183访问外网。

该操作一共可以分为两个部分:一、配置184代理 二、在服务器183上安装代理程序。

配置184代理

直接配置环境变量即可,可以在/etc/profile下配置:
在etc/profile中添加以下内容:

#IP为要连接的代理服务器183,端口是要代理的端口,如下的意思该服务器要通过192.168.1.183服务器的端口19191的代理来访问外网
export http_proxy=http://192.168.1.183:19191
#如果要设置https代理,应该添加如下配置,暂未尝试过
#export https_proxy=http://192.168.1.183:19191
#设置不代理的IP或者网址,如下配置,这些请求不会被代理,不支持模糊匹配
export no_proxy="127.0.0.1, localhost, 192.168.1.184,192.168.1.183"

然后source /etc/priofile,再使用指令echo $http_proxy,如果能打印出相关代理信息说明操作成功。

[root@cdh02 etc] echo $http_proxy
http://192.168.1.183:19191

在服务器183上安装代理程序

使用squid

#安装squid,这里采用yum的安装方式
[root@cdh01 ~] yum install -y squid
#cd到配置文件目录下
[root@cdh01 ~] cd  /etc/squid/
#备份原始配置文件
[root@cdh01 ~] cp squid.conf squid.conf_bak
#修改配置文件
[root@cdh01 squid]# vi squid.conf

squid.conf配置文件修改内容如下:

#将http_access deny all注释修改为http_access allow all
#http_access deny all
http_access allow all
# 修改端口为代理的端口
http_port 19191

启动squid:

#检查语法是否错误
[root@cdh01 squid] squid -k parse
#初始化缓存空间
[root@cdh01 squid] squid -z
[root@cdh01 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@cdh01 squid] service squid start
Redirecting to /bin/systemctl start squid.service
#检查端口是否开启成功
[root@cdh01 squid]# netstat -an | grep 19191
tcp6       0      0 :::10991                :::*                    LISTEN     

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

[root@cdh01 ~] 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]
    原文作者:我在北国不背锅
    原文地址: https://blog.csdn.net/weixin_44455388/article/details/106299191
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞