Time window-based access control in Nginx reverse proxy
With the development of the Internet, more and more applications are deployed in the cloud. How to ensure the security and stability of cloud services has become a key issue. Among them, Nginx, as a high-performance web server and reverse proxy, is widely used in the deployment and management of cloud services. In practical applications, access needs to be restricted in some scenarios, such as frequently accessed IPs, malicious access requests, large-traffic access, etc. This article will introduce an access control method based on time windows to ensure the security and stability of cloud services by limiting the number of accesses within a certain period of time.
1. What is a time window
The time window refers to a method of limiting events within a certain period of time. In access control, access can be restricted based on time windows, for example: a maximum of 10 visits in 1 minute, a maximum of 100 visits in 5 minutes, a maximum of 1,000 visits in an hour, etc. The time window can be adjusted according to actual conditions and is flexible and customizable.
2. Time window access control in Nginx reverse proxy
- Install the ngx_http_limit_req_module module
Before using nginx time window access control, you need to install it ngx_http_limit_req_module module. This module can control the frequency of arrival of client requests within the same time period. Usually when we install nginx, we will install this module at the same time. If it is not installed, you need to recompile and install nginx. The installation method is as follows:
$ wget http://nginx.org/download/nginx-1.14.0.tar.gz $ tar zxvf nginx-1.14.0.tar.gz $ cd nginx-1.14.0/ $ ./configure --prefix=/usr/local/nginx --add-module=../nginx-limit-req-module-master $ make $ sudo make install
- Configure Nginx reverse proxy
In Add the following content to the Nginx reverse proxy configuration file:
limit_req_zone $binary_remote_addr zone=one:10m rate=1r/s; server{ ... location /{ limit_req zone=one burst=5; proxy_pass http://backend; } }
In this configuration, limit_req_zone defines the limit area one, where 10m is the memory size, which can be adjusted according to actual needs. rate=1r/s defines one request per second. Limit_req zone=one burst=5 is added to the location, which means that when the number of requests exceeds 1 second, the excess requests will be processed with a peak value of 5 requests in the subsequent time.
- Verification time window access control
After the configuration is completed, you can use the ab tool to test, as follows:
$ ab -n 100 -c 10 http://localhost/ This is ApacheBench, Version 2.3 <$Revision: 1807734 $> Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/ Licensed to The Apache Software Foundation, http://www.apache.org/ Benchmarking localhost (be patient) Finished 100 requests
This command means: send 100 requests, the number of concurrent requests is 10. Test results: If 10 requests are sent within 1 second, the remaining requests will be restricted and a 429 error will be generated, as follows:
$ ab -n 100 -c 10 http://localhost/ This is ApacheBench, Version 2.3 <$Revision: 1807734 $> Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/ Licensed to The Apache Software Foundation, http://www.apache.org/ Benchmarking localhost (be patient) Finished 100 requests Server Software: nginx/1.14.0 Server Hostname: localhost Server Port: 80 Document Path: / Document Length: 0 bytes Concurrency Level: 10 Time taken for tests: 0.062 seconds Complete requests: 100 Failed requests: 9 (Connect: 0, Receive: 0, Length: 0, Exceptions: 9) Non-2xx responses: 9 Requests per second: 1617.28 [#/sec] (mean) Time per request: 6.173 [ms] (mean) Time per request: 0.617 [ms] (mean, across all concurrent requests) Transfer rate: 0.00 [Kbytes/sec] received Connection Times (ms) min mean[+/-sd] median max Connect: 0 1 0.2 1 1 Processing: 1 5 9.8 3 47 Waiting: 1 5 9.8 3 47 Total: 1 6 9.8 4 47 Percentage of the requests served within a certain time (ms) 50% 4 66% 5 75% 5 80% 6 90% 15 95% 47 98% 47 99% 47 100% 47 (longest request)
3. Summary
Deployment of cloud services and management, access control is crucial. Through the time window-based access control in Nginx reverse proxy, access frequency can be effectively controlled to ensure the security and stability of cloud services. This method can not only limit high-frequency requests, but also limit malicious access requests, effectively improving the reliability and security of cloud services.
The above is the detailed content of Time window-based access control 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 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 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.

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.

Steps to create a Docker image: Write a Dockerfile that contains the build instructions. Build the image in the terminal, using the docker build command. Tag the image and assign names and tags using the docker tag command.

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

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.

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.

To get Nginx to run Apache, you need to: 1. Install Nginx and Apache; 2. Configure the Nginx agent; 3. Start Nginx and Apache; 4. Test the configuration to ensure that you can see Apache content after accessing the domain name. In addition, you need to pay attention to other matters such as port number matching, virtual host configuration, and SSL/TLS settings.
