我在Centos 6.7服务器上使用nginx 1.8版本,但使用nginx -V命令时,我看不到geoip_module。 我怎样才能把它添加到nginx?
Nginx在apache的意义上没有“模块”,它必须用./configure中包含的模块进行重新编译。
这其实很简单 – 只需按照以下步骤操作:
通过使用以下命令安装构buildnginx的先决条件:
yum group install "Development Tools"
yum install gcc gd-devel GeoIP-devel
从http://nginx.org/en/download.html下载最新的nginx源代码
wget http://nginx.org/download/nginx-1.9.7.tar.gz
解压缩并进入源码树目录
tar xjvf nginx-1.9.7.tar.gz && cd nginx-1.9.7
获取已安装nginx的configuration参数(通过运行nginx -V ),为它们添加--with-http_geoip_module选项,或者使用以下命令进行configuration:
./configure --with-http_gzip_static_module --with-pcre --with-file-aio --without-http_scgi_module --without-http_uwsgi_module --without-http_fastcgi_module --user=nginx --group=nginx --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --conf-path=/etc/nginx/nginx.conf --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --with-http_image_filter_module --with-cc-opt="-march=native -mtune=native -O2 -pipe" --with-sha1-asm --with-zlib-asm=pentiumpro --with-md5-asm --with-pcre-jit --with-http_ssl_module --with-http_v2_module --with-http_stub_status_module --with-http_geoip_module
选项在这里解释
make && make install
现在你在你的nginx里有GeoIP支持。 要使用它,请从http://dev.maxmind.com/geoip/legacy/geolite/下载并解压数据库
curl http://geolite.maxmind.com/download/geoip/database/GeoLiteCountry/GeoIP.dat.gz | gzip -d - > /etc/nginx/GeoIP.dat
curl http://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz | gzip -d - > /etc/nginx/GeoLiteCity.dat
将它们添加到你的nginxconfiguration文件的http部分:
geoip_country /etc/nginx/GeoIP.dat;
geoip_city /etc/nginx/GeoLiteCity.dat;
最后定义标题,在你的nginxconfiguration文件的服务器部分将包含GeoIP信息:
proxy_set_header GEOIP_REGION $geoip_region;
proxy_set_header GEOIP_REGION_NAME $geoip_region_name;
proxy_set_header GEOIP_CITY $geoip_city;
proxy_set_header GEOIP_AREA_CODE $geoip_area_code;
proxy_set_header GEOIP_LATITUDE $geoip_latitude;
proxy_set_header GEOIP_LONGITUDE $geoip_longitude;
proxy_set_header GEOIP_POSTAL_CODE $geoip_postal_code;