环境描述:
操作系统:windows 10 64 位家庭版
nginx: 1.14.2
ab: This is ApacheBench, Version 2.3 <$Revision: 655654 $>
nginx 安装路径:E:\soft3\HTTP\nginx-1.14.2
location /limit {
limit_req zone=perip burst=3;
root E:/soft3/HTTP/nginx-1.14.2/html/;
index index.html index.htm;
}
页面路径:
一、niginx 配置限流
#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;
#gzip on;
#limit_conn_zone $binary_remote_addr zone=perip:10m;
#limit_conn_zone $server_name zone=perip:10m;
#limit_conn_log_level error;
#limit_conn_status 503;
limit_req_zone $binary_remote_addr zone=perip:10m rate=2r/s;
limit_conn_log_level error;
limit_conn_status 503;
upstream user {
server 127.0.0.1:8080;
}
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location /limit {
limit_req zone=perip burst=3;
root E:/soft3/HTTP/nginx-1.14.2/html/;
index index.html index.htm;
}
location / {
root html;
index index.html index.htm;
}
location /user/getToken {
proxy_pass http://user;
}
location /user {
proxy_pass http://user;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}
二、ab 测试
E:\soft3\HTTP>ab -n 5 -c 5 http://127.0.0.1/limit/1.html
由于限流区域没有配置为延迟模式(没有配置nodelay ),ab 模拟的并发数超过限制的并发数 ab 会被阻塞,等待获得令牌。
location /limit {
limit_req zone=perip burst=3;
root E:/soft3/HTTP/nginx-1.14.2/html/;
index index.html index.htm;
}
查看 NGINX 访问日志:
三、配置为无订单模式
location /limit {
limit_req zone=perip burst=3 nodelay;
root E:/soft3/HTTP/nginx-1.14.2/html/;
index index.html index.htm;
}
其他地方的配置和前面一样,只在标红部分加了 nodelay 。
ab 测试 ab -n 5 -c 5 http://127.0.0.1/limit/1.html
这次的测试结果与前面不同,ab 不会出现阻塞。nginx 允许突发流量(突发流量等于桶的容量,桶容量为3)。前面4个访问正常(ngx_http_limit_req_module不够精确所以多了一个访问成功),后面6个比限流返回503。
结论:
配置每秒处理2个请求,令牌桶缓存10
1. 使用无等待方式限流,令牌桶中必须缓存有令牌,如果令牌桶中一个令牌也没有,虽然
配置每秒处理2个请求,但实际会所有的请求都被限流(拒绝)。如果令牌桶中已经缓存
10个令牌,这个时候突发10个请求,10个请求都会成功。
2. 使用无等待方式限流,如果令牌桶中只有一个令牌,虽然 配置每秒处理2个请求,但实际
只有一个请求会成功,其他请求回被拒绝。
3. 使用等待方式限流,如果令牌桶中只有一个令牌,虽然 配置每秒处理2个请求,但实际
只有一个请求会成功,2到10个请求会等待获得令牌,获得令牌后会请求会处理成功。
超过10个的其他请求都会拒绝。