How To Install Nginx With GeoIP Module

Install Nginx With GeoIP Module

In this tutorial, we will show you how to install Nginx With GeoIP Module on CentOS.  For those of you who didn’t know, the Nginx GeoIP module for country and city geo-targeting can be installed in a few easy steps. It brings you a geo-targeting layer allowing you to show some parts of your websites, or even split traffic according to the geographical location of the end-users. By default, when you install modules from yum, Nginx will not come with the GeoIP module (This is the module: HttpGeoipModule), so we will install from source and the active the module.

This article assumes you have at least basic knowledge of Linux, know how to use the shell, and most importantly, you host your site on your own VPS. The installation is quite simple and assumes you are running in the root account, if not you may need to add ‘sudo‘ to the commands to get root privileges. I will show you through the step-by-step installation of the Nginx With GeoIP Module on the CentOS system.

Install Nginx With GeoIP Module

Step 1. First, let’s start by ensuring your system is up-to-date.

sudo yum install epel-release
sudo yum update

Step 2. Installing All Requires Dependency.

Now install requires a package for compiling:

yum install gcc-c++ pre pcre-devel zlib zlib-devel -y

Step 3. Download and Install Nginx.

Now we download the latest stable version of Nginx from here and build it with GeoIP module support.

cd /opt/nginx/
tar -zxf nginx-1.21.5.tar.gz
cd nginx-1.21.5/
--prefix=/etc/nginx \
--sbin-path=/etc/nginx/sbin/nginx \
--conf-path=/etc/nginx/conf/nginx.conf \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--pid-path=/var/run/ \
--lock-path=/var/run/ninx.lock \
--user=nobody \
--with-http_geoip_module \
--with-http_gzip_static_module \
--with-http_secure_link_module \
--without-mail_pop3_module \
--without-mail_imap_module \
--without-mail_smtp_module \

Once done compile, next install Nginx using the following command:

make install

Step 4. Create an init script for Nginx.

First, add the user Nginx to the system. This is a one time command:

useradd -r nginx

We need to set up the file /etc/init.d/nginx to run when the system starts:

wget -O /etc/init.d/nginx
chmod 0755 /etc/init.d/nginx
chown root:root /etc/init.d/nginx

Finally, start the Nginx:

systemctl restart nginx

Step 5. Installing GeoIP library on CentOS system.

Now run the following commands to install the GeoIP library

sudo yum install geoip geoip-devel

After successful installation, the library will be stored in /usr/share/GeoIP/GeoIP.dat For the latest updates can be downloaded here.

Step 5. Configure Nginx

Next, we configure the main file:

nano /etc/nginx/conf/nginx.conf

Add the following file:

http {
geoip_country /usr/share/GeoIP/GeoIP.dat;
map $geoip_country_code $allowed_country {
default yes;
CN no;

After that, we configure Nginx virtual host:

nano /etc/nginx/conf.d/yourdomain.conf

Add the following line:

server {
if ($allowed_country = no) {
return 444;
# # This means the server will stop processing, returns error 444 (The connection was reset),
# # And ignore always sending the response header.
# # Replace 444 by 403 if you want

The above configuration will accept all IPs and ban only from China IP (CN). About the Code of the country in the GeoIP database, you can refer here.

Congratulations! You have successfully installed Nginx With GeoIP Module. Thanks for using this tutorial for installing Nginx With GeoIP Module on the Linux system. For additional help or useful information, we recommend you check the official Nginx website.

Nginx Manage Service Offer
If you don’t have time to do all of this stuff, or if this is not your area of expertise, we offer a service to do “Nginx Manage Service Offer”, starting from $10 (Paypal payment). Please contact us to get the best deal!