Jquery中文网 www.jquerycn.cn
Jquery中文网 >  服务器技术  >  应用加速  >  正文 squid反向代理服务器加速web

squid反向代理服务器加速web

发布时间:2014-08-14   编辑:www.jquerycn.cn
squid反向代理服务器加速web
一、实验环境
(1)两台linux主机(Red Hat Enterprise Linux),分别是RHEL1和RHEL2,squid代理服务器和DNS服务器在RHEL2上,apache服务器在RHEL1上
RHEL1(内网)上

IP 网关 域名
192.168.1.51/24(eth0) 192.168.1.1 
10.1.1.2/24(eth0:1) 10.1.1.1 Web1.xxx.com
10.1.1.3/24(eth0:2) 10.1.1.1 Web2.xxx.com
10.1.1.4/24(eth0:3) 10.1.1.1 Web3.xxx.com
RHEL2(squid代理服务器)
外网网卡eth0 192.168.1.52/24
内网网卡eth1 10.1.1.1/24
(2)Win7本地主机:
IP:192.168.1.104/24   gateway:192.168.1.1
(3)浏览器
Firefox 4.1
 
二、配置网卡的IP
两台linux的网卡eth0上的IP事先已配好,且已设置开机启动此处略
[root@RHEL1 etc]# ifconfig eth0:1 10.1.1.2 netmask 255.255.255.0 up
[root@RHEL1 etc]# ifconfig eth0:2 10.1.1.3 netmask 255.255.255.0 up
[root@RHEL1 etc]# ifconfig eth0:3 10.1.1.4 netmask 255.255.255.0 up
[root@RHEL2 squid]# ifconfig eth1 10.1.1.1 netmask 255.255.255.0 up
其他网卡开机启动的设置
(1)RHEL1上的eth0:1、eth0:2、eth0:3
[root@RHEL1 conf]# cd /etc/sysconfig/network-scripts/
[root@RHEL1 network-scripts]# cp -p ifcfg-eth0 ifcfg-eth0:1
[root@RHEL1 network-scripts]# cp -p ifcfg-eth0 ifcfg-eth0:2
[root@RHEL1 network-scripts]# cp -p ifcfg-eth0 ifcfg-eth0:3
[root@RHEL1 network-scripts]# vi ifcfg-eth0:1
修改为以下内容
复制代码 代码如下:
DEVICE=eth0:1
BOOTPROTO=static
ONBOOT=yes
IPADDR=10.1.1.2
NETMASK=255.255.255.0
GATEWAY=10.1.1.1
 
eth0:2、eth0:3仿照进行配置
(2)RHEL2上的eth1
[root@RHEL2 squid]# cd /etc/sysconfig/network-scripts/
[root@RHEL2 network-scripts]# vi ifcfg-eth1
修改为以下内容
复制代码 代码如下:
DEVICE=eth1
BOOTPROTO=static
ONBOOT=yes
IPADDR=10.1.1.1
NETMASK=255.255.255.0
GATEWAY=10.1.1.1
HWADDR=00:0c:29:4c:81:11

三、创建xxx.com的正向区域
DNS服务器的安装此处省略!
[root@RHEL2 ~]# cd /var/named/chroot/etc
[root@RHEL2 etc]# vi named.rfc1912.zones
增加以下区域:
复制代码 代码如下:
zone "xxx.com" IN {
        type master;
        file "xxx.com.hosts";
        allow-update { none; };
};

创建xxx.com的正向解析区域数据库文件
[root@RHEL2 etc]# cd /var/named/chroot/var/named
[root@RHEL2 named]# cp -p localhost.zone xxx.com.hosts
[root@RHEL2 named]# vi xxx.com.hosts
编辑为以下内容:
复制代码 代码如下:
$TTL    86400
@               IN SOA  ns.xxx.com.       root.xxx.com. (
                                        42              ; serial (d. adams)
                                        3H              ; refresh
                                        15M             ; retry
                                        1W              ; expiry
                                        1D )            ; minimum
                IN NS           ns.xxx.com.
ns.xxx.com.   IN A            10.1.1.1
www.xxx.com.   IN A     10.1.1.2
    IN A      10.1.1.3
    IN A      10.1.1.4
web1.xxx.com.  IN A           10.1.1.2
web2.xxx.com.  IN A           10.1.1.3
web3.xxx.com.  IN A           10.1.1.4

保存退出,重启DNS
[root@RHEL2 named]# service named restart
修改DNS的客户端配置文件
[root@RHEL2 named]# vi /etc/resolv.conf
在末尾添加:(若之前其中有其他域的DNS,不用覆盖,不影响效果)
search  xxx.com
nameserver 10.1.1.1
 
