內網服務器通過代理服務器訪問外網

背景

首先有兩臺服務器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]
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章