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 theroot user
. We recommend acting as anon-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.