四、安装配置squid服务器
在RHEL2上进行
[root@RHEL2 named]# mount /dev/hdc /mnt/cdrom
mount: block device /dev/hdc is write-protected, mounting read-only
[root@RHEL2 named]# cd /mnt/cdrom/Server
[root@RHEL2 Server]# find -name "squid*"
./squid-2.6.STABLE21-3.el5.i386.rpm
[root@RHEL2Server]# rpm -ivh squid-2.6.STABLE21-3.el5.i386.rpm
warning: squid-2.6.STABLE21-3.el5.i386.rpm: Header V3 DSA signature: NOKEY, key ID 37017186
Preparing...                ########################################### [100%]
   1:squid                  ########################################### [100%]
安装成功,下面开始配置
[root@RHEL2 Server]# cd /etc/squid
[root@RHEL2 squid]# cp -p squid.conf squid.conf.bak
[root@RHEL2 squid]# rm -rf squid.conf
[root@RHEL2 squid]# vi squid.conf
添加以下配置信息
复制代码 代码如下:
http_port 80 accel vhost vport
cache_dir ufs /var/spool/squid 100 16 256
cache_mem   32 MB
cache_swap_low  90
cache_swap_high  95
cache_access_log /var/log/squid/access.log
cache_effective_user squid
cache_effective_group squid
acl all src 0.0.0.0/0.0.0.0
acl localhost src 127.0.0.1/255.255.255.255
http_access allow localhost
http_access allow all
http_reply_access allow all
coredump_dir /var/spool/squid
visible_hostname  www.xxx.com
cache_peer 10.1.1.2 parent 80 0  no-query originserver weight=1 name=web1
cache_peer 10.1.1.3 parent 80 0  no-query originserver weight=1 name=web2
cache_peer 10.1.1.4 parent 80 0  no-query originserver weight=1 name=web3
cache_peer_domain web1 www.xxx.com
cache_peer_domain web2 www.xxx.com
cache_peer_domain web3 www.xxx.com
cache_peer_access web1 allow all
cache_peer_access web2 allow all
cache_peer_access web3 allow all
 
保存退出
创建squid的缓存目录,并重启squid,设置开机启动,修改DNS客户端配置文件
[root@RHEL2squid]# squid –z
[root@RHEL2 squid]# service squid restart
[root@RHEL2 squid]# ntsysv
[root@RHEL2 etc]# vi resolv.conf
更改缓存目录的文件和目录所有者以及权限
[root@RHEL2 spool]# chown -R squid.squid squid
[root@RHEL2 spool]# chmod 777 squid
 
五、安装配置apache
[root@RHEL1 squid]# cd /mnt/cdrom/Server
[root@RHEL1 Server]# find -name "httpd*"
./httpd-2.2.3-22.el5.i386.rpm
./httpd-devel-2.2.3-22.el5.i386.rpm
./httpd-manual-2.2.3-22.el5.i386.rpm
[root@RHEL1 Server]# rpm -ivh httpd-2.2.3-22.el5.i386.rpm
warning: httpd-2.2.3-22.el5.i386.rpm: Header V3 DSA signature: NOKEY, key ID 37017186
error: Failed dependencies:
        libapr-1.so.0 is needed by httpd-2.2.3-22.el5.i386
        libaprutil-1.so.0 is needed by httpd-2.2.3-22.el5.i386
有包的依赖关系,依次安装下面几个包
[root@RHEL1 Server]# rpm -ivh apr-1.2.7-11.i386.rpm
[root@RHEL1 Server]# rpm -ivh postgresql-libs-8.1.11-1.el5_1.1.i386.rpm
[root@RHEL1 Server]# rpm -ivh apr-util-1.2.7-7.el5.i386.rpm
[root@RHEL1 Server]# rpm -ivh httpd-2.2.3-22.el5.i386.rpm
安装完毕,设置开机启动
[root@RHEL1 Server]# ntsysv
配置
[root@RHEL1 etc]# cd /etc/httpd/conf/
[root@RHEL1 conf]# ll
总计 52
-rw-r--r-- 1 root root 33726 2008-11-12 httpd.conf
-rw-r--r-- 1 root root 12958 2008-11-12 magic
[root@RHEL1 conf]# cp -p httpd.conf httpd.conf.bak
[root@RHEL1 conf]# vi httpd.conf
(1)找到# ServerName new.host.name:80这一行修改为:
ServerName 10.1.1.1:80(这里更改的是apache默认站点的名称和端口号,很重要的一步,缺少它最后解析不出来)
(2)在配置文件末尾添加以下内容(三个基于IP的虚拟主机)
复制代码 代码如下:
<VirtualHost 10.1.1.2:80>
    ServerAdmin webmaster@dummy-host.example.com
    DocumentRoot /www/web1
    ServerName 10.1.1.2
    ErrorLog logs/dummy-host.example.com-error_log
    CustomLog logs/dummy-host.example.com-access_log common
</VirtualHost>
<VirtualHost 10.1.1.3:80>
    ServerAdmin webmaster@dummy-host.example.com
    DocumentRoot /www/web2
    ServerName 10.1.1.3
    ErrorLog logs/dummy-host.example.com-error_log
    CustomLog logs/dummy-host.example.com-access_log common
