|
|
d853946 |
==============================
|
|
|
d853946 |
ip based geographic lookups...
|
|
|
d853946 |
==============================
|
|
|
d853946 |
|
|
|
d853946 |
-----------------
|
|
|
d853946 |
Module: mod_geoip
|
|
|
d853946 |
-----------------
|
|
|
d853946 |
|
|
|
d853946 |
|
|
|
d853946 |
|
|
|
d853946 |
.. contents:: Table of Contents
|
|
|
d853946 |
|
|
|
d853946 |
Requirements
|
|
|
d853946 |
============
|
|
|
d853946 |
|
|
|
d853946 |
:Packages: GeoIP C API & Library (http://www.maxmind.com/download/geoip/api/c/)
|
|
|
d853946 |
|
|
|
d853946 |
|
|
|
d853946 |
|
|
|
d853946 |
|
|
|
d853946 |
Overview
|
|
|
d853946 |
============
|
|
|
d853946 |
|
|
|
d853946 |
mod_geoip is a module for fast ip/location lookups. It uses MaxMind GeoIP / GeoCity databases.
|
|
|
d853946 |
If the ip was found in the database the module sets the appropriate environments variables to the request, thus making other modules/fcgi be informed.
|
|
|
d853946 |
|
|
|
d853946 |
.. note::
|
|
|
d853946 |
|
|
|
d853946 |
Currently only country/city databases are supported because they have a free version that i can test.
|
|
|
d853946 |
|
|
|
d853946 |
(http://www.maxmind.com/download/geoip/database/ - GeoIP.dat.gz for Countries, GeoLiteCity.dat.gz for Countries & Cities)
|
|
|
d853946 |
|
|
|
d853946 |
|
|
|
d853946 |
Installation
|
|
|
d853946 |
============
|
|
|
d853946 |
|
|
|
d853946 |
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.
|
|
|
d853946 |
|
|
|
d853946 |
1) Download mod_geoip.c (http://trac.lighttpd.net/trac/attachment/wiki/Docs/ModGeoip/mod_geoip.c)
|
|
|
d853946 |
- (for lighttpd 1.5 use http://trac.lighttpd.net/trac/attachment/wiki/Docs/ModGeoip/mod_geoip.4.c and rename it to mod_geoip.c)
|
|
|
d853946 |
2) Copy mod_geoip.c into lighttpd src directory.
|
|
|
d853946 |
3) Edit your src/Makefile.am and add this after the last module:
|
|
|
d853946 |
|
|
|
d853946 |
::
|
|
|
d853946 |
|
|
|
d853946 |
lib_LTLIBRARIES += mod_geoip.la
|
|
|
d853946 |
mod_geoip_la_SOURCES = mod_geoip.c
|
|
|
d853946 |
mod_geoip_la_LDFLAGS = -module -export-dynamic -avoid-version -no-undefined
|
|
|
d853946 |
mod_geoip_la_LIBADD = $(common_libadd) -lGeoIP
|
|
|
d853946 |
|
|
|
d853946 |
4) 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
|
|
|
d853946 |
Note: the command of 'make clean' gives error on Fedora 4 and may not be necessary to compile properly. [j lightstone//GlobalAdSales.company]
|
|
|
d853946 |
|
|
|
d853946 |
5) Make sure /usr/local/lib is in your ld.so.conf file and rebuld the ld database (using: ldconfig).
|
|
|
d853946 |
|
|
|
d853946 |
6) Add to the config file server.modules = ("mod_geoip") [j lightstone//GlobalAdSales.company]
|
|
|
d853946 |
|
|
|
d853946 |
Configuration Options
|
|
|
d853946 |
========================
|
|
|
d853946 |
|
|
|
d853946 |
NOTE: in lighttpd 1.5.x add the config vars only to the global config - adding into a vhost crashes lighty!
|
|
|
d853946 |
|
|
|
d853946 |
mod_geoip uses two configuration options.
|
|
|
d853946 |
|
|
|
d853946 |
1) geoip.db-filename = <path to the geoip or geocity database>
|
|
|
d853946 |
2) geoip.memory-cache = <enable|disable> : default disabled
|
|
|
d853946 |
|
|
|
d853946 |
if enabled, mod_geoip will load the database binary file to
|
|
|
d853946 |
memory for very fast lookups. the only penalty is memory usage.
|
|
|
d853946 |
|
|
|
d853946 |
.. note::
|
|
|
d853946 |
|
|
|
d853946 |
mod_geoip will determine the database type automatically so if you enter GeoCity databse path it will load GeoCity Env.
|
|
|
d853946 |
|
|
|
d853946 |
Environment
|
|
|
d853946 |
===========
|
|
|
d853946 |
|
|
|
d853946 |
Every database sets it's own ENV:
|
|
|
d853946 |
|
|
|
d853946 |
GeoIP (Country):
|
|
|
d853946 |
----------------
|
|
|
d853946 |
|
|
|
d853946 |
::
|
|
|
d853946 |
|
|
|
d853946 |
GEOIP_COUNTRY_CODE
|
|
|
d853946 |
GEOIP_COUNTRY_CODE3
|
|
|
d853946 |
GEOIP_COUNTRY_NAME
|
|
|
d853946 |
|
|
|
d853946 |
GeoCity:
|
|
|
d853946 |
--------
|
|
|
d853946 |
|
|
|
d853946 |
::
|
|
|
d853946 |
|
|
|
d853946 |
GEOIP_COUNTRY_CODE
|
|
|
d853946 |
GEOIP_COUNTRY_CODE3
|
|
|
d853946 |
GEOIP_COUNTRY_NAME
|
|
|
d853946 |
GEOIP_CITY_NAME
|
|
|
d853946 |
GEOIP_CITY_POSTAL_CODE
|
|
|
d853946 |
GEOIP_CITY_LATITUDE
|
|
|
d853946 |
GEOIP_CITY_LONG_LATITUDE
|
|
|
d853946 |
GEOIP_CITY_DMA_CODE
|
|
|
d853946 |
GEOIP_CITY_AREA_CODE
|
|
|
d853946 |
|
|
|
d853946 |
Examples
|
|
|
d853946 |
========
|
|
|
d853946 |
|
|
|
d853946 |
mod_geoip + php
|
|
|
d853946 |
---------------
|
|
|
d853946 |
|
|
|
d853946 |
NOTE: in lighttpd 1.5.x add the config vars only to the global config - adding into a vhost crashes lighty!
|
|
|
d853946 |
|
|
|
d853946 |
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.
|
|
|
d853946 |
|
|
|
d853946 |
Config-file ::
|
|
|
d853946 |
|
|
|
d853946 |
geoip.db-filename = "/your-geoip-db-path/GeoLiteCity.dat"
|
|
|
d853946 |
geoip.memory-cache = "enable"
|
|
|
d853946 |
|
|
|
d853946 |
index.php ::
|
|
|
d853946 |
|
|
|
d853946 |
|
|
|
d853946 |
$country_code = $_SERVER['GEOIP_COUNTRY_CODE'];
|
|
|
d853946 |
$country_code3 = $_SERVER['GEOIP_COUNTRY_CODE3'];
|
|
|
d853946 |
$country_name = $_SERVER['GEOIP_COUNTRY_NAME'];
|
|
|
d853946 |
|
|
|
d853946 |
$city_region = $_SERVER['GEOIP_CITY_REGION'];
|
|
|
d853946 |
$city_name = $_SERVER['GEOIP_CITY_NAME'];
|
|
|
d853946 |
$city_postal_code = $_SERVER['GEOIP_CITY_POSTAL_CODE'];
|
|
|
d853946 |
$city_latitude = $_SERVER['GEOIP_CITY_LATITUDE'];
|
|
|
d853946 |
$city_long_latitude = $_SERVER['GEOIP_CITY_LONG_LATITUDE'];
|
|
|
d853946 |
$city_dma_code = $_SERVER['GEOIP_CITY_DMA_CODE'];
|
|
|
d853946 |
$city_area_code = $_SERVER['GEOIP_CITY_AREA_CODE'];
|
|
|
d853946 |
|
|
|
d853946 |
echo "<html>\n<body>\n\t \n";
|
|
|
d853946 |
echo 'Country Code: ' . $country_code . ' ';
|
|
|
d853946 |
echo 'Country Code 3: ' . $country_code3 . ' ';
|
|
|
d853946 |
echo 'Country Name: ' . $country_name . ' ';
|
|
|
d853946 |
echo ' ';
|
|
|
d853946 |
echo 'City Region: ' . $city_region . ' ';
|
|
|
d853946 |
echo 'City Name: ' . $city_name . ' ';
|
|
|
d853946 |
echo 'City Postal Code: ' . $city_postal_code . ' ';
|
|
|
d853946 |
echo 'City Latitude: ' . $city_latitude . ' ';
|
|
|
d853946 |
echo 'City Long Latitude: ' . $city_long_latitude . ' ';
|
|
|
d853946 |
echo 'City DMA Code: ' . $city_dma_code . ' ';
|
|
|
d853946 |
echo 'City Area Code: ' . $city_area_code . ' ';
|
|
|
d853946 |
echo "</body>\n</html>";
|
|
|
d853946 |
?>
|
|
|
d853946 |
|
|
|
d853946 |
country based redirect
|
|
|
d853946 |
----------------------
|
|
|
d853946 |
|
|
|
d853946 |
Config-file ::
|
|
|
d853946 |
|
|
|
d853946 |
$HTTP["host"] =~ "www.domain.com" {
|
|
|
d853946 |
url.rewrite = ( "" => "/redirect.php")
|
|
|
d853946 |
}
|
|
|
d853946 |
|
|
|
d853946 |
redirect.php ::
|
|
|
d853946 |
|
|
|
d853946 |
|
|
|
d853946 |
$country_code = $_SERVER['GEOIP_COUNTRY_CODE'];
|
|
|
d853946 |
header ('Location: http://' . $country_code . '.domain.com/');
|
|
|
d853946 |
?>
|
|
|
d853946 |
|
|
|
d853946 |
.. note::
|
|
|
d853946 |
|
|
|
d853946 |
Currently it is not possible to redirect based on mod_geoip directly in lighttpd config file. But i believe with the relase of lighttpd mod_magnet it would be. (mod_magnet will be available in lighttpd 1.4.12+)
|
|
|
d853946 |
|
|
|
d853946 |
Downloads
|
|
|
d853946 |
=========
|
|
|
d853946 |
mod_geoip.c (http://trac.lighttpd.net/trac/attachment/wiki/Docs/ModGeoip/mod_geoip.c)
|
|
|
d853946 |
|
|
|
d853946 |
mod_geoip.c for lighttpd 1.5.x (http://trac.lighttpd.net/trac/attachment/wiki/Docs/ModGeoip/mod_geoip.4.c)
|
|
|
3423d02 |
|