Nginx GeoIP module to achieve regional load balancing _nginx

Source: Internet
Author: User
Tags geoip maxmind
Believe that have done awstats have used the open source Geoip.dat IP database, just nginx wiki on the GeoIP module, so that can achieve a regional load balance, but the Maxmind of IP database for China's support is not too good, but now is not bad ~
Reference article: Http://wiki.nginx.org/NginxHttpGeoIPModule
Next to my environment, I have an American Linux server, an American Windows 2003, a local XP. Machine, other test users are, QQ group of friends, okay to start testing
linux:75.125.x.x//USA
win2003:74.55.x.x//USA
Xp:localhost//Beijing
Test forwarding, US user ~ forwarding to www.google.cn
Telecom forwarding to my one of the public network Apache default page
Netcom forwarded to my a public network service server!!
1. Download and install Nginx.
Shell $> Get http://sysoev.ru/nginx/nginx-0.8.13.tar.gz
Shell $> Tar zxvf nginx-0.8.13.tar.gz
Shell $> CD nginx-0.8.13
Shell $>apt-get Install Libgeoip-dev
Shell $>./configure--prefix=/usr/local/nginx--with-http_flv_module--user=www--group=www--with-http_gzip_ Static_module--with-http_geoip_module
Shell $> Make
Shell $> make Install
2. Download the GeoLiteCity.dat.gz database ~
Shell $> wget http://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz
Shell $> gzip-d GeoLiteCity.dat.gz
Shell $> MV Geolitecity.dat/usr/local/nginx/conf/geolitecity.dat
3. Modify the configuration file to achieve a regional quality load
Shell $> cd/usr/local/nginx/conf
Shell $> Cat nginx.conf
Worker_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 502 503 504/50x.html;
Location =/50x.html {
root HTML;
}
}

}
4. Testing, using different local machines to do the test ~
I am a Beijing user, visit
I am a Beijing user access is the default page because I did not add 22 numbers to the configuration file. I was trying to make it easier! If you use it in production, you need to add 22 to
if ($geoip _region ~ "(05|08|09|10|12|17|18|19|20|24|25|26)")
No match to, access the default page ~ ~
Chengdu friends to help visit:
Guangzhou friends to help visit:
Hebei friends to help visit:
American Win2003 Visit:
Direct access to telecom servers and Netcom servers
59.151.x.x; 75.125.x.x;
Direct access to Netcom 59.151.x.x
Direct access to the telecom 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 figures represent the provinces of China ~ ~
The table is as follows:
cn,01, "Anhui."
cn,02, "Zhejiang."
cn,03, "Jiangxi."
cn,04, "Jiangsu."
cn,05, "Jilin."
cn,06, "Qinghai."
cn,07, "Fujian."
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 Mongol"
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.dat More variables look at the wiki. I only use two variables here. One is the $geoip_region one is the $geoip_city_country the first is the region, the second is the country to take only two letters shorthand!
Geoip_city
Syntax: geoip_city Path/to/db.dat;
Default: None
Context : http
The directive indicates the path to the "DAT file used for determining countries, regions and cities from ip-address of th E 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, the 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 just according to the southern Telecom, North Netcom to distinguish ~ ~ I was a Beijing user access is the default page because I did not add 22 numbers to the configuration file. I was trying to make it easier! If you use it in production, you need to add 22 to
if ($geoip _region ~ "(05|08|09|10|12|17|18|19|20|24|25|26)")
Netcom ~ But open source GeoIP still some inaccurate ~ ~ ~ only to give him 75 points ~ ~
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.