CentOSLinuxTutorials

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.  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 activate 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 the step-by-step installation of the Nginx With GeoIP Module on the CentOS or RHEL-based system.

Prerequisites

  • A server running one of the following operating systems: CentOS or RHEL-based.
  • It’s recommended that you use a fresh OS install to prevent any potential issues.
  • SSH access to the server (or just open Terminal if you’re on a desktop).
  • A non-root sudo user or access to the root user. We recommend acting as a non-root sudo user, however, as you can harm your system if you’re not careful when acting as the root.

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
sudo yum install geoip geoip-devel

Step 2. Installing Nginx from Source.

The GeoIP module is not included in the default Nginx package. Therefore, you need to download the Nginx source code and compile it with the GeoIP module. You can download the latest version of Nginx from the official website or use the following command:

cd /opt/nginx/
wget https://nginx.org/download/nginx-1.22.1.tar.gz

After downloading the Nginx source code, you need to compile it with the GeoIP module. You can do this by running the following commands:

tar -xzvf nginx-1.22.1.tar.gz
cd nginx-1.20.1
./configure --with-http_geoip_module
make
sudo make install

Step 3. Configure Nginx With the GeoIP Database.

You need to configure it to use the GeoIP database. You can do this by adding the following lines to your Nginx configuration file:

http {
  geoip_country /usr/share/GeoIP/GeoIP.dat;
  geoip_city /usr/share/GeoIP/GeoLiteCity.dat;
  ...
}

These lines will configure Nginx to use the GeoIP database for country and city information. You can change the path of the GeoIP database to match your system configuration.

Save and close the file, then restart the Nginx service to take change effect:

nginx -t
sudo systemctl restart nginx

Once is done, now you can access a webpage and check the GeoIP information by using the curl command:

curl -I your-domain.com

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!

r00t

r00t is an experienced Linux enthusiast and technical writer with a passion for open-source software. With years of hands-on experience in various Linux distributions, r00t has developed a deep understanding of the Linux ecosystem and its powerful tools. He holds certifications in SCE and has contributed to several open-source projects. r00t is dedicated to sharing her knowledge and expertise through well-researched and informative articles, helping others navigate the world of Linux with confidence.
Back to top button