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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

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 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 whether nginx is started? How to check whether nginx is started? Apr 14, 2025 pm 12:48 PM

In Linux, use the following command to check whether Nginx is started: systemctl status nginx judges based on the command output: If "Active: active (running)" is displayed, Nginx is started. If "Active: inactive (dead)" is displayed, Nginx is stopped.

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 start nginx in Linux How to start nginx in Linux Apr 14, 2025 pm 12:51 PM

Steps to start Nginx in Linux: Check whether Nginx is installed. Use systemctl start nginx to start the Nginx service. Use systemctl enable nginx to enable automatic startup of Nginx at system startup. Use systemctl status nginx to verify that the startup is successful. Visit http://localhost in a web browser to view the default welcome page.

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

How to solve nginx403 error How to solve nginx403 error Apr 14, 2025 pm 12:54 PM

The server does not have permission to access the requested resource, resulting in a nginx 403 error. Solutions include: Check file permissions. Check the .htaccess configuration. Check nginx configuration. Configure SELinux permissions. Check the firewall rules. Troubleshoot other causes such as browser problems, server failures, or other possible errors.

How to solve the problem of nginx cross-domain How to solve the problem of nginx cross-domain Apr 14, 2025 am 10:15 AM

There are two ways to solve the Nginx cross-domain problem: modify the cross-domain response header: add directives to allow cross-domain requests, specify allowed methods and headers, and set cache time. Use CORS modules: Enable modules and configure CORS rules that allow cross-domain requests, methods, headers, and cache times.

How to solve nginx304 error How to solve nginx304 error Apr 14, 2025 pm 12:45 PM

Answer to the question: 304 Not Modified error indicates that the browser has cached the latest resource version of the client request. Solution: 1. Clear the browser cache; 2. Disable the browser cache; 3. Configure Nginx to allow client cache; 4. Check file permissions; 5. Check file hash; 6. Disable CDN or reverse proxy cache; 7. Restart Nginx.

See all articles