Country blocking using Nginx with GeoIP2 in Ubuntu 16.04

After try out the country block with multiple module, including the previous Nginx http_geoip_module, until I manage to figure it out and make it work, than only I found out that GeoIP version 1 will be stop update soon by Maxmind company, where they encourage developer to move to the next generation of IP database, which is GeoIP2. After sometime of figure out, finally I manage to make the module functioning as per what I expected.

The following are the step that I make it work from the fresh installation of ubuntu 16.04 server. Basically the following script did the following thing

  1. 1. Add the maxmind repository into the Ubuntu default repository list
  2. 2. Update the repo
  3. 3. Install all necessary module that needed in order to do the installation
  4. 4. Create the working directory in the home folder
  5. 5. Download the pcre source code, compile and install it
  6. 6. Download the zlib source code, compile and install it
  7. 7.  Download the openssl source code, compile and install it
  8. 8. Download the geoip2 module and decompress it
  9. 9. Download the Nginx 1.15.0 source code and decompress it
  10. 10. Configure the Nginx with all the necessary module
  11. 11. Compile and install the Nginx 1.15
  12. 12. Crate the service file and move to the  /etc/systemd/system/
  13. 13. Create the auto start for the Nginx when the server boot up
  14. 14. Download the free GeoIP2 database from Maxmind website and decompress it
  15. 15. Configure the Nginx.conf to include the GeoIP2 database
  16. 16. Create the block country list and include it into the virtual server configuration


sudo add-apt-repository ppa:maxmind/ppa &amp;&amp; \</p>
<p>sudo apt update &amp;&amp; sudo apt upgrade -y &amp;&amp; \</p>
<p>sudo apt install build-essential dh-autoreconf unzip wget curl libmaxminddb0 libmaxminddb-dev mmdb-bin checkinstall -y &amp;&amp; \</p>
<p>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 />
sudo mv ngx_http_geoip2_module-master ngx_http_geoip2_module &amp;&amp; \<br />
sudo rm &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 \<br />
--prefix=/usr/share/nginx \<br />
--sbin-path=/usr/sbin/nginx \<br />
--modules-path=/usr/lib/nginx/modules \<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=/var/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 \<br />
--add-module=../ngx_http_geoip2_module \<br />
--with-cc-opt='-g -O2 -fstack-protector --param=ssp-buffer-size=4 -Wformat -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2' \<br />
--with-ld-opt='-Wl,-z,relro -Wl,--as-needed' &amp;&amp; \</p>
<p>sudo make &amp;&amp; \<br />
sudo make install &amp;&amp; \<br />
sudo mkdir -p /var/lib/nginx &amp;&amp; \<br />
sudo ldconfig &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; &amp;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 mkdir /etc/nginx/geoip &amp;&amp; cd /etc/nginx/geoip &amp;&amp; \</p>
<p>sudo wget &amp;&amp; \</p>
<p>sudo gunzip GeoLite2-Country.mmdb.gz


After the above script finish run, you should have your Nginx 1.15.0 + ngx_http_geoip2_module + GeoLite2 database ready in your server. If you wish to double check is the ngx_http_geoip2_module install and ready in your Nginx, you may always run the following command, you should see the module highlighted in red color, if not, you may need to go back to the above script and try to run it line by line to debug what’s going wrong.

nginx -V 2&gt;&1|grep --color=always ngx_http_geoip2_module


Once confirm the Nginx module is success install, we may proceed to do the configuration on the Nginx.conf, add the following script into your nginx.conf (Normally will be at the /etc/nginx/ directory)

sudo vim /etc/nginx/nginx.conf

Add the following script into the file

<p>geoip2 /etc/nginx/geoip/GeoLite2-Country.mmdb{<br />
$geoip2_data_country_code default=US country iso_code;<br />
$geoip2_data_country_name country names en;<br />
<p>include /etc/nginx/geoip/whitelistip;</p>
<p>include /etc/nginx/GeoIP/geoblocker;</p>


Once done to include the GeoIP2 database into the Nginx, than we may create the whitelist file

sudo vim /etc/nginx/geoip/whitelistip


Add in the following script into the file

geo $whitelistip {<br />
default 0;<br /> 1;<br /> 1;<br /> 1;<br />
}<br />


Create another file to store the logic of the country block, so that you may include it for more than one server directive

sudo vim /etc/nginx/geoip/geoblocker


Add in the following script into the file

<p>set $geoblock 0;<br />
if ($geoip2_data_country_code!~ (DK|NO|SE)) {<br />
set $geoblock 1;<br />
<p>if ($whitelistip = 1){<br />
set $geoblock 0;<br />
<p>if ($geoblock = 1){<br />
rewrite ^;<br />


You may try using any browser to access your Nginx web server, if the IP is not block, you should get the default nginx page such as following.


If you add your country code to block list, than you should be redirect to Google home page.


Hopefully this sharing help and please do leave your comment if you have any better suggestion to share with me or other.

Leave a Reply

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

%d bloggers like this: