How should I add GeoIP module to nginx?

11,450

Solution 1

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:

  1. Install prerequisites for building nginx by using the following commands:

    yum group install "Development Tools"

    yum install gcc gd-devel GeoIP-devel

  2. Download latest nginx source from http://nginx.org/en/download.html

    wget http://nginx.org/download/nginx-1.15.7.tar.gz

  3. Unpack it & enter the source tree directory

    tar xzfv nginx-1.15.7.tar.gz && cd nginx-1.15.7

  4. 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

  5. make && make install

  6. 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

  7. Add them to the http section of your nginx config file:

    geoip_country /etc/nginx/GeoIP.dat;

    geoip_city /etc/nginx/GeoLiteCity.dat;

  8. 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;

Solution 2

  1. 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'

  1. then , yum install nginx-module-geoip

you got it

Share:
11,450

Related videos on Youtube

movi ran
Author by

movi ran

Updated on September 18, 2022

Comments

  • movi ran
    movi ran almost 2 years

    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 ?

  • Chris
    Chris about 5 years
    If nginx is already installed and running and one follows your steps above, does this simply overwrite existing install? Config losses?
  • Anubioz
    Anubioz about 5 years
    @Chris nope, it only overwrites nginx binariesm. Make sure you use exact configuration directives from your system in step 4 though