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

How to use CSS to display multiple images horizontally in the center

Let me first talk about the implementation steps:...

Vue SPA first screen optimization solution

Table of contents Preface optimization SSR Import...

Solution to the lack of my.ini file in MySQL 5.7

What is my.ini? my.ini is the configuration file ...

Method of implementing recursive components based on Vue technology

describe This article introduces a method to impl...

Create a movable stack widget function using flutter

This post focuses on a super secret Flutter proje...

How does MySQL achieve multi-version concurrency?

Table of contents MySQL multi-version concurrency...

Simple analysis of EffectList in React

Table of contents EffectList Collection EffectLis...

Problems with Vue imitating Bibibili's homepage

Engineering Structure The project is divided into...

How to generate mysql primary key id (self-increment, unique and irregular)

Table of contents 1. Use the uuid function to gen...

Shell script nginx automation script

This script can satisfy the operations of startin...

In-depth understanding of this in JavaScript

In-depth understanding of this in Js JavaScript s...

How to make Python scripts run directly under Ubuntu

Let’s take the translation program as an example....