Home Operation and Maintenance Nginx ACL configuration based on IP address and geographical location in Nginx reverse proxy

ACL configuration based on IP address and geographical location in Nginx reverse proxy

Jun 10, 2023 am 10:39 AM
nginx reverse proxy acl configuration

Nginx is an open source high-performance web server and reverse proxy server that is widely used in actual production environments. Its reverse proxy function can help us implement functions such as traffic control, load balancing, and security protection. However, other factors need to be considered when performing a reverse proxy, such as the client's IP address and geographical location information, so that we can better control access and ensure the security of the website.

Therefore, this article will introduce how to configure ACL based on IP address and geographical location in Nginx reverse proxy to achieve more refined access control.

1. What is ACL

Before introducing the specific configuration method, we first need to understand what ACL is. ACL (Access Control List) is an access control list, which is a policy used to control the flow of data on network devices. Through ACL, traffic can be classified and restricted based on different conditions to achieve network security and traffic control purposes.

In Nginx, we can configure ACL based on IP address, geographical location and other conditions to control access. Therefore, when performing a reverse proxy, we can configure the corresponding ACL based on the client's IP address and location information to better control the client's requests.

2. ACL configuration based on IP address

  1. What is an IP address

IP address is the abbreviation of Internet Protocol Address, that is, Internet protocol address. As an identifier that identifies a unique computer on the Internet, it consists of 32 binary bits, usually represented as 4 decimal numbers, each of which has a value between 0 and 255, separated by a period (for example, 127.0 .0.1).

  1. Classification of IP addresses

IP addresses can be classified according to factors such as their scope of use, allocation rules, and address format. Common classification methods are as follows:

(1) According to the scope of use, it is divided into public IP address and private IP address. Public IP address is used to connect to the Internet, and private IP address is used for intranet communication.

(2) According to the allocation rules, it is divided into static IP address and dynamic IP address. A static IP address is a fixed IP address that is manually set by the network administrator and is usually used for fixed-location devices such as servers. A dynamic IP address is an IP address dynamically assigned by a network service provider and changes over time.

(3) According to the address format, it is divided into IPv4 address and IPv6 address. IPv4 address is a 32-bit address format currently widely used. IPv6 address is a new generation of IP address that uses 128-bit address format and is used to replace IPv4 address.

  1. IP address-based ACL configuration in Nginx

In Nginx, we can perform reverse proxy ACL configuration based on the client's IP address. The specific configuration is as follows:

(1) Single IP address restriction

If you only need to restrict access to a single IP address, you can use the following configuration:

location / {
    #allow access from IP address 192.168.1.100
    allow 192.168.1.100;
    deny all;
}
Copy after login

In the above configuration, allow The directive is used to restrict access, and the deny directive is used to deny access. Only clients with IP address 192.168.1.100 are allowed to access, and other clients are denied.

(2) Multiple IP address restrictions

If you need to restrict access to multiple IP addresses, you can use the following configuration:

location / {
    #allow access from IP address 192.168.1.100 and 192.168.1.101
    allow 192.168.1.100;
    allow 192.168.1.101;
    deny all;
}
Copy after login

In the above configuration, the allowed IP addresses are Clients at 192.168.1.100 and 192.168.1.101 are allowed to access, and other clients are denied access.

(3) Restriction based on IP address segment

If you need to restrict access to a certain IP address segment, you can use the following configuration:

location / {
    # allow access from IP address segments 192.168.1.0/24
    allow 192.168.1.0/24;
    deny all;
}
Copy after login

In the above configuration, IP addresses are allowed Clients with segment 192.168.1.0/24 are allowed to access, and other clients are denied access. Among them, "/24" means mask, which means that the first 24 bits are the network address and the last 8 bits are the host address.

3. ACL configuration based on geographical location

  1. MaxMind GeoIP2

Implementing ACL configuration based on geographical location in Nginx requires MaxMind GeoIP2. MaxMind GeoIP2 is an IP geographical location database that provides rich geographical location information. Through GeoIP2, we can map the client's IP address to information such as city, region, country and its ISO code.

  1. Installation of GeoIP2

Installing GeoIP2 is mainly divided into four steps:

(1) Install dependency packages

yum -y install automake autoconf libtool gcc make pcre-devel zlib-devel
Copy after login

( 2) Download GeoIP2

wget https://github.com/maxmind/geoip-api-c/releases/download/v1.9.2/GeoIP-1.9.2.tar.gz
Copy after login

(3) Unzip and install GeoIP2

tar xzf GeoIP-1.9.2.tar.gz
cd GeoIP-1.9.2
./configure
make && make check && make install
Copy after login

(4) Download GeoIP2-Library and GeoIP2-City

