How to use geoip to restrict regions in nginx

How to use geoip to restrict regions in nginx

This blog is a work note

environment:

nginx version: nginx/1.14.0

centos version: centos7

The requirements are as follows:

Differentiate between domestic and foreign IP addresses, and then jump to different pages. Finally, use nginx's third-party module: geoip to achieve this. I won't talk about its advantages. There are many explanations on the Internet. Let's see how to configure it.

My system is configured with nignx.repo. I installed the geoip module directly using yum, without adding the module and recompiling it.

yum install nginx-module-geoip

Download the geoip database file

cd /etc/nginx
mkdir geoipdat
cd geoipdat

Download wget http://geolite.maxmind.com/download/geoip/database/GeoLiteCountry/GeoIP.dat.gz
wget http://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz

Unzip gunzip GeoIP.dat.gz
gunzip GeoLiteCity.dat.gz

Configure nginx according to your needs

First, load the geoip library in nginx.conf and configure it as follows:

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

user nginx;
worker_processes 1;

error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;


events {
  worker_connections 1024;
}


http {
  include /etc/nginx/mime.types;
  default_type application/octet-stream;

  log_format main '$remote_addr - $remote_user [$time_local] "$request" '
           '$status $body_bytes_sent "$http_referer" '
           '"$http_user_agent" "$http_x_forwarded_for"';

  access_log /var/log/nginx/access.log main;

  sendfile on;
  #tcp_nopush on;

  keepalive_timeout 65;

  #gzip on;

  include /etc/nginx/conf.d/*.conf;
}

Configure the virtual host as follows:

geoip_country /etc/nginx/geoipdat/GeoIP.dat;
geoip_city /etc/nginx/geoipdat/GeoLiteCity.dat;


server {
  listen 80;
  server_name localhost;
  location / {
 root /opt;
 if ($geoip_country_code = CN){
 rewrite (.*) /zh$1 break;
 }
    rewrite (.*) /en$1 break;
  }
    error_page 500 502 503 504 /50x.html;
  location = /50x.html {
    root /usr/share/nginx/html;
  }

}

The opt directory is as follows

[root@VM_0_15_centos opt]# tree
.
|
└── en
│ └── index.html
└── en
  └── index.html

The above is just a simple configuration. . . .

The above method of using geoip for regional restriction in nginx 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 restricts IP access to certain pages
  • How to configure nginx to limit the access frequency of the same IP
  • Analysis of Nginx server's method of restricting access through ip and user_gent
  • How to handle malicious calls to SMS interface by nginx restricted IP
  • Nginx limits the number of visits and requests to a certain IP in the same time period. Example code
  • A complete analysis of various situations in which Nginx server restricts IP access
  • How to limit concurrent IP and download speed in Nginx
  • How to set up nginx and apache to limit concurrent IP access and limit IP connections
  • Limiting the number of short-term accesses to a certain IP based on Nginx

<<:  MySQL 5.7.16 free installation version installation and configuration method graphic tutorial

>>:  Solution to the paging error problem of MySQL one-to-many association query

Recommend

MySQL encoding utf8 and utf8mb4 utf8mb4_unicode_ci and utf8mb4_general_ci

Reference: MySQL character set summary utf8mb4 ha...

Example of writing mobile H5 to invoke APP (IOS, Android)

iOS 1. URL scheme This solution is basically for ...

HTML table mouse drag sorting function

Effect picture: 1. Import files <script src=&q...

Steps to set up Windows Server 2016 AD server (picture and text)

Introduction: AD is the abbreviation of Active Di...

CSS3 flexible box flex to achieve three-column layout

As the title says: The height is known, the width...

JS realizes special effects of web page navigation bar

This article shares with you a practical web navi...

Detailed analysis of Vue child components and parent components

Table of contents 1. Parent components and child ...

Vue implements multi-grid input box on mobile terminal

Recently, the company has put forward a requireme...

MySQL Workbench download and use tutorial detailed explanation

1. Download MySQL Workbench Workbench is a graphi...

Implementation steps for docker-compose to deploy etcd cluster

Table of contents Write docker-compose.yml Run do...

Detailed explanation of the new CSS display:box property

1. display:box; Setting this property on an eleme...

MySQL 8.0.22 decompression version installation tutorial (for beginners only)

Table of contents 1. Resource download 2. Unzip t...