


ACL configuration based on keywords and blacklist in Nginx reverse proxy
With the rapid progress of network development, the number of applications and services deployed is increasing. In some scenarios, requests need to be routed to specific servers or applications. Nginx is a high-performance web server and a commonly used reverse proxy method that can solve these problems. Based on the ACL module provided by the Nginx reverse proxy, administrators can flexibly control and manage request routing, access control, and other network security issues.
In a reverse proxy, a request is sent from the client to the reverse proxy server, and the proxy server sends a request to the backend server on behalf of the client and provides the return result to the client. For example, in modern web applications with multiple languages and technology stacks, Nginx reverse proxy can be used to route different requests to different back-end services through the same domain name.
In this article, we will learn how to configure keyword- and blacklist-based ACLs to implement more granular routing policies and security controls for Nginx reverse proxy.
Keyword ACL
Keyword ACL is a way to implement request routing by matching keywords in the request URL. For example, in the current application, when the requested URL contains "/app1/", we want the Nginx reverse proxy to route the request to backend application 1. If the URL contains "/app2/", the request is routed to backend application 2.
To implement this function, you need to configure a keyword ACL in the Nginx configuration file. Here's how to configure it:
http { ... # 关键词acl map $request_uri $app_name { ~* "/app1/" app1; ~* "/app2/" app2; default ""; } }
In this configuration, $request_uri is an Nginx built-in variable that represents the requested URL. The value of this variable is passed to the map directive, which matches the predefined keyword regular expression. If the match is successful, the name of the application is stored in the $app_name variable, otherwise the default value is used.
Next, you can pass the $app_name variable defined above as the target of the proxy to the proxy URL option in the proxy directive:
server { ... location / { ... # 配置关键词代理 proxy_pass http://$app_name.backend.com; } }
In this configuration, the keyword ACL will be requested from The name of the requested application is matched in the URL, and the client's request is routed to the corresponding back-end application through proxy instructions.
Blacklist-based ACL
Blacklist ACL is a method used to block access to certain IPs or request URLs. This approach is very useful in some situations. For example, in the event of a malicious attack, the administrator can configure a blacklist ACL in the Nginx reverse proxy to deny access to a group of IP addresses. You can also use blacklist ACLs to deny certain common attack URLs.
Here's how to configure a blacklist-based ACL:
http { ... # 黑名单acl geo $blocked_ip { default 0; include /path/to/blacklists/ip.txt; } }
In this configuration, the geo directive defines a memory variable named $blocked_ip, which is used to store the list of blocked IP addresses. In this example, an external IP address blacklist file is used. The format of the file is as follows:
10.2.1.10 1; 192.168.0.0/24 1; 202.102.85.154 1;
Each line of the file contains an IP address or network segment in CIDR format, followed by the number 1 to indicate that this IP address or CIDR network segment is blocked.
Next you can use this blacklist ACL in the Nginx configuration:
server { ... location / { ... # 配置黑名单ACL if ($blocked_ip) { return 403; } proxy_pass http://backend.com; } }
In this configuration, the if directive is used to determine whether the requested IP address is in the blacklist. If so A 403 Forbidden response will be returned directly. Otherwise, the request will be routed to the backend proxy server.
To sum up, Nginx reverse proxy provides a good ACL module, which can be used together with other functional modules to achieve very flexible request routing and access control. In the use of reverse proxy, understanding and mastering these methods can allow us to better adapt to various situations and improve the quality and security of network services.
The above is the detailed content of ACL configuration based on keywords and blacklist in Nginx reverse proxy. For more information, please follow other related articles on the PHP Chinese website!

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

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











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.

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".

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 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.

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]

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 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.

When the Nginx server goes down, you can perform the following troubleshooting steps: Check that the nginx process is running. View the error log for error messages. Check the syntax of nginx configuration. Make sure nginx has the permissions you need to access the file. Check file descriptor to open limits. Confirm that nginx is listening on the correct port. Add firewall rules to allow nginx traffic. Check reverse proxy settings, including backend server availability. For further assistance, please contact technical support.