mkdir /usr/share/GeoIP
cd /usr/share/GeoIP/
wget http://geolite.maxmind.com/download/geoip/database/GeoLite2-City.tar.gz
wget http://geolite.maxmind.com/download/geoip/database/GeoLite2-Country.tar.gz
tar -zxvf GeoLite2-City.tar.gz
tar -zxvf GeoLite2-Country.tar.gz
Copy after login
  1. Nginx based on geography Location ACL configuration

After installing GeoIP2, we need to configure it accordingly in Nginx. The specific steps are as follows:

(1) Add GeoIP2 related configuration in the Nginx configuration file

# set geoip database path
geoip_country /usr/share/GeoIP/GeoLite2-Country.mmdb;
geoip_city /usr/share/GeoIP/GeoLite2-City.mmdb;

# enable nginx api
http {
    geoip2 /usr/share/GeoIP/GeoLite2-City.mmdb {
        $geoip2_data_city_name city names en;
        $geoip2_data_country_iso_code country iso_code;
        $geoip2_data_latitude latitude;
        $geoip2_data_longitude longitude;
    }
}
Copy after login

In the above configuration, we specified the database path of GeoIP2 and configured the city, country, Longitude, latitude and other related information for subsequent use.

(2) Using GeoIP2 in Nginx location

location / {
    # allow access from China
    if ($geoip2_data_country_iso_code != CN) {
        return 403;
    }
}
Copy after login

In the above configuration, we judge the client’s geographical location information and only allow clients from China (country code is CN) to access .

4. Summary

Through the introduction of this article, we have learned about the ACL configuration based on IP address and geographical location in the Nginx reverse proxy, and how to use MaxMind GeoIP2 to query and access geographical location information. control. These functions can help us better control client access and provide more secure and efficient services. Hope this article is helpful to readers.

The above is the detailed content of ACL configuration based on IP address and geographical location in Nginx reverse proxy. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to check the name of the docker container How to check the name of the docker container Apr 15, 2025 pm 12:21 PM

You can query the Docker container name by following the steps: List all containers (docker ps). Filter the container list (using the grep command). Gets the container name (located in the "NAMES" column).

How to configure nginx in Windows How to configure nginx in Windows Apr 14, 2025 pm 12:57 PM

How to configure Nginx in Windows? Install Nginx and create a virtual host configuration. Modify the main configuration file and include the virtual host configuration. Start or reload Nginx. Test the configuration and view the website. Selectively enable SSL and configure SSL certificates. Selectively set the firewall to allow port 80 and 443 traffic.

How to configure cloud server domain name in nginx How to configure cloud server domain name in nginx Apr 14, 2025 pm 12:18 PM

How to configure an Nginx domain name on a cloud server: Create an A record pointing to the public IP address of the cloud server. Add virtual host blocks in the Nginx configuration file, specifying the listening port, domain name, and website root directory. Restart Nginx to apply the changes. Access the domain name test configuration. Other notes: Install the SSL certificate to enable HTTPS, ensure that the firewall allows port 80 traffic, and wait for DNS resolution to take effect.

How to check whether nginx is started How to check whether nginx is started Apr 14, 2025 pm 01:03 PM

How to confirm whether Nginx is started: 1. Use the command line: systemctl status nginx (Linux/Unix), netstat -ano | findstr 80 (Windows); 2. Check whether port 80 is open; 3. Check the Nginx startup message in the system log; 4. Use third-party tools, such as Nagios, Zabbix, and Icinga.

How to check nginx version How to check nginx version Apr 14, 2025 am 11:57 AM

The methods that can query the Nginx version are: use the nginx -v command; view the version directive in the nginx.conf file; open the Nginx error page and view the page title.

How to create containers for docker How to create containers for docker Apr 15, 2025 pm 12:18 PM

Create a container in Docker: 1. Pull the image: docker pull [mirror name] 2. Create a container: docker run [Options] [mirror name] [Command] 3. Start the container: docker start [Container name]

How to start containers by docker How to start containers by docker Apr 15, 2025 pm 12:27 PM

Docker container startup steps: Pull the container image: Run "docker pull [mirror name]". Create a container: Use "docker create [options] [mirror name] [commands and parameters]". Start the container: Execute "docker start [Container name or ID]". Check container status: Verify that the container is running with "docker ps".

How to start nginx server How to start nginx server Apr 14, 2025 pm 12:27 PM

Starting an Nginx server requires different steps according to different operating systems: Linux/Unix system: Install the Nginx package (for example, using apt-get or yum). Use systemctl to start an Nginx service (for example, sudo systemctl start nginx). Windows system: Download and install Windows binary files. Start Nginx using the nginx.exe executable (for example, nginx.exe -c conf\nginx.conf). No matter which operating system you use, you can access the server IP

See all articles