{"id":411,"date":"2013-11-21T14:48:04","date_gmt":"2013-11-21T14:48:04","guid":{"rendered":"http:\/\/blog.shineservers.com\/?p=411"},"modified":"2013-11-21T14:48:04","modified_gmt":"2013-11-21T14:48:04","slug":"install-geoip-module-countrycity-level-geo-targeting-nginx","status":"publish","type":"post","link":"https:\/\/www.shineservers.com\/2013\/11\/21\/install-geoip-module-countrycity-level-geo-targeting-nginx\/","title":{"rendered":"Install GeoIP Module For Country\/City Level Geo Targeting (NGINX)"},"content":{"rendered":"<h2>Install MaxMind C API<\/h2>\n<p>Type the following commands to install MaxMind C API:<br \/>\n<code># cd \/tmp<br \/>\n#\u00a0<a href=\"http:\/\/www.cyberciti.biz\/tips\/linux-wget-your-ultimate-command-line-downloader.html\">wget http:\/\/geolite.maxmind.com\/download\/geoip\/api\/c\/GeoIP.tar.gz<\/a><br \/>\n# tar -zxvf GeoIP.tar.gz<br \/>\n# cd GeoIP-1.4.6<br \/>\n#\u00a0<a href=\"http:\/\/www.cyberciti.biz\/faq\/rhel-centos-fedora-linux-yum-command-howto\/\">yum install zlib-devel<\/a><br \/>\n# .\/configure<br \/>\n# make<br \/>\n# make install<\/code><br \/>\nYou need to configure dynamic linker run time bindings as follows:<br \/>\n<code># echo '\/usr\/local\/lib' &gt; \/etc\/ld.so.conf.d\/geoip.conf<\/code><br \/>\nRun ldconfig to activate configuration:<br \/>\n<code># ldconfig<br \/>\n# ldconfig -v | less<\/code><\/p>\n<h2>Compile nginx With GeoIP Module<\/h2>\n<p>Get the latest source code:<br \/>\n<code># cd \/tmp<br \/>\n#\u00a0<a href=\"http:\/\/www.cyberciti.biz\/tips\/linux-wget-your-ultimate-command-line-downloader.html\">wget http:\/\/nginx.org\/download\/nginx-0.8.52.tar.gz<\/a><br \/>\n# tar -zxvf nginx-0.8.52.tar.gz<br \/>\n# cd nginx-0.8.52<br \/>\n#\u00a0<a href=\"http:\/\/www.cyberciti.biz\/faq\/rhel-centos-fedora-linux-yum-command-howto\/\">yum install gcc pcre-devel.x86_64 openssl-devel.x86_64<\/a><br \/>\n# .\/configure --without-http_empty_gif_module --with-poll_module --with-http_stub_status_module --with-http_ssl_module --with-ipv6\u00a0--with-http_geoip_module<br \/>\n# make<br \/>\n# make install<\/code><br \/>\nMake sure you .\/configure nginx with &#8211;with-http_geoip_module option.<\/p>\n<h2>Grab MaxMind Databases<\/h2>\n<p>Type the following command to get the free database of geo_city:<br \/>\n<code># wget http:\/\/geolite.maxmind.com\/download\/geoip\/database\/GeoLiteCity.dat.gz -O \/usr\/local\/share\/GeoIP\/GeoLiteCity.dat.gz<br \/>\n#\u00a0<a href=\"http:\/\/www.cyberciti.biz\/faq\/howto-compress-expand-gz-files\/\">gunzip \/usr\/local\/share\/GeoIP\/GeoLiteCity.dat.gz<\/a><\/code><br \/>\nGet the free database of geo_country:<br \/>\n<code># wget http:\/\/geolite.maxmind.com\/download\/geoip\/database\/GeoLiteCountry\/GeoIP.dat.gz -O \/usr\/local\/share\/GeoIP\/GeoIP.dat.gz<br \/>\n#\u00a0<a href=\"http:\/\/www.cyberciti.biz\/faq\/howto-compress-expand-gz-files\/\">gunzip \/usr\/local\/share\/GeoIP\/GeoIP.dat.gz<\/a><\/code><\/p>\n<h3>A Note About MaxMind Paid Version<\/h3>\n<p>If you need 99.8% accuracy about IP address use the\u00a0<a href=\"https:\/\/www.maxmind.com\/app\/license_key_login\" target=\"_blank\" rel=\"noopener noreferrer\">paid version<\/a>. Edit \/usr\/local\/etc\/GeoIP.conf, enter:<br \/>\n<code># vi \/usr\/local\/etc\/GeoIP.conf<\/code><br \/>\nUpdate config as follows:<\/p>\n<pre>\u00a0\n# see https:\/\/www.maxmind.com\/app\/license_key_login\n# Enter your license key here\nLicenseKey YOUR_LICENSE_KEY_HERE\n\n# Enter your User ID here\nUserId YOUR_USER_ID_HERE\n\n# Enter your Product ID here i.e. 106 geoip country\nProductIds 106<\/pre>\n<p>Save and close the file. Get latest licensed version database, enter:<br \/>\n<code># \/usr\/local\/bin\/geoipupdate<\/code><\/p>\n<h2>nginx Configuration<\/h2>\n<p>Edit nginx.conf, enter:<br \/>\n<code># vi \/usr\/local\/nginx\/conf\/nginx.conf<\/code><br \/>\nLocate http section and update it as follows for<strong>\u00a0geoip country<\/strong>\u00a0lookup:<\/p>\n<pre>\u00a0\n  ### SET the path to the .dat file used for determining the visitor's country from the IP-address ###\n  geoip_country \/usr\/local\/share\/GeoIP\/GeoIP.dat;\n\n  ### SET FASTCGI Variables ###\n  fastcgi_param GEOIP_COUNTRY_CODE $geoip_country_code;\n  fastcgi_param GEOIP_COUNTRY_CODE3 $geoip_country_code3;\n  fastcgi_param GEOIP_COUNTRY_NAME $geoip_country_name;<\/pre>\n<p>Save and close the file. If you want city level geo targeting set it as follows:<\/p>\n<pre>\u00a0\n  ### SET the path to the .dat file used for determining the visitor's country from the IP-address ###\n  geoip_city  \/usr\/local\/share\/GeoIP\/GeoLiteCity.dat;\n\n  ### SET FASTCGI Variables ###\n  fastcgi_param GEOIP_CITY_COUNTRY_CODE $geoip_city_country_code;\n  fastcgi_param GEOIP_CITY_COUNTRY_CODE3 $geoip_city_country_code3;\n  fastcgi_param GEOIP_CITY_COUNTRY_NAME $geoip_city_country_name;\n  fastcgi_param GEOIP_REGION $geoip_region;\n  fastcgi_param GEOIP_CITY $geoip_city;\n  fastcgi_param GEOIP_POSTAL_CODE $geoip_postal_code;\n  fastcgi_param GEOIP_CITY_CONTINENT_CODE $geoip_city_continent_code;\n  fastcgi_param GEOIP_LATITUDE $geoip_latitude;\n  fastcgi_param GEOIP_LONGITUDE $geoip_longitude;<\/pre>\n<p>Save and close the file. Finally, reload nginx:<br \/>\n<code># \/usr\/local\/nginx\/sbin\/nginx -s reload<\/code><\/p>\n<h2>PHP Test Script<\/h2>\n<p>Create a php test script as follows geoip.php<\/p>\n<pre>&lt;html&gt;\n&lt;head&gt;\n  &lt;title&gt;What is my IP address - determine or retrieve my IP address&lt;\/title&gt;\n &lt;\/head&gt;\n&lt;body&gt;\n &lt;?php\n     if (<a href=\"http:\/\/www.php.net\/getenv\">getenv<\/a>(HTTP_X_FORWARDED_FOR)) {\n        $pipaddress = <a href=\"http:\/\/www.php.net\/getenv\">getenv<\/a>(HTTP_X_FORWARDED_FOR);\n        $ipaddress = <a href=\"http:\/\/www.php.net\/getenv\">getenv<\/a>(REMOTE_ADDR);\n        <a href=\"http:\/\/www.php.net\/echo\">echo<\/a> \"Your Proxy IP address is : \".$pipaddress. \" (via $ipaddress) \" ;\n    } else {\n        $ipaddress = <a href=\"http:\/\/www.php.net\/getenv\">getenv<\/a>(REMOTE_ADDR);\n        <a href=\"http:\/\/www.php.net\/echo\">echo<\/a> \"Your IP address is : $ipaddress\";\n    }\n    $country = <a href=\"http:\/\/www.php.net\/getenv\">getenv<\/a>(GEOIP_COUNTRY_NAME);\n    $country_code = <a href=\"http:\/\/www.php.net\/getenv\">getenv<\/a>(GEOIP_COUNTRY_CODE);\n    <a href=\"http:\/\/www.php.net\/echo\">echo<\/a> \"&lt;br\/&gt;Your country : $country ( $country_code ) \";\n?&gt;\n&lt;\/body&gt;\n&lt;\/html&gt;<\/pre>\n<p>Sample outputs:<\/p>\n<p><a href=\"http:\/\/www.cyberciti.biz\/faq\/linux-unix-nginx-geoip-module-configuration\/geoip-what-is-my-ip-address\/\" rel=\"attachment wp-att-8950\"><img fetchpriority=\"high\" decoding=\"async\" title=\"GeoIP: What is my ip address?\" alt=\"Fig.01: What is my ip address?\" src=\"http:\/\/files.cyberciti.biz\/uploads\/faq\/2010\/10\/geoip-what-is-my-IP-Address.png\" width=\"550\" height=\"198\" \/><\/a><\/p>\n<p>Fig.01: What is my ip address?<br \/>\nYou can see working\u00a0<a href=\"http:\/\/www.cyberciti.biz\/files\/what-is-my-ip-address.php\">example here<\/a>.<\/p>\n<p>&nbsp;<\/p>\n<h2>How Do I Find Out MaxMind Database Info?<\/h2>\n<p>Type the following command:<br \/>\n<code># \/usr\/local\/bin\/geoiplookup -v 1.2.3.4<\/code><br \/>\nSample outputs:<\/p>\n<pre>GeoIP Country Edition: GEO-106 20101001 Build 1 Copyright (c) 2010 MaxMind Inc All Rights Reserved<\/pre>\n<h2>How Do I Use Country Code in nginx Configuration?<\/h2>\n<p>You can use it as follows:<\/p>\n<pre>\u00a0\n### USA specific config ###\n if ($geoip_country_code = US) {\n    do something here for USA visitors;\n    # e.g. set root path \/var\/www\/html\/content\/usa\/;\n }\n\n### India specific config ###\n if ($geoip_country_code = IN) {\n    do something here for Indian visitors ;\n    # e.g. set root path \/var\/www\/html\/content\/india\/;\n }<\/pre>\n<h2>How Do I Test City Level Geo Targeting?<\/h2>\n<p>Use the following php code:<\/p>\n<pre>&lt;html&gt;\n&lt;head&gt;\n &lt;title&gt;What is my IP address - determine or retrieve my IP address&lt;\/title&gt;\n&lt;\/head&gt;\n&lt;body&gt;\n&lt;?php\n    if (<a href=\"http:\/\/www.php.net\/getenv\">getenv<\/a>(HTTP_X_FORWARDED_FOR)) {\n        $pipaddress = <a href=\"http:\/\/www.php.net\/getenv\">getenv<\/a>(HTTP_X_FORWARDED_FOR);\n        $ipaddress = <a href=\"http:\/\/www.php.net\/getenv\">getenv<\/a>(REMOTE_ADDR);\n        <a href=\"http:\/\/www.php.net\/echo\">echo<\/a> \"&lt;br&gt;Your Proxy IP address is : \".$pipaddress. \" (via $ipaddress) \" ;\n    } else {\n        $ipaddress = <a href=\"http:\/\/www.php.net\/getenv\">getenv<\/a>(REMOTE_ADDR);\n        <a href=\"http:\/\/www.php.net\/echo\">echo<\/a> \"&lt;br&gt;Your IP address is : $ipaddress\";\n    }\n  $geoip_city_country_code = <a href=\"http:\/\/www.php.net\/getenv\">getenv<\/a>(GEOIP_CITY_COUNTRY_CODE);\n  $geoip_city_country_code3 = <a href=\"http:\/\/www.php.net\/getenv\">getenv<\/a>(GEOIP_CITY_COUNTRY_CODE3);\n  $geoip_city_country_name = <a href=\"http:\/\/www.php.net\/getenv\">getenv<\/a>(GEOIP_CITY_COUNTRY_NAME);\n  $geoip_region = <a href=\"http:\/\/www.php.net\/getenv\">getenv<\/a>(GEOIP_REGION);\n  $geoip_city = <a href=\"http:\/\/www.php.net\/getenv\">getenv<\/a>(GEOIP_CITY);\n  $geoip_postal_code = <a href=\"http:\/\/www.php.net\/getenv\">getenv<\/a>(GEOIP_POSTAL_CODE);\n  $geoip_city_continent_code = <a href=\"http:\/\/www.php.net\/getenv\">getenv<\/a>(GEOIP_CITY_CONTINENT_CODE);\n  $geoip_latitude = <a href=\"http:\/\/www.php.net\/getenv\">getenv<\/a>(GEOIP_LATITUDE);\n  $geoip_longitude = <a href=\"http:\/\/www.php.net\/getenv\">getenv<\/a>(GEOIP_LONGITUDE);\n  <a href=\"http:\/\/www.php.net\/echo\">echo<\/a> \"&lt;br&gt;Country : $geoip_city_country_name ( $geoip_city_country_code3 , $geoip_city_country_code ) \";\n  <a href=\"http:\/\/www.php.net\/echo\">echo<\/a> \"&lt;br&gt;Region :  $geoip_region\";\n  <a href=\"http:\/\/www.php.net\/echo\">echo<\/a> \"&lt;br&gt;City :  $geoip_city \";\n  <a href=\"http:\/\/www.php.net\/echo\">echo<\/a> \"&lt;br&gt;Postal code :  $geoip_postal_code\";\n  <a href=\"http:\/\/www.php.net\/echo\">echo<\/a> \"&lt;br&gt;City continent code :  $geoip_city_continent_code\";\n  <a href=\"http:\/\/www.php.net\/echo\">echo<\/a> \"&lt;br&gt;Geoip latitude :  $geoip_latitude \";\n  <a href=\"http:\/\/www.php.net\/echo\">echo<\/a> \"&lt;br&gt;Geoip longitude :   $geoip_longitude \";\n\n?&gt;\n&lt;\/body&gt;\n&lt;\/html&gt;<\/pre>\n<p>Sample outputs:<\/p>\n<p><a href=\"http:\/\/www.cyberciti.biz\/faq\/linux-unix-nginx-geoip-module-configuration\/geocity-level-php-script-outout\/\" rel=\"attachment wp-att-8964\"><img decoding=\"async\" title=\"GeoCity Database PHP Script Output\" alt=\"Fig.02: GeoCity Database PHP Script Output\" src=\"http:\/\/files.cyberciti.biz\/uploads\/faq\/2010\/10\/geocity-level-php-script-outout.png\" width=\"372\" height=\"199\" \/><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Install MaxMind C API Type the following commands to install MaxMind C API: # cd \/tmp #\u00a0wget http:\/\/geolite.maxmind.com\/download\/geoip\/api\/c\/GeoIP.tar.gz # tar -zxvf GeoIP.tar.gz # cd GeoIP-1.4.6 #\u00a0yum install zlib-devel # .\/configure # make # make install You need to configure dynamic linker run time bindings as follows: # echo &#8216;\/usr\/local\/lib&#8217; &gt; \/etc\/ld.so.conf.d\/geoip.conf Run ldconfig to activate [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[60],"tags":[158],"class_list":["post-411","post","type-post","status-publish","format-standard","hentry","category-linux","tag-install-geoip-module-for-countrycity-level-geo-targeting-nginx"],"acf":[],"aioseo_notices":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.shineservers.com\/wp-json\/wp\/v2\/posts\/411","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.shineservers.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.shineservers.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.shineservers.com\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.shineservers.com\/wp-json\/wp\/v2\/comments?post=411"}],"version-history":[{"count":0,"href":"https:\/\/www.shineservers.com\/wp-json\/wp\/v2\/posts\/411\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.shineservers.com\/wp-json\/wp\/v2\/media?parent=411"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.shineservers.com\/wp-json\/wp\/v2\/categories?post=411"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.shineservers.com\/wp-json\/wp\/v2\/tags?post=411"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}