How to read the regional information of IP using Nginx and GeoIP module

How to read the regional information of IP using Nginx and GeoIP module

Install GeoIP on Linux

yum install nginx-module-geoip

http_geoip_module usage scenarios

1. Differentiate between domestic and foreign HTTP access rules

2. Differentiate domestic cities and regions to make HTTP access rules

After yum is installed, find the installed module file

If nginx is installed using yun, it is usually installed in the /etc/nginx/modules/ directory

Note: If nginx is not installed by yum but compiled from source code, you need to reinstall and compile nginx with this module, and then you do not need to add this module manually.

Manually add modules

The module is loaded at the same level as http in the header of the nginx.conf configuration file

load_module "modules/ngx_http_geoip_module.so";
load_module "modules/ngx_stream_geoip_module.so";

Because GeoIP is based on the database file provided by MaxMind to read the regional information, it is necessary to download the regional file of the IP.

This database is binary and cannot be opened with a text editor. The GeoIP library above is required to read it.

wget http://geolite.maxmind.com/download/geoip/database/GeoLiteCountry/GeoIP.dat.gz #Country's regional IP
wget http://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz #City's regional IP

Then unzip

load_module "modules/ngx_http_geoip_module.so";
load_module "modules/ngx_stream_geoip_module.so";

.......


http{
geoip_country /etc/nginx/geoip/GeoIP.dat; #Load country IP
geoip_city /etc/nginx/geoip/GeoLiteCity.dat; #Load city IP

.........

 server
 {
 ......



 location / {
 #If it is not Chinese, return 403;
 if ($geoip_country_code != CN) {
  return 403;
 }
 }
 #Return country and city information location /myip {
 default_type text/plain;
 return 200 "$remote_addr $geoip_country_name $geoip_country_code $geoip_city";
 }



....
 }
}

Then visit your IP address/myip to return the country and city information of the IP.

The above method of using Nginx and GeoIP module to read the regional information of IP is all the content that the editor shares with you. I hope it can give you a reference. I also hope that you will support 123WORDPRESS.COM.

You may also be interested in:
  • Nginx geoip module to achieve regional load balancing

<<:  Implementation of communication between Vue and Flask

>>:  Vue-pdf implements online preview of PDF files

Recommend

Docker advanced method of rapid expansion

1. Command method Run the nginx service in the cr...

Explanation of building graph database neo4j in Linux environment

Neo4j (one of the Nosql) is a high-performance gr...

Native JavaScript to achieve skinning

The specific code for implementing skinning with ...

Writing High-Quality Code Web Front-End Development Practice Book Excerpts

(P4) Web standards are composed of a series of sta...

Detailed explanation of creating, calling and managing MySQL stored procedures

Table of contents Introduction to stored procedur...

Let's talk about the difference between MyISAM and InnoDB

The main differences are as follows: 1. MySQL use...

A brief talk about JavaScript Sandbox

Preface: Speaking of sandboxes, our minds may ref...

Solution for adding iptables firewall policy to MySQL service

If your MySQL database is installed on a centos7 ...

MySQL operations: JSON data type operations

In the previous article, we introduced the detail...

Detailed explanation of HTML basic tags and structures

1. HTML Overview 1.HTML: Hypertext Markup Languag...

Problems and solutions when installing MySQL8.0.13 on Win10 system

Operating system: Window10 MySQL version: 8.0.13-...

How to install binary MySQL on Linux and crack MySQL password

1. Make sure the system has the required libaio s...