Nginx的限流
nginx还可以用来限流。
limit_req_zone 用来限制单位时间内的请求数目,以及速度限制。
limit_req_conn 用来限制同一时间连接数,即并发限制。
配置
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log notice;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
limit_req_zone $binary_remote_addr zone=mylimit:10m rate=1r/s;
server {
listen 80;
server_name localhost;
limit_req zone=mylimit burst=1 nodelay;
limit_req_status 598;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
[20:03:43] xiaoyu:nginx $ ab -n 4 -c 2 http://localhost/
This is ApacheBench, Version 2.3 <$Revision: 1879490 $>
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).....done
Server Software: nginx/1.23.1
Server Hostname: localhost
Server Port: 80
Document Path: /
Document Length: 615 bytes
Concurrency Level: 2
Time taken for tests: 0.043 seconds
Complete requests: 4
Failed requests: 2
(Connect: 0, Receive: 0, Length: 2, Exceptions: 0)
Non-2xx responses: 2
Total transferred: 1924 bytes
HTML transferred: 1230 bytes
Requests per second: 93.28 [#/sec] (mean)
Time per request: 21.441 [ms] (mean)
Time per request: 10.720 [ms] (mean, across all concurrent requests)
Transfer rate: 43.82 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 0 0.0 0 0
Processing: 4 19 16.1 33 33
Waiting: 4 19 16.2 33 33
Total: 4 19 16.1 33 33
Percentage of the requests served within a certain time (ms)
50% 33
66% 33
75% 33
80% 33
90% 33
95% 33
98% 33
99% 33
100% 33 (longest request)
版权声明:本文为xiaolixi199311原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。