Nginx connect req access 模块演练
- limig_conn模块:限制TCP连接数
- limit_req模块:限制请求频率
- access 模块(allow/deny):限制ip段访问
- auth_request: 基于HTTP响应状态码做权限控制
- 压测可以使用 postman的 run collection进行,设置iterations(并发数)和 delay(延迟)
#声明一个 limit_conn的条件 名称叫做 mylimitconn大小10m
limit_conn_zone $binary_remote_addr zone=mylimitconn:10m;
#声明一个 limit_req的条件 名称叫做 mylimit大小10m, 频率是每分钟 处理12个请求
limit_req_zone $binary_remote_addr zone=mylimit:10m rate=12r/m;
server {
listen 80;
server_name access.lvzb.com;
location /access/{
root html;
index index.html;
error_log logs/limit_req_error.log info;
# access module 开放2个ip段,拒绝其它ip
allow 192.168.125.100;
allow 192.168.125.1;
deny all;
# limit_conn module 限制tcp连接个数
limit_conn_status 503;
limit_conn_log_level warn;
# 限制TPC的连接数 2个,多余的就会拒绝
limit_conn mylimitconn 2;
# 限制响应速率 50字节
limit_rate 50;
# limit_req 请求限速
limit_req_status 504;
# 超过频率和 burst后的日志级别
limit_req_log_level notice;
# 延迟处理参数
limit_req zone=mylimit burst=7 nodelay;
}
}
理解burst=7:
- 加上前面mylimit 设置的 每分钟12个请求,也就是每5秒处理一个请求,那么就是开始最多可以有 1+7个请求。第一个不用入队,后面七个就会入队。第八个就会延迟执行或者拒绝。
理解nodelay:
- nodelay只是对放到burst队列中的请求立即处理,但处理完成后队列并不立即清空,队列清空的速度仍然按原来的速度每秒一个清空,所以当再有请求过来时,并不会马上又有两个burst请求被处理.
理解 auth_request: - 当访问 access.lvzb.com/OA/时,会转发到access.lvzb.com/auth/ 只有它返回 2XX状态码时,才会跳转到 login.html页面
server {
listen 80;
server_name access.lvzb.com;
location /OA/{
root html;
index login.html;
auth_request /auth;
}
location /auth/{
root html;
index index.html;
proxy_pass http:auth.lvzb.com/auth;
}
}