您的位置:首页 > 运维架构 > 反向代理

SQUID反向代理

2016-03-28 19:17 507 查看

3 反向代理

3.1 问题

通过配置代理服务器,实现以下目标:代理服务器可以将远程的Web服务器页面缓存在本地

代理服务器端口设置为80端口

用户通过访问代理服务器即可或则远程Web服务器上的页面内容

远程Web服务器对客户端用户是透明的

客户端通过代理访问www.tarena.com和bbs.tarena.com可以获得两台不同后端Web服务器的页面内容

3.2 方案

使用4台RHEL6虚拟机,其中一台作为Squid代理服务器,该服务器用来连接两个网段,因此需要配置两块网卡,地址分别为192.168.4.5和172.16.0.254。一台作为客户端测试主机,IP地址为172.16.0.100。一台Web服务器,地址为192.168.4.205,该Web服务器需要配置虚拟主机,基于www.tarena.com和bbs.tarena.com两个域名的虚拟主机,拓扑如图-6所示。


图-6

3.3 步骤

实现此案例需要按照如下步骤进行。步骤一:构建web服务器1)使用yum安装web软件包[root@svr5 ~]# yum -y install httpd

.. ..

[root@svr5 ~]# rpm -q httpd

httpd-2.2.15-29.el6_4.x86_64

2)修改httpd配置文件,设置虚拟主机功能
[root@svr5 ~]# cat /etc/httpd/conf/httpd.conf

.. ..

NameVirtualHost *:80

<VirtualHost *:80>

DocumentRoot /var/www/web1

ServerName www.tarena.com

CustomLog logs/www.tarena.com-access_log common

</VirtualHost>

<VirtualHost *:80>

DocumentRoot /var/www/web2

ServerName bbs.tarena.com

CustomLog logs/bbs.tarena.com-access_log common

</VirtualHost>

[root@svr5 ~]# mkdir /var/www/{web1,web2}

3)启用httpd服务,并设为开机自动运行
[root@svr5 ~]# service httpd restart ; chkconfig httpd on

Stopping httpd: [FAILED]

Starting httpd: [OK

httpd服务默认通过TCP 80端口监听客户端请求:
3)为Web访问建立测试文件在网站根目录/var/www/{web1,web2}下创建一个名为index.html的首页文件:[root@svr5 ~]# cat /var/www/web1/index.html

<html>

<title>Welcome</title>

<body>

<h1>www.tarena.com</h1>

</body>

</html>

[root@svr5 ~]# cat /var/www/web2/index.html

<html>

<title>Welcome</title>

<body>

<h1>bbs.tarena.com</h1>

</body>

</html>

步骤二:部署Squid代理服务器1)使用yum安装squid软件包:[root@svr5 ~]# yum -y install squid

.. ..

[root@svr5 ~]# rpm -q squid

squid-3.1.10-19.el6_4.x86_64

2)修改/etc/squid/squid.conf配置文件:
[root@svr5 ~]# vim /etc/squid/squid.conf

.. ..

http_port 80 vhost //设置反向代理

visible_hostname squid.svr5.com //设置主机名

cache_peer 172.16.16.254 parent 80 0 originserver name=www

cache_peer 172.16.16.254 parent 80 0 originserver name=bbs

cache_peer_domain bbs bbs.tarena.com

cache_peer_domain www www.tarena.com

cache_mem 128 MB //内存缓存

cache_dir ufs /var/spool/squid 200 16 128 //硬盘缓存

http_access allow localnet //允许本机所在网络中的所有主机使用代理服务器

3)启动squid服务,并设置为开机启动:
[root@svr5 ~]# service squid restart; chkconfing squid on

Stopping squid: [FAILED]

init_cache_dir /var/spool/squid...

Starting squid: . [ OK ]

4)squid服务默认通过TCP 3128端口监听客户端请求:
[root@svr5 ~]# netstat -anptu | grep 80

tcp 0 0 :::80 :::* LISTEN 3213/(squid)

步骤三:客户端测试通过/etc/hosts文件,配置域名解析,将域名解析为Squid服务器IP地址[root@svr5 ~]# cat /etc/hosts

.. ..

192.168.4.5 www.tarena.com bbs.tarena.com

2)客户端开启浏览器,分别访问两个不同的域名站点
[root@svr5 ~]# firefox http://www.tarena.com
[root@svr5 ~]# firefox http://bbs.tarena.com
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: