Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Sample code that Nginx restricts the number of visits and requests of an IP in the same time period

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

Nginx can be configured with ngx_http_limit_conn_module and ngx_http_limit_req_module to limit the number of visits to ip at the same time.

Ngx_http_limit_conn_module: this module is used to limit the number of connections per defined key, especially the number of connections to a single IP ​​ address. Use limit_conn_zone and limit_conn instructions.

Ngx_http_limit_req_module: used to limit the processing rate of requests for each defined key, especially from a single IP address. Use the "leak bucket" method to limit. Instructions: limit_req_zone and limit_req.

Ngx_http_limit_conn_module: example of limiting the number of connections to a single IP:

Http {limit_conn_zone $binary_remote_addr zone=addr:10m; # defines a limit_req_zone named addr to store session with 10m memory, # uses $binary_remote_addr as key, # nginx replaces limit_conn with limit_conn_zone later, # and can only be placed in http {} code snippet. Server {... Location / download/ {limit_conn addr 1; # connection limit # sets the shared memory area and the maximum number of connections allowed for a given key. When this limit is exceeded, the server will return a 503 (service temporarily unavailable) error. # if the zone storage space is insufficient, the server will return a 503 (service temporarily unavailable) error}

There may be several limit_conn directives, and the following configuration limits the number of connections per client IP to the server and the total number of connections to the virtual server:

Http {limit_conn_zone $binary_remote_addr zone=perip:10m; limit_conn_zone $server_name zone=perserver:10m... Server {... Limit_conn perip 10; # connections from a single client ip to the server. Limit_conn perserver 100; # limit the total number of connections to the server}

Reference document: http://nginx.org/en/docs/http/ngx_http_limit_conn_module.html

Ngx_http_limit_req_module: limit the number of requests for a single IP at a certain time.

Example:

Http {limit_req_zone $binary_remote_addr zone=one:10m rate=1r/s;. # defines a limit_req_zone named one to store session with a size of 10 megabytes of memory, # uses $binary_remote_addr as key, limits the average number of requests per second to 1, # 1m can store 16000 states, and the value of rete must be an integer, server {. Location / search/ {limit_req zone=one burst=5 # limit the number of requests per ip to no more than 1 request per second, and the number of leaky barrels burst is 5, that is, the queue. # nodelay, if this option is not set, the average rate limit is strictly used to limit the number of requests, and requests exceeding the limit are delayed. # take a chestnut: # set the number of rate=20r/s requests per second to 20 The number of leaky buckets burst is 5, which means that if there are 19 requests in 4 seconds in the first second and 25 requests in the fifth second, it can be understood that if you have 25 requests in the first second and the request exceeds 20 in the second second, a 503 error will be returned. # if the area storage space is insufficient The server will return a 503 (service temporarily unavailable) error # rate specified in the request per second (rhand s). If the rate of less than one request per second is required, it is specified as the request per minute. }

You can also limit the rate at which requests from a single IP address can be processed, while limiting the request processing rate for virtual servers:

Http {limit_req_zone $binary_remote_addr zone=perip:10m rate=1r/s; limit_req_zone $server_name zone=perserver:10m rate=10r/s;... Server {... The number of limit_req zone=perip burst=5 nodelay; # leaky buckets is 5. That is, the number of queues. Nodelay: delay is not enabled. Limit_req zone=perserver burst=10; # limits the processing rate of nginx to 10 per second}

Summary

The above is the sample code that the editor introduces to you that Nginx restricts the number of visits and requests of an IP at the same time. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply you in time. Thank you very much for your support to the website!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report