How do I install GeoIP nginx module for country and/or city level geo targeting?
nginx server version 0.7.63 and 0.8.6 above comes with ngx_http_geoip_module. It will match the IP-address of the client the MaxMind GeoIP databases i.e. ip/location lookups.
In this FAQ you will learn about deploying nginx based mod_geoip installation and php server side examples to determine visitors country under UNIX / Linux / *BSD like operating systems.
Install MaxMind C API
Type the following commands to install 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
# yum install zlib-devel
# ./configure
# make
# make install
You need to configure dynamic linker run time bindings as follows:
# echo '/usr/local/lib' > /etc/ld.so.conf.d/geoip.conf
Run ldconfig to activate configuration:
# ldconfig
# ldconfig -v | less
Compile nginx With GeoIP Module
Get the latest source code:
# cd /tmp
# 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 --without-http_empty_gif_module --with-poll_module --with-http_stub_status_module --with-http_ssl_module --with-ipv6 --with-http_geoip_module
# make
# make install
Make sure you ./configure nginx with --with-http_geoip_module option.
Grab MaxMind Databases
Type the following command to get the free database of geo_city:
# 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
Get the free database of geo_country:
# wget http://geolite.maxmind.com/download/geoip/database/GeoLiteCountry/GeoIP.dat.gz -O /usr/local/share/GeoIP/GeoIP.dat.gz
# gunzip /usr/local/share/GeoIP/GeoIP.dat.gz
A Note About MaxMind Paid Version
If you need 99.8% accuracy about IP address use the paid version. Edit /usr/local/etc/GeoIP.conf, enter:
# vi /usr/local/etc/GeoIP.conf
Update config as follows:
# see https://www.maxmind.com/app/license_key_login # Enter your license key here LicenseKey YOUR_LICENSE_KEY_HERE # Enter your User ID here UserId YOUR_USER_ID_HERE # Enter your Product ID here i.e. 106 geoip country ProductIds 106
Save and close the file. Get latest licensed version database, enter:
# /usr/local/bin/geoipupdate
nginx Configuration
Edit nginx.conf, enter:
# vi /usr/local/nginx/conf/nginx.conf
Locate http section and update it as follows for geoip country lookup:
### SET the path to the .dat file used for determining the visitor's country from the IP-address ### geoip_country /usr/local/share/GeoIP/GeoIP.dat; ### SET FASTCGI Variables ### 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:
### 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;
Save and close the file. Finally, reload nginx:
# /usr/local/nginx/sbin/nginx -s reload
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>
Sample outputs:
You can see working example here.
How Do I Find Out MaxMind Database Info?
Type the following command:
# /usr/local/bin/geoiplookup -v 1.2.3.4
Sample outputs:
GeoIP Country Edition: GEO-106 20101001 Build 1 Copyright (c) 2010 MaxMind Inc All Rights Reserved
How Do I Use Country Code in nginx Configuration?
You can use it as follows:
### USA specific config ### if ($geoip_country_code = US) { do something here for USA visitors; # e.g. set root path /var/www/html/content/usa/; } ### India specific config ### if ($geoip_country_code = IN) { do something here for Indian visitors ; # e.g. set root path /var/www/html/content/india/; }
How Do I Test City Level Geo Targeting?
Use the following php code:
<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>
Sample outputs:
Further readings:
You should follow me on twitter here or grab rss feed to keep track of new changes.
Featured Articles:
- 30 Handy Bash Shell Aliases For Linux / Unix / Mac OS X
- Top 30 Nmap Command Examples For Sys/Network Admins
- 25 PHP Security Best Practices For Sys Admins
- 20 Linux System Monitoring Tools Every SysAdmin Should Know
- 20 Linux Server Hardening Security Tips
- Linux: 20 Iptables Examples For New SysAdmins
- Top 20 OpenSSH Server Best Security Practices
- Top 20 Nginx WebServer Best Security Practices
- 20 Examples: Make Sure Unix / Linux Configuration Files Are Free From Syntax Errors
- 15 Greatest Open Source Terminal Applications Of 2012

- My 10 UNIX Command Line Mistakes
- Top 10 Open Source Web-Based Project Management Software
- Top 5 Email Client For Linux, Mac OS X, and Windows Users
- The Novice Guide To Buying A Linux Laptop

















{ 11 comments… read them below or add one }
o_O
kick ass bookmar?
Want to kill some time or may be website?
http://erkie.github.com/
Haha
Hi,
how to activate this on a existing Installation ?
thanks..
You need to compile minimum with nginx version stable 0.7.63 or latest 0.8.6. Also use apt-get or yum to search for the same.
Thanks a lots!
This looks exactly like what I was looking for so thank you very much for posting and if it works I will link to it etc to try and get you some more traffic!
Hello,
I followed the exact steps, everything went fine but i keep receiving the error:
PHP Fatal error: Call to undefined function geoip_country_code_by_code() in /var/www/vhosts/…
Please advice!
Thanks!
This tutorial have a “bug”,it should be mentioned how this section:
### SET FASTCGI Variables ###
fastcgi_param GEOIP_COUNTRY_CODE $geoip_country_code;
fastcgi_param GEOIP_COUNTRY_CODE3 $geoip_country_code3;
fastcgi_param GEOIP_COUNTRY_NAME $geoip_country_name;
goes to fastcgi_params file,not nginx.conf.Didn’t worked because of that.
Thanks for your reply!
NOT WORKING!! :(
You was missed something in installing maxmind class C which it’s required gcc-libs