Compile nginx With GeoIP Module

wget http://nginx.org/download/nginx-0.8.52.tar.gz
 tar -zxvf nginx-0.8.52.tar.gz
 cd nginx-0.8.52
 yum install gcc pcre-devel.x86_64 openssl-devel.x86_64
 ./configure --with-http_geoip_module
 make
 make install

nginx Configuration

Edit nginx.conf, enter:

Locate http section and update it as follows for geoip country lookup:

geoip_country /usr/local/share/GeoIP/GeoIP.dat;
fastcgi_param GEOIP_COUNTRY_CODE $geoip_country_code;
fastcgi_param GEOIP_COUNTRY_CODE3 $geoip_country_code3;
fastcgi_param GEOIP_COUNTRY_NAME $geoip_country_name;

Save and close the file. If you want city level geo targeting set it as follows:

PHP Test Script

Create a php test script as follows geoip.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 "Your Proxy IP address is : ".$pipaddress. " (via $ipaddress) " ;
    } else {
        $ipaddress = getenv(REMOTE_ADDR);
        echo "Your IP address is : $ipaddress";
    }
    $country = getenv(GEOIP_COUNTRY_NAME);
    $country_code = getenv(GEOIP_COUNTRY_CODE);
    echo "<br/>Your country : $country ( $country_code ) ";
?>
</body>
</html>

sources

[cyberciti] : http://www.cyberciti.biz/faq/linux-unix-nginx-geoip-module-configuration/