Use DNS + GeoIP + Nginx + Varnish for world-class CDN

Source: Internet
Author: User
Tags geoip node server varnish

This article is from the "practice test truth" blog


How to use BIND, GeoIP, Nginx, and Varnish to create your own efficient CDN network?
CDN stands for Content Distrubtion Network, which is a Content delivery Network. In short, it refers to Server Load balancer in all regions. The concept of a whole region can be national or global. The unified DNS server performs address forwarding and selects the region server closest to the user for load balancing. In essence, Server Load balancer is expanded from an equipment room to a global Server Load balancer. At the same time, the localized content can be implemented by the local server. Make the browser automatically select the region.
For example, in China, China is divided into two major regions: China Netcom in the north and China Telecom in the south. Mutual access between the two networks is slow. As a large website, one solution is to set up all servers at a dual-line or three-line ISP, and the ISP will provide the route selection. In this way, the cost of the line will be relatively high. Another method is to set up the server on both sides, one in the south and one in the north. Then, the server selects the server. If the IP address is in China Telecom, the server forwards the request to the server in the south, if it is Netcom, it will forward it to the server in the north.
By expanding the scope, requests from the United States can be handed over to the U.S. server for processing, which also shortens the waiting time on the route. This is the content delivery network.
All nodes on the network can be viewed as virtual servers. Server Load balancer can be performed between nodes on servers in different regions.
The preparation work is as follows: You need to download the following software to implement the above functions
Nginx, BIND, GeoIP, Varnish
Next, compile and install bind9 and geoip.
# 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 // patch bind9 to allow bind9 to directly support the geoip Library
# 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
After bind is installed, create named. conf.
View "us "{
// Match the North American client 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 to South America
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 ";
};
};
You can also match with Europe, Africa, and so on, and then start to make nginx and varnish
Note: The above content is what you want to do on the master node server. The master node server is only responsible for forwarding DNS requests.
As agreed, the Bind server is called a dynamic node server, and Nginx + Varnish is called a boundary server.
The following content is what the secondary node server needs to do, that is, the server actually placed in a region
#./Configure-prefix =/usr/local/nginx-with-http_realip_module
# Make
# Make install
And configure Nginx
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 is the IP address of the primary DNS Node
}
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;
}
Above we use nginx to cache static files only, and cache static files in the/var/www/cache folder. If you do not have any, you need to create this folder. Nginx listens to port 8080. This is because varnish is used to listen to port 80 for dynamic file forwarding. In fact, nginx is used as a reverse proxy and cache server for static files, and varnish is the reverse proxy that truly allows users to see webpages and dynamic files, the separate storage of static and dynamic files can greatly improve the efficiency.
Finally, we will configure the varnish service.
# Tar-xzvf varnish-2.1.2.tar.gz
#./Configure-prefix =/usr/local/varnish
# Make
# Make install
Then the varnish Option
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;
}
}
For other configuration content, see the varnish configuration document.
Summary:
The benefits of doing so are:
1. The root cause solves the uncertainty of DNS in round-robin and enables fast response on DNS. It also avoids high load when Nginx + GeoIP is used in the past. After all, DNS computing is much smaller than Nginx.
2. Reduce the server load pressure and operating costs of large websites. After all, the price and service fee of F5BigIP and dual-line are too high.
3. Easy scalability. If the load pressure in a region is high, you only need to add the web server of the border server group in the region. You do not need to consider the jump issue.
Let me think about other advantages.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.