</VirtualHost>
<VirtualHost 10.1.1.4:80>
    ServerAdmin webmaster@dummy-host.example.com
    DocumentRoot /www/web3
    ServerName dummy-host.example.com
    ErrorLog logs/dummy-host.example.com-error_log
    CustomLog logs/dummy-host.example.com-access_log common
</VirtualHost>
 
保存退出,创建虚拟主机的文档根目录及三个网站的相同的页面index.html
[root@RHEL1 conf]# mkdir -p /www/web1
[root@RHEL1 conf]# mkdir -p /www/web2
[root@RHEL1 conf]# mkdir -p /www/web3
[root@RHEL1 conf]# vi /www/web1/index.html
输入:这是网站的测试页面!(测试信息)
[root@RHEL1 conf]# cp -p /www/web1/index.html /www/web2/index.html
[root@RHEL1 conf]# cp -p /www/web1/index.html /www/web2/index.html
[root@RHEL1 conf]# cp -p /www/web1/index.html /www/web3/index.html
重启httpd
[root@RHEL1 conf]# service httpd restart
修改RHEL1上DNS的客户端配置文件
[root@RHEL1 named]# vi /etc/resolv.conf
在末尾添加:(若之前其中有其他域的DNS,不用覆盖,不影响效果)
search  xxx.com
nameserver 10.1.1.1

六、激活squid服务器上的路由转发功能
[root@RHEL2 ~]# echo 1 > /proc/sys/net/ipv4/ip_forward
写入开机启动
[root@RHEL2 ~]# vi /etc/rc.d/rc.local
编辑/etc/rc.d/rc.local在后面写上上面一句

七、测试
(1)在win7上打开C:windowssystem32driversetchosts
添加以下一条主机记录:
192.168.1.52    www.xxx.com
(2)把win7的首选DNS改为RHEL2上squid服务器的外网IP,即192.168.1.52
 
(3)连通性测试
在RHEL1上:
第一次ping www.xxx.com
[root@RHEL1 ~]# ping www.xxx.com
PING www.xxx.com (10.1.1.2) 56(84) bytes of data.
64 bytes from 10.1.1.2: icmp_seq=1 ttl=64 time=0.594 ms
64 bytes from 10.1.1.2: icmp_seq=2 ttl=64 time=0.069 ms
第二次ping www.xxx.com
[root@RHEL1 ~]# ping www.xxx.com
PING www.xxx.com (10.1.1.4) 56(84) bytes of data.
64 bytes from 10.1.1.4: icmp_seq=1 ttl=64 time=0.108 ms
64 bytes from 10.1.1.4: icmp_seq=2 ttl=64 time=0.039 ms
第三次ping www.xxx.com
[root@RHEL1 ~]# ping www.xxx.com
PING www.xxx.com (10.1.1.3) 56(84) bytes of data.
64 bytes from 10.1.1.3: icmp_seq=1 ttl=64 time=0.049 ms
64 bytes from 10.1.1.3: icmp_seq=2 ttl=64 time=0.039 ms
64 bytes from 10.1.1.3: icmp_seq=3 ttl=64 time=0.041 ms
通过连续三次ping,发现返回的IP都不一样,由此可知通过DNS的轮询配置实现了负载均衡(连续三次访问web,squid分别把请求送给了三台web,依次循环往复,减少了一台web的压力)
在RHEL2上同样可以得出上述结果!
在win7上:
C:Usersguoyin>ping www.xxx.com
正在 Ping www.xxx.com [192.168.1.52] 具有 32 字节的数据:
来自 192.168.1.52 的回复: 字节=32 时间=5ms TTL=64
来自 192.168.1.52 的回复: 字节=32 时间<1ms TTL=64
来自 192.168.1.52 的回复: 字节=32 时间<1ms TTL=64
来自 192.168.1.52 的回复: 字节=32 时间<1ms TTL=64
C:Usersguoyin>ping web1.xxx.com
正在 Ping web1.xxx.com [10.1.1.2] 具有 32 字节的数据:
请求超时。
请求超时。
请求超时。
说明:在外网上ping www.xxx.com时,返回的IP是squid服务器的外网IP,而直接ping内网的web服务器时ping不通。由此可知隐藏了内网的web站点,实现了代理。
(4)在浏览器上测试
 
至此,测试成功。说明squid服务器成功搭建,能够实现对内网web加速!

您可能感兴趣的文章:
清除代理服务器squid上的cache记录
squid反向代理配置(web服务器的前端内容缓存器)
squid2.6反向代理实验模型
决定squid反向代理的性能要素
squid2.5配置详解+认证
squid2.6透明代理配置一例
squid代理配置一例
squid3.x透明代理配置详解
squid2.5安装与配置详解
squid反向代理服务器加速web

[关闭]