How should I add GeoIP module to nginx?
I'm using nginx version 1.8 on a centos 6.7 server but when using nginx -V command , I can't see geoip_module there . How can I add it to nginx ?
Nginx doesn't have "modules" in apache sense, it has to be recompiled with the module you need included during ./configure.
It's actually pretty easy - just follow this steps:
-
Install prerequisites for building nginx by using the following commands:
yum group install "Development Tools"
yum install gcc gd-devel GeoIP-devel
-
Download latest nginx source from http://nginx.org/en/download.html
wget http://nginx.org/download/nginx-1.15.7.tar.gz
-
Unpack it & enter the source tree directory
tar xzfv nginx-1.15.7.tar.gz && cd nginx-1.15.7
-
Get the configuration arguments of your installed nginx (by running
nginx -V
), add the--with-http_geoip_module
option to them or just configure with the following command:./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
options are explained here
make && make install
-
Now you have the GeoIP support in your nginx. To use it, download and unpack databases from 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
-
Add them to the http section of your nginx config file:
geoip_country /etc/nginx/GeoIP.dat;
geoip_city /etc/nginx/GeoLiteCity.dat;
-
And finally define headers, which will contain the GeoIP information in the server section of your nginx config file:
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;
- create a new file , /etc/yum.repos.d/nginx.repo
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/6/$basearch/
gpgcheck=0
enabled=1
if your system version is centos 7.x ,you need change it in the 'baseurl'
- then ,
yum install nginx-module-geoip
you got it