


How do websites set black/whitelist IP restrictions and country and city IP access restrictions through nginx?
1. Black/white list IP restricted access configuration
There are several ways to configure black and white lists in nginx. Here are only two commonly used methods.
1. The first method: allow, deny
The deny and allow instructions belong to ngx_http_access_module. nginx loads this module by default, so it can be used directly.
This method is the simplest and most direct. Set up similar to firewall iptable, usage method:
Add directly to the configuration file:
#白名单设置,allow后面为可访问IP location / { allow 123.13.123.12; allow 23.53.32.1/100; deny all; } #黑名单设置,deny后面接限制的IP,为什么不加allow all? 因为这个默认是开启的 location / { deny 123.13.123.12; } #白名单,特定目录访问限制 location /tree/list { allow 123.13.123.12; deny all; }
or configure the whitelist by reading the file IP
location /{ include /home/whitelist.conf; #默认位置路径为/etc/nginx/ 下, #如直接写include whitelist.conf,则只需要在/etc/nginx目录下创建whitelist.conf deny all; }
Create in the /home/ directory whitelist.conf, and write the IP that needs to be added to the whitelist. After the addition is completed, view the following:
cat /home/whitelist.conf #白名单IP allow 10.1.1.10; allow 10.1.1.11;
The whitelist setting is completed, and the blacklist setting method is the same.
2: The second method, ngx_http_geo_module
By default, this module is usually added to nginx. ngx_http_geo_module: Official document, the parameters need to be set in the http module.
This module can set IP restrictions and country and region restrictions. The location can be outside the server module.
Syntax example:
Add the configuration file directly
geo $ip_list { default 0; #设置默认值为0 192.168.1.0/24 1; 10.1.0.0/16 1; } server { listen 8081; server_name 192.168.152.100; location / { root /var/www/test; index index.html index.htm index.php; if ( $ip_list = 0 ) { #判断默认值,如果值为0,可访问,这时上面添加的IP为黑名单。 #白名单,将设置$ip_list = 1,这时上面添加的IP为白名单。 proxy_pass http://192.168.152.100:8081; }
You can also read the file IP configuration
geo $ip_list { default 0; #设置默认值为0 include ip_white.conf; } server { listen 8081; server_name 192.168.152.100; location / { root /var/www/test; index index.html index.htm index.php; if ( $ip_list = 0 ) { return 403; #限制的IP返回值为403,也可以设置为503,504其他值。 #建议设置503,504这样返回的页面不会暴露nginx相关信息,限制的IP看到的信息只显示服务器错误,无法判断真正原因。 }
Create ip_list in the /etc/nginx directory .conf, after adding the IP, view the following:
cat /etc/nginx/ip_list.conf 192.168.152.1 1; 192.168.150.0/24 1;
When the setting is completed, the IP list file ip_list.conf will be used as a whitelist. If the requested IP is not in the list, the 403 page will be returned directly. The blacklist setting method is the same.
3. ngx_http_geo_module load balancing (extension)
ngx_http_geo_module, the module can also be used for load balancing, such as web clusters with servers in different regions, IP segments in a certain region, load balancing to access Servers in a certain region. A similar way is to add custom values behind the IP. These values are not limited to numbers, but letters can also be used, such as US, CN, etc.
Example:
If there are three servers: 122.11.11.11, 133.11.12.22, 144.11.11.33
geo $country { default default; 111.11.11.0/24 uk; #IP段定义值uk 111.11.12.0/24 us; #IP段定义值us } upstream uk.server { erver 122.11.11.11:9090; #定义值uk的IP直接访问此服务器 } upstream us.server { server 133.11.12.22:9090; #定义值us的IP直接访问此服务器 } upstream default.server { server 144.11.11.33:9090; #默认的定义值default的IP直接访问此服务器 } server { listen 9090; server_name 144.11.11.33; location / { root /var/www/html/; index index.html index.htm; } }
Then
2. Country and region IP Restricting access
Some third-party services such as cloudflare also provide setting options to make the setting of firewall rules more convenient. Here we talk about how to set up nginx.
1: Install the ngx_http_geoip_module module
ngx_http_geoip_module: Official document, the parameters need to be set in the http module.
nginx does not build this module by default, it should be enabled using the --with-http_geoip_module configuration parameter.
For Ubuntu systems, install nginx-extras components directly, including almost all modules.
sudo apt install nginx-extras
For centos system, install the module.
yum install nginx-module-geoip
2. Download the IP database
This module depends on the IP database. All data is read in this database, and the ip library (dat format) needs to be downloaded.
MaxMind provides a free IP geographical database. The bad news is that MaxMind has officially stopped supporting the dat format IP database.
You can find dat format files in other places, or old versions. Of course, the data cannot be the latest, and there are some errors.
Download includes country and city versions of both Ipv4 and Ipv6.
#下载国家IP库,解压并移动到nginx配置文件目录, sudo wget https://dl.miyuru.lk/geoip/maxmind/country/maxmind.dat.gz gunzip maxmind.dat.gz sudo mv maxmind.dat /etc/nginx/GeoCountry.dat sudo wget https://dl.miyuru.lk/geoip/maxmind/city/maxmind.dat.gz gunzip maxmind.dat.gz sudo mv maxmind.dat /etc/nginx/GeoCity.dat
3. Configure nginx
Example:
geoip_country /etc/nginx/GeoCountry.dat; geoip_city /etc/nginx/GeoCity.dat; server { listen 80; server_name 144.11.11.33; location / { root /var/www/html/; index index.html index.htm; if ($geoip_country_code = CN) { return 403; #中国地区,拒绝访问。返回403页面 } } }
Here, the regional and country basic settings are completed.
Geoip other parameters:
Country-related parameters:
$geoip_country_code #Two-character English country code. For example: CN, US
$geoip_country_code3 #A three-character English country code. For example: CHN, USA
$geoip_country_name #The full English name of the country. For example: China, United States
City related parameters:
$geoip_city_country_code # is also a two-character English country code.
$geoip_city_country_code3 #Same as above
$geoip_city_country_name #Same as above.
$geoip_region #This has been tested to be a two-digit number, such as 02 for Hangzhou and 23 for Shanghai. However, no relevant information was found. I hope friends who know more can leave a message.
$geoip_city #The English name of the city. For example: Hangzhou
$geoip_postal_code #The postal code of the city. After testing, this field is empty in China
$geoip_city_continent_code #I don’t know what it is used for, but it seems to be AS
$geoip_latitude #Latitude
$geoip_longitude #Longitude
The above is the detailed content of How do websites set black/whitelist IP restrictions and country and city IP access restrictions through nginx?. For more information, please follow other related articles on the PHP Chinese website!

NGINX can improve website performance and reliability by: 1. Process static content as a web server; 2. forward requests as a reverse proxy server; 3. allocate requests as a load balancer; 4. Reduce backend pressure as a cache server. NGINX can significantly improve website performance through configuration optimizations such as enabling Gzip compression and adjusting connection pooling.

NGINXserveswebcontentandactsasareverseproxy,loadbalancer,andmore.1)ItefficientlyservesstaticcontentlikeHTMLandimages.2)Itfunctionsasareverseproxyandloadbalancer,distributingtrafficacrossservers.3)NGINXenhancesperformancethroughcaching.4)Itofferssecur

NGINXUnit simplifies application deployment with dynamic configuration and multilingual support. 1) Dynamic configuration can be modified without restarting the server. 2) Supports multiple programming languages, such as Python, PHP, and Java. 3) Adopt asynchronous non-blocking I/O model to improve high concurrency processing performance.

