# tar -xzvf bind-9.2.4.tar.gz
# tar -xzvf GeoIP-1.4.6.tar.gz
# cd GeoIP-1.4.6
# ./configure –prefix=/usr/local/geoip
# make
# make install
# cd ..
# patch -p0 < bind-9.2.4-geodns-patch/patch.diff //给bind9打补丁,让bind9直接支持geoip库
# cd bind-9.2.4
# CFLAGS=”-I/usr/local/geoip/include” LDFLAGS=”-L/usr/local/geoip/lib -lGeoIP” ./configure –prefix=/usr/local/bind
# make
# make install
view “us” {
// 匹配北美的客户端 US & Canada
match-clients { country_US; country_CA; };
// Provide recursive service to internal clients only.
recursion no;
zone “cdn.xianglei.com” {
type master;
file “pri/xianglei-us.db”;
};
zone “.” IN {
type hint;
file “named.ca”;
};
};
view “latin” {
// 匹配到南美国家
match-clients { country_AR; country_CL; country_BR; };
recursion no;
zone “cdn.xianglei.com” {
type master;
file “pri/xianglei-latin.db”;
};
zone “.” IN {
type hint;
file “named.ca”;
};
};
注意,以上内容是你要在主节点服务器上做的,主节点服务器只负责对DNS请求进行转发。
约定一下,我们将Bind服务器叫做动态节点服务器,Nginx+Varnish叫做边界服务器。
# ./configure –prefix=/usr/local/nginx –with-http_realip_module
# make
# make install
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
upstream dynamic_node {
server 1.1.1.1:80; # 1.1.1.1 是主DNS节点的IP地址
}
server {
listen 8080;
server_name cdn.xianglei.net;
location ~* \.(gif|jpg|jpeg|png|wmv|avi|mpg|mpeg|mp4|htm|html|js|css|mp3|swf|ico|flv)$ {
proxy_set_header X-Real-IP $remote_addr;
proxy_pass http://dynamic_node;
proxy_store /var/www/cache$uri;
proxy_store_access user:rw group:rw all:r;
}
# tar -xzvf varnish-2.1.2.tar.gz
# ./configure –prefix=/usr/local/varnish
# make
# make install
backend default {
.host = “127.0.0.1″;
.port = “8080″;
}sub vcl_recv {
if (req.url ~ “\.(js|css|jpg|jpeg|png|gif|gz|tgz|bz2|tbz|mp3|ogg|swf)$”) {
return (lookup);
}
}sub vcl_fetch {
if (req.url ~ “\.(js|css|jpg|jpeg|png|gif|gz|tgz|bz2|tbz|mp3|ogg|swf)$”) {
unset obj.http.set-cookie;
}
}
这样做的好处在于:
1.从根源上解决了DNS在轮询上的不确定性,能够做到在DNS上的快速响应。也避免了过去用Nginx+GeoIP时的负载高的问题。毕竟DNS的计算要比Nginx小多了。
2.降低大网站的服务器负载压力和运营成本,毕竟F5BigIP和双线路的价格和服务费都太高了。
3.易扩展性强,如某地区负载压力大,只需在该地区增加边界服务器组的web server即可完成,无需考虑跳转问题。