Blog

  • My client wanted me route traffic in such a way that when a customer access somedomain.com from Africa then it must redirect him to somedomain.africa.

    I looked for many blogs but did not find all the things at one place so I am representing everything required for this.

     

    I am using nginx 1.14.0 which is the current latest at time of writing but this will work for other versions also.

     

    Step 1: Instal nginx

    sudo add-apt-repository ppa:nginx/stable
    sudo apt update 
    sudo apt install nginx
    

     

    Step 2: Download source code and compile nginx --with-http_geoip_module headers  

    Since we installed nginx with apt ,and the default nginx does not include the required headers  will have to download the source code of nginx and recompile it with --with-http_geoip_module.

    Download tarball

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

    Extract tarball

    tar -zxvf nginx-1.14.0.tar.gz
    

    Download the source code for module that needs to be compiled

    git clone https://github.com/openresty/headers-more-nginx-module.git

     

    Get the current header of nginx
     

    nginx  -V
    

     

    Compile the code with headers

    cd nginx-1.14.0
    ./configure --add-dynamic-module=./headers-more-nginx-module  < paste the header you copied using nginx -V > 
    make modules
    sudo apt install  libgeoip-dev
    


    Step 3: copy obj.so to /etc/nginx

    sudo cp objs/ngx_http_headers_more_filter_modules.so /etc/nginx/modules
    

     

    Step 4: add let the nginx know of the updates  by adding below line in nginx.conf before http

    Load_module modules/ngx_http_headers_more_filter_modules.so
    

    Step 5: Download the geoip.dat from maxmind

    sudo mkdir /etc/nginx/geoip
    cd /etc/nginx/geoip
    

     

    Go to https://dev.maxmind.com/

    Download city database, you can also download country database if you want to play with countries, in my case I need continent code which is in city database.

    wget http://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz 
    Gunzip GeoLiteCity.dat.gz
    

     

    Step 6:  edit your nginx file that contains server info

     vim /etc/nginx/sites-available/default
    #Add the following line outside server block
    
    geoip_city  /etc/nginx/geoip/GeoLiteCity.dat;
    
    #In the server block add geoip_city_continent_code to check for continent code 
    
    geoip_country /etc/nginx/geoip/GeoIP.dat;
    geoip_city /etc/nginx/geoip/GeoLiteCity.dat;
    
    server {
             server_name somedomain.com;
             listen 80;
             root /var/www/dist;
             index index.html;
    
             if ($geoip_city_continent_code = AF){
             return 301 http://somedomain.africa;
                  }
             location / {
                      try_files $uri $uri/ /index.html;
                      }
     }
    
    

      

    Save and exit

     

    Important Note : The maxmind database is free up to jan 2019 only after that you need to buy the same.

     

    Step 7: chek and reload the nginx service

    sudo nginx -t  &&
    sudo service nginx reload
    

     

     

    Now get connected to any vpn in African country which is not free and check the redirection by visiting your browser.

    Please also have a look at risks of if command at  https://www.nginx.com/resources/wiki/start/topics/depth/ifisevil/

    Thank You, Have a Great Day.

     

Tags: nginx