環境 : CentOS 5.5 x86
安裝 yum repos
wget http://download.fedoraproject.org/pub/epel/5/i386/epel-release-5-4.noarch.rpm rpm -Uvh epel-release-5-4.noarch.rpm
安裝 geoip 相關套件
yum install mod_geoip GeoIP GeoIP-devel GeoIP-data zlib-devel httpd mod_php
在 apache 就有 geoip 設定 (/etc/httpd/conf.d/geoip.conf)
LoadModule geoip_module modules/mod_geoip.so <IfModule mod_geoip.c> GeoIPEnable On GeoIPDBFile /usr/share/GeoIP/GeoIP.dat </IfModule>
啟動 apache
service httpd start
寫一個 PHP
<html> <head> <title>What is my IP address and Country</title> </head> <body> <?php if (getenv(HTTP_X_FORWARDED_FOR)) { $pipaddress = getenv(HTTP_X_FORWARDED_FOR); $ipaddress = getenv(REMOTE_ADDR); echo "Your Proxy IP address is : ".$pipaddress. " (via $ipaddress) " ; } else { $ipaddress = getenv(REMOTE_ADDR); echo "My IP address is : $ipaddress"; } $country = getenv(GEOIP_COUNTRY_NAME); echo "<br />My Country : $country"; ?> </body> </html>
呈現如下
My IP address is : 124.xx.xx.92 My Country : Taiwan
如果要更新 GeoIP.dat 可到 http://dev.maxmind.com/geoip/geolite 下載 Free 的
還可搭配 mod_rewrite 重導那些來源區域到特定地方
# Redirect one country RewriteEngine on RewriteCond %{ENV:GEOIP_COUNTRY_CODE} ^AS$ RewriteRule ^(.*)$ http://www.tecmint.com$1 [R,L]
或阻擋或開放
SetEnvIf GEOIP_COUNTRY_CODE AS BlockCountry SetEnvIf GEOIP_COUNTRY_CODE US AllowCountry # ... place more countries here Deny from all Deny from env=BlockCountry Allow from env=AllowCountry
參考 :
http://www.tecmint.com/install-mod_geoip-for-apache-in-rhelcentos-6-35-8/
http://dev.maxmind.com/geoip/mod_geoip2
留言