Country blocking using Nginx with IP2Location Module

After few days of trying, trying and trying, finally find out the way to compile the IP2Location module into the Nginx in Ubuntu server. I try to compare multiple way of doing the country block with Nginx, where in my previous post, the country blocking is done with GeoIP module (which I found out more easy compare to the IP2Location module), and also the country block without any 3rd party plugin for Nginx, with this method, that’s no any compilation of Nginx needed in order to make it, it’s most suitable for the current running Nginx which install directly from the APT repo.

IP2Location is one of the most famous IP location provider in the market and they are from Penang. I being use their service since many years ago and that’s the reason why I wanna try out to doing the Nginx country block with IP2Location module.

I can’t really find a full and complete step on the installing and compiling of the module, that’s why it take me many days to figure out how to make the whole thing work nicely.

The following script basically just download all necessary module source code (OpenSSL, PCRE, IP2Location C module, IP2Lacation Nginx module, Nginx 1.15 .0 source code) and compile all. After that, create the system service and put the Nginx into auto start mode and include the IP2Location database into Nginx.conf. Beside that, I also include the script to auto download the database directly from IP2Location server.


<br />
sudo apt update &amp;&amp; sudo apt upgrade -y &amp;&amp; \<br />
sudo apt install build-essential dh-autoreconf unzip -y &amp;&amp; \<br />
sudo mkdir nginx-dev &amp;&amp; cd nginx-dev &amp;&amp; \</p>
<p>sudo wget &amp;&amp; \<br />
sudo tar -zxf pcre-8.42.tar.gz &amp;&amp; \<br />
cd pcre-8.42 &amp;&amp; \<br />
sudo ./configure &amp;&amp; \<br />
sudo make &amp;&amp; \<br />
sudo make install &amp;&amp; \<br />
cd .. &amp;&amp; \</p>
<p>sudo wget &amp;&amp; \<br />
sudo tar -zxf zlib-1.2.11.tar.gz &amp;&amp; \<br />
cd zlib-1.2.11 &amp;&amp; \<br />
sudo ./configure &amp;&amp; \<br />
sudo make &amp;&amp; \<br />
sudo make install &amp;&amp; \<br />
cd .. &amp;&amp; \</p>
<p>sudo wget &amp;&amp; sudo tar xzvf openssl-1.0.2o.tar.gz &amp;&amp; \<br />
cd openssl-1.0.2o &amp;&amp; \<br />
sudo ./config --prefix=/usr &amp;&amp; \<br />
sudo make &amp;&amp; \<br />
sudo make install &amp;&amp; \<br />
cd .. &amp;&amp; \</p>
<p>sudo rm *.gz &amp;&amp; \</p>
<p>sudo wget &amp;&amp; \<br />
sudo unzip &amp;&amp; \<br />
cd IP2Location-C-Library-master &amp;&amp; \<br />
sudo autoreconf -i -v --force &amp;&amp; \<br />
sudo ./configure &amp;&amp; \<br />
sudo make &amp;&amp; \<br />
sudo make install &amp;&amp; \<br />
cd ~/nginx-dev &amp;&amp; \<br />
sudo rm &amp;&amp; \</p>
<p>sudo wget &amp;&amp; \<br />
sudo unzip &amp;&amp; \<br />
sudo rm &amp;&amp; \<br />
sudo sed -i 's/#include &quot;IP2Location.h&quot;/#include &quot;\/home\/ubuntu\/nginx-dev\/IP2Location-C-Library-master\/libIP2Location\/IP2Location.h&quot;/g' ~/nginx-dev/ip2location-nginx-master/ngx_http_ip2location_module.c &amp;&amp; \<br />
sudo mkdir /etc/ip2location &amp;&amp; cd /etc/ip2location &amp;&amp; \</p>
<p>sudo curl -o DB1-IP-COUNTRY.BIN.ZIP &quot;;file=DB1BIN&quot; &amp;&amp; \<br />
sudo unzip -o DB1-IP-COUNTRY.BIN.ZIP &amp;&amp; \<br />
sudo rm !(*.BIN) &amp;&amp; \</p>
<p>cd ~/nginx-dev &amp;&amp; \<br />
sudo wget &amp;&amp; \<br />
sudo tar zxf nginx-1.15.0.tar.gz &amp;&amp; \<br />
cd nginx-1.15.0 &amp;&amp; \</p>
<p>sudo ./configure --prefix=/usr/share/nginx \<br />
--sbin-path=/usr/sbin/nginx \<br />
--modules-path=/usr/lib/nginx/modules \<br />
--add-module=../ip2location-nginx-master \<br />
--conf-path=/etc/nginx/nginx.conf \<br />
--error-log-path=/var/log/nginx/error.log \<br />
--http-log-path=/var/log/nginx/access.log \<br />
--pid-path=/run/ \<br />
--lock-path=/var/lock/nginx.lock \<br />
--user=www-data \<br />
--group=www-data \<br />
--build=Ubuntu \<br />
--http-client-body-temp-path=/var/lib/nginx/body \<br />
--http-fastcgi-temp-path=/var/lib/nginx/fastcgi \<br />
--http-proxy-temp-path=/var/lib/nginx/proxy \<br />
--http-scgi-temp-path=/var/lib/nginx/scgi \<br />
--http-uwsgi-temp-path=/var/lib/nginx/uwsgi \<br />
--with-openssl=../openssl-1.0.2o \<br />
--with-openssl-opt=enable-ec_nistp_64_gcc_128 \<br />
--with-openssl-opt=no-nextprotoneg \<br />
--with-openssl-opt=no-weak-ssl-ciphers \<br />
--with-openssl-opt=no-ssl3 \<br />
--with-pcre=../pcre-8.42 \<br />
--with-pcre-jit \<br />
--with-zlib=../zlib-1.2.11 \<br />
--with-compat \<br />
--with-file-aio \<br />
--with-threads \<br />
--with-http_addition_module \<br />
--with-http_auth_request_module \<br />
--with-http_dav_module \<br />
--with-http_flv_module \<br />
--with-http_gunzip_module \<br />
--with-http_gzip_static_module \<br />
--with-http_mp4_module \<br />
--with-http_random_index_module \<br />
--with-http_realip_module \<br />
--with-http_slice_module \<br />
--with-http_ssl_module \<br />
--with-http_sub_module \<br />
--with-http_stub_status_module \<br />
--with-http_v2_module \<br />
--with-http_secure_link_module \<br />
--with-mail \<br />
--with-mail_ssl_module \<br />
--with-stream \<br />
--with-stream_realip_module \<br />
--with-stream_ssl_module \<br />
--with-stream_ssl_preread_module \<br />
--with-debug &amp;&amp; \</p>
<p>sudo make &amp;&amp; \<br />
sudo make install &amp;&amp; \<br />
sudo ldconfig &amp;&amp; \<br />
sudo nginx -v &amp;&amp; sudo nginx -V &amp;&amp; \<br />
sudo mkdir -p /var/lib/nginx &amp;&amp; sudo nginx -t &amp;&amp; \</p>
<p>sudo echo &quot;<br />
[Unit]<br />
Description=A high performance web server and a reverse proxy server<br /></p>
<p>[Service]<br />
Type=forking<br />
PIDFile=/run/<br />
ExecStartPre=/usr/sbin/nginx -t -q -g 'daemon on; master_process on;'<br />
ExecStart=/usr/sbin/nginx -g 'daemon on; master_process on;'<br />
ExecReload=/usr/sbin/nginx -g 'daemon on; master_process on;' -s reload<br />
ExecStop=-/sbin/start-stop-daemon --quiet --stop --retry QUIT/5 --pidfile /run/<br />
TimeoutStopSec=5<br />
<p>[Install]<br /> &quot; &gt; ~/nginx.service &amp;&amp; \</p>
<p>sudo mv ~/nginx.service /etc/systemd/system/nginx.service &amp;&amp; \</p>
<p>sudo systemctl start nginx.service &amp;&amp; sudo systemctl enable nginx.service &amp;&amp; \</p>
<p>sudo sed -i 's/default_type application\/octet-stream;/default_type application\/octet-stream;\nip2location on;\nip2location_database \/etc\/ip2location\/IP-COUNTRY.BIN;\nip2location_access_type shared_memory;/g' /etc/nginx/nginx.conf


After finish run above command, just add the following script into your Nginx.conf or your website virtual host directive in or to make rewrite all incoming traffic to your desire destination

<br />
server{<br />
listen 80;<br />
server_name _;</p>
<p>if ($ip2location_country_short ~ ^MY$) {<br />
rewrite ^(.*)$ last;<br />
}<br />
}<br />


Finally you may testing your configure by browsing the server IP using any of your browse, if you are from Malaysia, than you should be redirected to Google home page

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: