Gentoo下Nginx使用GeoIP模块为Django提供GEO信息
Tuesday, July 27th, 2010首先要在Gentoo下安装编译Nginx,需要加入GeoIP模块,请参考另一篇文章《Gentoo 安装编译Nginx》。在安装的过程中应该是会自动安装dev-libs/geoip这个包,如果没有的话使用emerge安装:
emerge -avt dev-libs/geoip
下载MaxMind的GEO数据,解压缩放在/etc/nginx目录下:
#Get the free database of geo_city wget http://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz gunzip GeoLiteCity.dat.gz mv GeoLiteCity.dat /etc/nginx #Get the free database of geo_coundty wget http://geolite.maxmind.com/download/geoip/database/GeoLiteCountry/GeoIP.dat.gz gunzip GeoIP.dat.gz mv GeoIP.dat /etc/nginx
编辑/etc/nginx/nginx.con文件,加入GeoIP的支持:
http {
geoip_country /etc/nginx/GeoIP.dat;
geoip_city /etc/nginx/GeoLiteCity.dat;
}编辑django的fastcgi_param:
fastcgi_param GEOIP_COUNTRY_CODE $geoip_city_country_code; fastcgi_param GEOIP_COUNTRY_CODE3 $geoip_country_code3; fastcgi_param GEOIP_COUNTRY_NAME $geoip_country_name; 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_LAT $geoip_latitude; fastcgi_param GEOIP_LNG $geoip_longitude;
在Django的使用方法:
def index(request): return HttpResponse(request.META['GEOIP_COUNTRY_CODE'])