背景
首先有两台服务器10和20,其中10无外网,20有外网。现在10服务器是无法访问外网的,需要使用代理的方法让10通过有外网的20访问外网。
该操作一共可以分为两个部分:
一、配置1代理
二、在服务器20上安装代理程序。
配置10代理
直接配置环境变量即可,可以在/etc/profile下配置:
在etc/profile中添加以下内容:
#IP为要连接的代理服务器20,端口是要代理的端口,如下的意思该服务器要通过192.168.1.20服务器的端口19191的代理来访问外网export http_proxy=http://192.168.1.20:19191#如果要设置https代理,应该添加如下配置,暂未尝试过#export https_proxy=http://192.168.1.20:19191#设置不代理的IP或者网址,如下配置,这些请求不会被代理,不支持模糊匹配export no_proxy="127.0.0.1, localhost, 192.168.1.10,192.168.1.20"
然后source /etc/priofile,再使用指令echo $http_proxy,如果能打印出相关代理信息说明操作成功。
source /etc/profileecho $http_proxy
[root@cdh02 etc] echo $http_proxyhttp://192.168.1.183:19191
在服务器20上安装代理程序
使用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 allhttp_access allow all# 修改端口为代理的端口http_port 19191
启动squid:
#检查语法是否错误[root@cdh01 squid] squid -k parse#初始化缓存空间[root@cdh01 squid] squid -z[root@cdh01 squid] /12/05 13:58:56 kid1| Set Current Directory to /var/spool/squid/12/05 13:58:56 kid1| Creating missing swap directories/12/13:58:56 kid1| No cache_dir stores are configured.#启动squid[root@cdh01 squid] service squid startRedirecting to /bin/systemctl start squid.service#检查端口是否开启成功[root@cdh01 squid]# netstat -an | grep 19191tcp6 00 :::10991:::*LISTEN
测试,使用wget 或curl 测试内网服务器能否访问外网
[root@cdh01 ~] wget ---12-05 14:07:48-- /Connecting to 172.19.58.201:10991... connected.Proxy request sent, awaiting response... 200 OKLength: 81 [text/html]Saving to: ‘index.html.1’100%[=================================================================================================================================>] 81--.-K/s in 0s-12-05 14:07:48 (20.7 MB/s) - ‘index.html.1’ saved [81/81]