實現過程如下(該配置過程中IP和網關均爲臨時使用,生產中應配置在配置文件中):
1.配置網關服務器(用以模擬ISP)
ifconfig eth0:0 192.168.10.254
ifconfig eth0:1 192.168.20.254
ifconfig eth0:2 192.168.30.254
ifconfig eth0:3 192.168.40.254
ifconfig eth0:4 192.168.50.254
echo 1 > /proc/sys/net/ipv4/ip_forward #開啓網卡轉發,實現數據包的轉發,使該服務器能夠具有路由功能
2.配置DNS服務器
#配置服務器IP和默認網關
ifconfig eth0 192.168.30.11/24
route add default gw 192.168.30.254
#安裝DNS軟件包-bind
yum install bind bind-chroot
#配置DNS主配置文件
#vim /etc/named/chroot/etc/named.conf
- options{
- directory "var/named";
- };
- #創建acl地址池
- acl beijing {
- 192.168.10.0/24;
- # 10.1.0.0/16;
- # 172.16.0.1;
- };
- acl shanghai {
- 192.168.20.0/24;
- };
- 創建試圖,匹配包括在不同地址池中的地址,爲其分配不同的區域配置文件
- view beijing {
- match-clients { beijing; };
- recursion no;
- zone "smileliuyb.com." {
- type master;
- file "beijing.smileliuyb.com.zone";
- };
- };
- view shanghai {
- match-clients { shanghai; };
- recursion no;
- zone "smileliuyb.com." {
- type master;
- file "shanghai.smileliuyb.com.zone";
- };
- };
- view other {
- match-clients { any; };
- recursion no;
- zone "smileliuyb.com." {
- type master;
- file "beijing.smileliuyb.com.zone";
- };
#配置區域文件
#cd /var/named/chroot/var/named/
#cp /usr/share/doc/bind-9.3.6/sample/var/named/named.local beijing.smileliuyb.com.zone
#vim beijing.smileliuyb.com.zone
- $TTL 86400
- @ IN SOA localhost. root.localhost. (
- 1997022700 ; Serial
- 28800 ; Refresh
- 14400 ; Retry
- 3600000 ; Expire
- 86400 ) ; Minimum
- IN NS localhost.
- www.smileliuyb.com. IN A 192.168.40.250
- image.smileliuyb.com. IN A 192.168.40.251
#cp beijing.smileliuyb.com.zone shanghai.smileliuyb.com.zone
#vim shanghai.smileliuyb.com.zone
- $TTL 86400
- @ IN SOA localhost. root.localhost. (
- 1997022700 ; Serial
- 28800 ; Refresh
- 14400 ; Retry
- 3600000 ; Expire
- 86400 ) ; Minimum
- IN NS localhost.
- www.smileliuyb.com. IN A 192.168.40.250
- image.smileliuyb.com. IN A 192.168.50.251
#修改區域配置文件的屬主和屬組
chown named.named *.smileliuyb.com.zone
#重啓DNS服務
/etc/init.d/named restart
3.配置網頁服務器-Apache
#配置服務器IP和網關
ifconfig eth0 192.168.40.250/24
route add default gw 192.168.40.254
#安裝apache服務軟件包
yum install httpd -y
#創建一個測試頁
#vim /var/www/html/index.html
- <html>
- <body>
- <p>
- <img border="0" src="http://image.smileliuyb.com/a.jpg"/>
- </p>
- </body>
- </html>
#重啓apache服務
/etc/init.d/httpd restart
4.配置圖片服務器-nginx
#配置服務器ip和默認網關
ifconfig eth0 192.168.40.251/24
route add default gw 192.168.40.254
#安裝nginx軟件包
rpm -Uvh nginx-~.el5.i386.rpm
#配置nginx服務主配置文件
#vim /etc/nginx/nginx.conf
- server {
- listen 80;
- server_name image.smileliuyb.com;
- location / {
- root /tmp/image;
- # 開啓目錄訪問
- autoindex on;
- }
#重啓nginx服務
/etc/init.d/nginx restart
5.配置圖片緩存服務器-varnish
#配置服務器IP和網關
ifconfig eth0:0 192.168.50.251/24
route add default gw 192.168.50.254
#編譯varnish源碼包
yum install gcc pcre-devel ncurses-devel -y
tar fxz varnish.~.gz
cd varnish.~
./configure --prefix=/usr/local/varnish && make && make install
#編輯varnish配置文件,加入下面內容
#vim /usr/local/varnish/etc/varnish/default.vcl
- #建立默認的反向代理
- backend default {
- .host = "192.168.40.250";
- .port = "80";
- }
- #建立image的反向代理
- backend image {
- .host = "192.168.40.251";
- .port = "80";
- }
- #建立規則匹配不同的反向代理
- sub vcl_recv {
- # 匹配以.jpg結尾的url
- if (req.url ~ "\.jpg$") {
- set req.backend = image;
- # return (pass);
- return (lookup);
- }
- # 匹配其他
- else {
- set req.backend = default;
- return (pass);
- }
- }
#啓動vanishi服務,該命令中,-f指定配置文件位置,-s爲緩存文件分配的內存大小,-T指定管理ip,-a指定監聽端口
/usr/local/varnish/sbin/varnishd -f /usr/local/varnish/etc/varnish/default.vcl -s malloc,100M -T 127.0.0.1:2000 -a 0.0.0.0:80
在該項業務中,當北京用戶需要訪問站點www.smileliuyb.com時,需要執行以下過程:首先,用戶需要向DNS服務器發出請求,請求www.smileliuyb.com對應的ip地址;
DNS協議 | TCP協議 | IP協議 | MAC |
www.smileliuyb.com's IP is? | dport:53 |
Dip:192.168.30.11 |
源目MAC地址 |
DNS接受到該請求,則比對配置文件中的acl地址池,發現其對應的區域文件爲beijing.smileliuyb.com.zone,則讀取該區域文件內容,並將其中的ip地址回覆給用戶
DNS協議 | TCP協議 | IP協議 | MAC |
www.smileliuyb.com's ip is 192.168.40.250 |
sport:53 |
Dip:192.168.10.X |
源目MAC地址 |
用戶在得到該解析後,向www.smileliuyb.com站點發出瀏覽請求
HTTP協議 | TCP協議 | IP協議 | MAC |
GET *.html HEAD www.smileliuyb.com |
dport:80 | Dip:192.168.40.250 Sip:192.168.10.X |
源目MAC地址 |
服務器在接受到用戶請求後,分析該請求並返回相應的頁面
HTTP協議 | TCP協議 | IP協議 | MAC |
Type:text/html DATA(index.html) |
sport:80 | Dip:192.168.10.X Sip:192.168.40.250 |
源目MAC地址 |
用戶在得到相應的頁面後,發現頁面中存在代碼<img border="0" src="http://image.smileliuyb.com/a.jpg"/> ,此時,用戶再次向DNS服務器發出請求,請求image.smileliuyb.com的IP地址。
DNS協議 | TCP協議 | IP協議 | MAC |
image.smileliuyb.com's IP is? | dport:53 |
Dip:192.168.30.11 |
源目MAC地址 |
DNS服務器在接受到用戶請求時,比對acl地址池並返回相應視圖所對應的區域文件中關於該網址的IP數據
DNS協議 | TCP協議 | IP協議 | MAC |
image.smileliuyb.com's ip is 192.168.40.251 |
sport:53 |
Dip:192.168.10.X |
源目MAC地址 |
在此之前,北京用戶與上海用戶的數據包基本相同,之後,請求a.jpg時,纔會出現不同,上一個DNS請求,上海用戶得到的回覆數據包如下
DNS協議 | TCP協議 | IP協議 | MAC |
image.smileliuyb.com's ip is 192.168.50.251 |
sport:53 |
Dip:192.168.20.X |
源目MAC地址 |
用戶在得到DNS的解析應答後,向相應的服務器請求,此時,北京用戶發出的請求如下
HTTP協議 | TCP協議 | IP協議 | MAC |
GET a.jpg HEAD image.smileliuyb.com |
dport:80 | Dip:192.168.40.251 Sip:192.168.10.X |
源目MAC地址 |
上海用戶發出的請求如下
HTTP協議 | TCP協議 | IP協議 | MAC |
GET a.jpg HEAD image.smileliuyb.com |
dport:80 | Dip:192.168.50.251 Sip:192.168.10.X |
源目MAC地址 |
當北京用戶的請求被圖片服務器接收到之後,服務器將返回a.jpg文件,用戶得到該文件後,這次訪問就完成了。
HTTP協議 | TCP協議 | IP協議 | MAC |
Type:image DATA(a.jpg) |
sport:80 | Dip:192.168.10.X Sip:192.168.40.251 |
源目MAC地址 |
而上海用戶的請求被圖片緩存服務其接收到之後,該緩存服務器將查看自己的緩存空間,如果緩存空間中已近緩存了該圖片,則直接應答用戶請求;如果緩存空間中沒有該文件,則需要先向圖片服務器發送請求,請求該圖片,在得到應答後,在向用戶發送應答數據包,並將該文件存入到自己的緩存空間中。該過程數據包如下:
a.緩存服務器請求圖片
HTTP協議 | TCP協議 | IP協議 | MAC |
GET a.jpg HEAD image.smileliuyb.com |
dport:80 | Dip:192.168.40.251 Sip:192.168.50.251 |
源目MAC地址 |
b.圖片服務器應答請求緩存服務器請求
HTTP協議 | TCP協議 | IP協議 | MAC |
Type:image DATA(a.jpg) |
sport:80 | Dip:192.168.50.251 Sip:192.168.40.251 |
源目MAC地址 |
c.緩存服務器應答用戶請求
HTTP協議 | TCP協議 | IP協議 | MAC |
Type:image DATA(a.jpg) |
sport:80 | Dip:192.168.50.251 Sip:192.168.20.X |
源目MAC地址 |
當上海其他用戶再次請求該圖片時,因爲該圖片已近在緩存服務器的緩存中,緩存服務器可以直接向用戶返回應答請求,達到加速的目的。