Nginx geoip module achieves regional Load Balancing

Source: Internet
Author: User
Tags geoip maxmind

I believe that awstats has used open source geoip. the dat ip database is just nginx wiki with the geoip module, which can achieve regional load balancing. However, maxmind's ip database does not support China very well, but it is also good now ~

Http://wiki.nginx.org/NginxHttpGeoIPModule: my environment, I have an American linux server, an American windows 2003, an XP. Machines and other test users are all friends in the QQ group. Now, let's start testing linux: 75.125.x.x // us win2003: 74.55.x.x // us XP: localhost/Beijing testing forwarding. U.S. users ~ Forward to www.google.cn China Telecom forward to one of my public network apache default pages China Netcom forward to one of my public network business servers !! 1. download and install nginx. shell $> get http://sysoev.ru/nginx/nginx-0.8.13.tar.gzshell $> tar zxvf nginx-0.8.13.tar.gzshell $> cd nginx-0.8.13shell $> apt-get install libgeoip-devshell $>. /configure -- prefix =/usr/local/nginx -- with-http_flv_module -- user = www -- group = www -- with-http_gzip_static_module -- with-http_geoip_moduleshell $> make shell $> make install 2.download geolitecity.dat.gz database ~ Shell $> wget http://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gzshell $> gzip-d GeoLiteCity.dat.gz shell $> mv GeoLiteCity. dat/usr/local/nginx/conf/GeoLiteCity. dat 3. modify the configuration file to implement regional load shell $> cd/usr/local/nginx/confshell $> cat nginx. confworker_processes 1; events {
Worker_connections 1024;
} Http {
Include mime. types;
Default_type application/octet-stream;
Geoip_city GeoLiteCity. dat; upstream wangtong {
Server 59.151.X.X;
}
Upstream dianxin {
Server 75.125.X.X;
}
Upstream USA {
Server www.google.cn;
} Sendfile on; keepalive_timeout 65; server {
Listen 80;
Server_name 75.125.197.200;
Root html;
Index index.html index.htm; Location /{
If ($ geoip_region ~ "(01 | 02 | 03 | 04 | 06 | 07 | 11 | 13 | 14 | 15 | 16 | 21 | 23 | 29 | 30 | 31 | 32 | 33 )") {
Proxy_pass
Http: // dianxin $ request_uri ;
}
If ($ geoip_region ~ "(05 | 08 | 09 | 10 | 12 | 17 | 18 | 19 | 20 | 24 | 25 | 26 )"){
Proxy_pass
Http: // wangtong $ request_uri ;
}
If ($ geoip_city_country_code ~ "US "){
Proxy_pass
Http: // USA $ request_uri ;
}
}Error_page 500 502 503 x.html;
Location =/50x.html {
Root html;
}}} 4. Test. Use machines in different places for testing ~ I am a user in Beijing. I access the default page from a user in Beijing because I did not add the number 22 to the configuration file. I am trying to facilitate the test! We need to add 22 to production.

If ($ geoip_region ~ "(05 | 08 | 09 | 10 | 12 | 17 | 18 | 19 | 20 | 24 | 25 | 26 )")If no match is found, the default page is accessed ~~ Chengdu friends help visit: Guangzhou friends help visit: Hebei friends help visit: US win2003 access: direct access to telecommunications servers and Netcom servers 59.151.X.X; 75.125.X.X; directly access Netcom 59.151.X.X and directly access the telecommunications server 75.125.X.X. Let me explain if ($ geoip_region ~ "(01 | 02 | 03 | 04 | 06 | 07 | 11 | 13 | 14 | 15 | 16 | 21 | 23 | 29 | 30 | 31 | 32 | 33 )") these numbers represent provinces and regions in China ~~ Table: CN, 01, "Anhui"
CN, 02, "Zhejiang"
CN, 03, "Jiangxi"
CN, 04, "Jiangsu"
CN, 05, "Jilin"
CN, 06, "Qinghai"
CN, 07, "fujiian"
CN, 08, "Heilongjiang"
CN, 09, "Henan"
CN, 10, "Hebei"
CN, 11, "Hunan"
CN, 12, "Hubei"
CN, 13, "Xinjiang"
CN, 14, "Xizang"
CN, 15, "Gansu"
CN, 16, "Guangxi"
CN, 18, "Guizhou"
CN, 19, "Liaoning"
CN, 20, "Nei rjl"
CN, 21, "Ningxia"
CN, 22, "Beijing"
CN, 23, "Shanghai"
CN, 24, "Shanxi"
CN, 25, "Shandong"
CN, 26, "Shaanxi"
CN, 28, "Tianjin"
CN, 29, "Yunnan"
CN, 30, "Guangdong"
CN, 31, "Hainan"
CN, 32, "Sichuan"
CN, 33, "Chongqing" GeoLiteCity. for more variables of dat, see wiki. Here we only use two variables: $ geoip_region, $ geoip_city_country, region, and country! Geoip_city

Syntax: Geoip_city path/to/db. dat; Default:None Context: HttpThe directive indicates the path to the. dat file used for determining countries, regions and cities from IP-address of the client. When set the module makes available the following variables:

$ Geoip_city_country_code;-two-letter country code, for example, "RU", "US ". $ geoip_city_country_code3;-three-letter country code, for example, "RUS", "USA ". $ geoip_city_country_name;-the name of the country, for example, "Russian Federation", "United States ". $ geoip_region;-the name of region (province, region, state, province, federal land, and the like), for example, "Moscow City", "DC ". $ geoip_city;-the name of the city, for example, "Moscow", "Washington ". $ geoip_postal_code;-postal code.

PS: I only distinguish between China Southern Telecom and China North Telecom ~~ The default page is accessed by Beijing users because I did not add the 22 numbers to the configuration file. I am trying to facilitate the test! We need to add 22 to production. If ($ geoip_region ~ "(05 | 08 | 09 | 10 | 12 | 17 | 18 | 19 | 20 | 24 | 25 | 26 )")China Netcom ~ However, some open-source geoips are inaccurate ~~~ You can only give him 75 minutes ~~
This article is from the "linuxer" blog, please be sure to keep this source http://deidara.blog.51cto.com/400447/198469

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.