Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use geoip for area restriction in nginx

Shulou Source: shulou.com Published: 2022-06-03 11:01:11 11月14日 Update

This article mainly introduces nginx how to use geoip to do regional restrictions, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let Xiaobian take you to understand.

The GeoIP filter adds information about the geographic location of the IP address based on data from the Maxmind GeoLite2 database.

Through IP to distinguish between domestic and foreign, so as to jump to different pages, and finally use nginx's third-party module:geoip to achieve, not to mention its advantages, there are many explanations online, let's see how to configure (recommended learning: nginx use)

My system is configured with nignx.repo, I directly use yum to install the geoip module, not by adding module reprogramming

Yum install nginx-module-geoip

Download the database file for geoip

Download cd / etc/nginxmkdir geoipdatcd geoipdat to download wget http://geolite.maxmind.com/download/geoip/database/GeoLiteCountry/GeoIP.dat.gzwget http://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz and extract gunzip GeoIP.dat.gzgunzip GeoLiteCity.dat.gz

Configure nginx according to demand

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 include errorists 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 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 └── zh └── index.html Thank you for reading this article carefully. I hope the article "how to use geoip to restrict areas in nginx" shared by the editor will be helpful to everyone. At the same time, I also hope that you will support and follow the industry information channel. More related knowledge is waiting for you to learn!

Tags: Configuration articles data regions restrictions databases modules learning differences hosts values advantages locations information interests at the same time addresses geography geographic locations files Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno macOS OPPO Reno vpn Shulou Technology Xiaomi