2010年12月25日 星期六

Nginx - Install GeoIP Module For Country

1.編譯/安裝 MaxMind C API
# cd /tmp
# wget http://geolite.maxmind.com/download/geoip/api/c/GeoIP.tar.gz
# tar -zxvf GeoIP.tar.gz
# cd GeoIP-1.4.6
# ./configure
# make && make install



2.設定動態連結
# echo '/usr/local/lib' > /etc/ld.so.conf.d/geoip.conf
# ldconfig



3.新增使用者/群組
# groupadd www
# useradd -g www www


4.編譯/安裝 Nginx With Geo IP Module
#cd /usr/local/src/
# wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.10.tar.gz

# tar zxvf pcre-8.10.tar.gz
# cd pcre-8.10
# ./configure
# make && make install
# cd ..
#wget http://nginx.org/download/nginx-0.8.52.tar.gz
# tar zxvf nginx-0.8.52.tar.gz
# cd nginx-0.8.52
#./configure --prefix=/usr/local/nginx --user=www --group=www --without-http_empty_gif_module --with-poll_module --with-http_stub_status_module --with-http_ssl_module --with-http_geoip_module
# make && make install
# cd ..

5.取得MaxMind資料庫
# wget http://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz -O /usr/local/share/GeoIP/GeoLiteCity.dat.gz
# gunzip /usr/local/share/GeoIP/GeoLiteCity.dat.gz

# vi /usr/local/etc/GeoIP.conf
# /usr/local/bin/geoipupdate

6.編輯nginx.conf
# vi /usr/local/nginx/conf/nginx.conf
http { 裡加入

### SET the path to the .dat file used for determining the visitor's country from the IP-address ###
geoip_city /usr/local/share/GeoIP/GeoLiteCity.dat;

  ### SET FASTCGI Variables ###
  fastcgi_param GEOIP_CITY_COUNTRY_CODE $geoip_city_country_code;
  fastcgi_param GEOIP_CITY_COUNTRY_CODE3 $geoip_city_country_code3;
  fastcgi_param GEOIP_CITY_COUNTRY_NAME $geoip_city_country_name;
  fastcgi_param GEOIP_REGION $geoip_region;
  fastcgi_param GEOIP_CITY $geoip_city;
  fastcgi_param GEOIP_POSTAL_CODE $geoip_postal_code;
  fastcgi_param GEOIP_CITY_CONTINENT_CODE $geoip_city_continent_code;
  fastcgi_param GEOIP_LATITUDE $geoip_latitude;
  fastcgi_param GEOIP_LONGITUDE $geoip_longitude;

7.重啟nginx
# /usr/local/nginx/sbin/nginx -s reload

8.測試
# vi /usr/local/nginx/html/test.php
內容如下
<html>
<head>
<title>What is my IP address - determine or retrieve my IP address</title>
</head>
<body>
<?php
if (getenv(HTTP_X_FORWARDED_FOR)) {
$pipaddress = getenv(HTTP_X_FORWARDED_FOR);
$ipaddress = getenv(REMOTE_ADDR);
echo "<br>Your Proxy IP address is : ".$pipaddress. " (via $ipaddress) " ;
} else {
$ipaddress = getenv(REMOTE_ADDR);
echo "<br>Your IP address is : $ipaddress";
}
$geoip_city_country_code = getenv(GEOIP_CITY_COUNTRY_CODE);
$geoip_city_country_code3 = getenv(GEOIP_CITY_COUNTRY_CODE3);
$geoip_city_country_name = getenv(GEOIP_CITY_COUNTRY_NAME);
$geoip_region = getenv(GEOIP_REGION);
$geoip_city = getenv(GEOIP_CITY);
$geoip_postal_code = getenv(GEOIP_POSTAL_CODE);
$geoip_city_continent_code = getenv(GEOIP_CITY_CONTINENT_CODE);
$geoip_latitude = getenv(GEOIP_LATITUDE);
$geoip_longitude = getenv(GEOIP_LONGITUDE);
echo "<br>Country : $geoip_city_country_name ( $geoip_city_country_code3 , $geoip_city_country_code ) ";
echo "<br>Region : $geoip_region";
echo "<br>City : $geoip_city ";
echo "<br>Postal code : $geoip_postal_code";
echo "<br>City continent code : $geoip_city_continent_code";
echo "<br>Geoip latitude : $geoip_latitude ";
echo "<br>Geoip longitude : $geoip_longitude ";

?>
</body>
</html>

大功告成! 以上nginx要先設定好支援PHP功能

沒有留言:

張貼留言