Nginx采用的是漏桶算法
官方文档
http://nginx.org/en/docs/http/ngx_http_limit_req_module.html
http://hopestar.github.io/2013/06/08/nginx-limit-moule-note/
配置文件如下:
#user nobody;
worker_processes 1;
error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include 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 logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#rate limit
limit_conn_zone $binary_remote_addr zone=one:10m; #容器共使用10M的内存来对于IP传输开销
limit_req_zone $binary_remote_addr zone=req_one:20m rate=5r/s;
limit_conn one 1; #限制每个IP只能发起一个并发连接
limit_req zone=req_one burst=120;#请求排队
upstream hctp {
server 127.0.0.1:9090;
}
#gzip on;
server {
listen 8080;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
#location / {
# root html;
# index index.html index.htm;
#}
location / {
proxy_pass http://hctp/;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
include servers/*;
}
使用jmeter 并发12请求,有拒绝请求