Home Operation and Maintenance Nginx URI-based ACL configuration in Nginx reverse proxy

URI-based ACL configuration in Nginx reverse proxy

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

Nginx is an open source high-performance web server and reverse proxy server that can easily handle a large number of concurrent requests. Nginx integrates a variety of functional modules, among which ACL (Access Control List) is an important configuration method. ACL allows administrators to control which users or IP addresses can access the Nginx server, thereby improving the security and reliability of the web server. This article will introduce how to use ACL configuration in Nginx reverse proxy to control access permissions based on URI.

  1. What is ACL?

ACL, full name Access Control List, Chinese name Access Control List, is a technical means to control access rights. It allows or denies access to specific users or IP addresses, thereby protecting your web server from malicious attacks.

There are two ways to implement ACL in Nginx, one is ACL based on IP address, and the other is ACL based on URI. This article will focus on URI-based ACLs.

  1. URI-based ACL configuration

URI-based ACL is a relatively common and flexible means of controlling access permissions. By filtering the requested URI, user access rights can be controlled by category.

Let’s look at a specific example. Suppose our web application has two modules, one is the background management module and the other is the front-end user module. The access URIs for these two modules are different.

We can use the ACL function of Nginx to cooperate with the reverse proxy module to achieve access control to these two modules. First, we need to add the following ACL rules to the Nginx configuration file:

location / {

proxy_pass http://backend;
allow 192.168.1.0/24; # 允许192.168.1.0/24网段的访问
deny all; # 拒绝其他所有IP地址的访问
Copy after login
Copy after login

}

location /admin {

proxy_pass http://backend;
allow 192.168.1.10; # 允许指定IP地址的访问
deny all; # 拒绝其他所有IP地址的访问
Copy after login

}

In the above configuration, we set different ACL rules for all URIs and /admin URIs. For all URIs, we only allow IP addresses in the 192.168.1.0/24 network segment to be accessible. For /admin URI, we only allow the specified IP address to be accessed, and other IP addresses are rejected.

It should be noted that the order of ACL rules is very important. Nginx first matches the longest URI. If the match is successful, the ACL rule corresponding to the URI is used. If the URI matches multiple ACL rules, the first matching ACL rule will be used.

  1. Regular expression-based ACL configuration

In addition to URI-based ACL configuration, Nginx also supports regular expression-based ACL configuration. This method is more flexible and can be customized according to different needs.

Let’s take a look at an example. Suppose we need to control access to all URIs starting with /api, we can use the following ACL configuration:

location ~ ^/api/(.*)$ {

proxy_pass http://backend;
allow 192.168.1.0/24; # 允许192.168.1.0/24网段的访问
deny all; # 拒绝其他所有IP地址的访问
Copy after login
Copy after login

}

In the above configuration, we use regular expressions to match all URIs starting with /api, and set the corresponding ACL rules.

It should be noted that using regular expressions for ACL configuration may cause some performance overhead. Therefore, we recommend using URI-based ACL configuration whenever possible.

  1. Summary

ACL is a very important means of controlling access permissions in Nginx reverse proxy. URI-based ACL configuration can set different access permissions for different URIs. ACL configuration based on regular expressions is more flexible and can be customized according to different needs. For different application scenarios, we can choose different ACL configuration methods. At the same time, in order to improve performance, we should try to avoid using complex regular expressions for ACL configuration.

The above is the detailed content of URI-based ACL configuration 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 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 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 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 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 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 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 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 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