NGINX initially solved the C10K problem and has now developed into an all-rounder who handles load balancing, reverse proxying and API gateways. 1) It is well-known for event-driven and non-blocking architectures and is suitable for high concurrency. 2) NGINX can be used as an HTTP and reverse proxy server, supporting IMAP/POP3. 3) Its working principle is based on event-driven and asynchronous I/O models, improving performance. 4) Basic usage includes configuring virtual hosts and load balancing, and advanced usage involves complex load balancing and caching strategies. 5) Common errors include configuration syntax errors and permission issues, and debugging skills include using nginx-t command and stub_status module. 6) Performance optimization suggestions include adjusting worker parameters, using gzip compression and

Diagnosis and solutions for common errors of Nginx include: 1. View log files, 2. Adjust configuration files, 3. Optimize performance. By analyzing logs, adjusting timeout settings and optimizing cache and load balancing, errors such as 404, 502, 504 can be effectively resolved to improve website stability and performance.

NGINXUnitischosenfordeployingapplicationsduetoitsflexibility,easeofuse,andabilitytohandledynamicapplications.1)ItsupportsmultipleprogramminglanguageslikePython,PHP,Node.js,andJava.2)Itallowsdynamicreconfigurationwithoutdowntime.3)ItusesJSONforconfigu

NGINX can be used to serve files and manage traffic. 1) Configure NGINX service static files: define the listening port and file directory. 2) Implement load balancing and traffic management: Use upstream module and cache policies to optimize performance.

NGINX is suitable for handling high concurrency and static content, while Apache is suitable for dynamic content and complex URL rewrites. 1.NGINX adopts an event-driven model, suitable for high concurrency. 2. Apache uses process or thread model, which is suitable for dynamic content. 3. NGINX configuration is simple, Apache configuration is complex but more flexible.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Zend Studio 13.0.1
Powerful PHP integrated development environment

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software
