Docs ModGeoip » History » Revision 37
Revision 36 (gstrauss, 2016-09-11 00:57) → Revision 37/39 (glen, 2016-10-16 15:05)
h1. IP Based Geographic Lookups {{>toc}} Module: mod_geoip h2. Requirements Packages: GeoIP C API & Library (http://www.maxmind.com/download/geoip/api/c/) h2. Overview mod_geoip is a module for fast ip/location lookups. It uses MaxMind GeoIP / GeoCity databases. If the ip was found in the database the module sets the appropriate environment environments variables to the request, thus making other modules/fcgi be informed. *NOTE:* Currently only country/city databases are supported because they have a free version that i can test. http://www.maxmind.com/download/geoip/database/ - GeoIP.dat.gz for Countries, GeoLiteCity.dat.gz for Countries & Cities h1. Installation *NOTE:* As of 1.4.14 the lighttpd distribution does not include the compile file required to perform a automake in the source directory. Use "automake -a" instead. # Download the source file (https://redmine.lighttpd.net/attachments/download/1689/mod_geoip_for_1.4.39.c) and rename it to "mod_geoip.c" # Copy mod_geoip.c into lighttpd src directory. # Edit your src/Makefile.am and add this after the last module: <pre> lib_LTLIBRARIES += mod_geoip.la mod_geoip_la_SOURCES = mod_geoip.c mod_geoip_la_LDFLAGS = -module -export-dynamic -avoid-version -no-undefined mod_geoip_la_LIBADD = $(common_libadd) -lGeoIP </pre> # Go back to lighttpd root directory and first do: aclocal && automake -a && autoconf, after that do: make clean; ./configure --enable-maintainer-mode; make; make install *NOTE:* the command of 'make clean' gives error on Fedora 4 and may not be necessary to compile properly. [j lightstone//GlobalAdSales.company] # Make sure /usr/local/lib is in your ld.so.conf file and rebuld the ld database (using: ldconfig). # Add to the config file server.modules = ("mod_geoip") [j lightstone//GlobalAdSales.company] *NOTE:* If using mod_proxy, add mod_geoip BEFORE mod_proxy. FYI: https://redmine.lighttpd.net/attachments/download/1689/mod_geoip_for_1.4.39.c includes patches from https://redmine.lighttpd.net/issues/1962 https://redmine.lighttpd.net/issues/2025 h2. Configuration Options mod_geoip uses two configuration options. # geoip.db-filename = <path to the geoip or geocity database> # geoip.memory-cache = <enable|disable> : default disabled if enabled, mod_geoip will load the database binary file to memory for very fast lookups. the only penalty is memory usage. *NOTE:* mod_geoip will determine the database type automatically so if you enter GeoCity database path it will load GeoCity Env. h2. Environment Every database sets it's own ENV: GeoIP (Country): <pre> GEOIP_COUNTRY_CODE GEOIP_COUNTRY_CODE3 GEOIP_COUNTRY_NAME </pre> GeoCity: <pre> GEOIP_COUNTRY_CODE GEOIP_COUNTRY_CODE3 GEOIP_COUNTRY_NAME GEOIP_CITY_NAME GEOIP_CITY_POSTAL_CODE GEOIP_CITY_LATITUDE GEOIP_CITY_LONG_LATITUDE GEOIP_CITY_DMA_CODE GEOIP_CITY_AREA_CODE </pre> h2. Examples mod_geoip + php when using fastcgi (not only php) you can access mod_geoip env and do as you please. this example just prints all mod_geoip envs to the client, html. Config-file <pre> geoip.db-filename = "/your-geoip-db-path/GeoLiteCity.dat" geoip.memory-cache = "enable" </pre> index.php <pre> <?php $country_code = $_SERVER['GEOIP_COUNTRY_CODE']; $country_code3 = $_SERVER['GEOIP_COUNTRY_CODE3']; $country_name = $_SERVER['GEOIP_COUNTRY_NAME']; $city_region = $_SERVER['GEOIP_CITY_REGION']; $city_name = $_SERVER['GEOIP_CITY_NAME']; $city_postal_code = $_SERVER['GEOIP_CITY_POSTAL_CODE']; $city_latitude = $_SERVER['GEOIP_CITY_LATITUDE']; $city_long_latitude = $_SERVER['GEOIP_CITY_LONG_LATITUDE']; $city_dma_code = $_SERVER['GEOIP_CITY_DMA_CODE']; $city_area_code = $_SERVER['GEOIP_CITY_AREA_CODE']; echo "<html>\n<body>\n\t<br>\n"; echo 'Country Code: ' . $country_code . '<br>'; echo 'Country Code 3: ' . $country_code3 . '<br>'; echo 'Country Name: ' . $country_name . '<br>'; echo '<br>'; echo 'City Region: ' . $city_region . '<br>'; echo 'City Name: ' . $city_name . '<br>'; echo 'City Postal Code: ' . $city_postal_code . '<br>'; echo 'City Latitude: ' . $city_latitude . '<br>'; echo 'City Long Latitude: ' . $city_long_latitude . '<br>'; echo 'City DMA Code: ' . $city_dma_code . '<br>'; echo 'City Area Code: ' . $city_area_code . '<br>'; echo "</body>\n</html>"; ?> </pre> +country based redirect+ Config-file <pre> $HTTP["host"] =~ "www.domain.com" { url.rewrite = ( "" => "/redirect.php") } </pre> redirect.php <pre> <?php $country_code = ( !empty( $_SERVER['GEOIP_COUNTRY_CODE'] ) ) ? $_SERVER['GEOIP_COUNTRY_CODE'] : 'US'; header( 'Location: http://'.strtolower( $country_code ).'.domain.com/' ); exit; ?> </pre> Currently redirecting based on mod_geoip through the lighttpd config file is possible with some patches: http://trac.lighttpd.net/trac/ticket/1546. An alternative would be to use mod_magnet. h2. Downloads