社区微信群开通啦,扫一扫抢先加入社区官方微信群
社区微信群
HTTP协议版本
|
连接关系
|
HTTP1.0
|
TCP不能复用
|
HTTP1.1
|
顺序性TCP复用
|
HTTP2.0
|
多路复用TCP复用
|
语法
|
范围
|
说明
|
limit_conn_zone 标识 zone=空间名:空间大小;
|
http
|
用于声明一个存储空间
|
limit_conn 空间名 并发限制数;
|
http、server、location
|
用于限制某个存储空间的并发数量
|
limit_conn_log_level 日志等级;
|
http、server、location
|
当达到最大限制连接数后, 记录日志的等级
|
limit_conn_status 状态码;
|
http、server、location
|
当超过限制后,返回的响应状态码,默认是503
|
limit_req_zone key zone=空间名:空间大小 rate=每秒请求数;
|
http
|
用于声明一个存储空间
|
limit_req zone=空间名 [burst=队列数] [nodelay];
|
http、server、location
|
用于限制某个存储空间的并发数量
|
1 http { 2 limit_req_zone $binary_remote_addr zone=req_zone:1m rate=10r/s; 3 }
1 [root@nginx01 ~]# mkdir /usr/share/nginx/limit/ 2 [root@nginx01 ~]# echo '<h1>Limit</h1>' > /usr/share/nginx/limit/index.html 3 4 [root@nginx01 ~]# vi /etc/nginx/nginx.conf 5 …… 6 http { 7 limit_conn_zone $binary_remote_addr zone=conn_zone:10m; 8 limit_req_zone $binary_remote_addr zone=req_zone:10m rate=1r/s; 9 …… 10 }
1 [root@nginx01 ~]# vi /etc/nginx/conf.d/limit.conf 2 server { 3 server_name limit.linuxds.com; 4 location / { 5 root /usr/share/nginx/limit; 6 index index.html; 7 limit_conn conn_zone 1; #引用全局中的连接限制zone,并设置同一时刻只允许一个客户端连接 8 limit_req zone=req_zone; #引用全局中的请求限制zone 9 #limit_req zone=req_zone burst=3 nodelay; #参考场景三,此处注释 10 } 11 }
1 [root@nginx01 ~]# nginx -t -c /etc/nginx/nginx.conf #检查配置文件 2 [root@nginx01 ~]# nginx -s reload #重载配置文件
1 [root@client ~]# yum -y install httpd-tools 2 [root@client ~]# ab -n 50 -c 10 http://limit.linuxds.com/index.html
语法
|
范围
|
说明
|
allow IP地址 | CIDR网段 | unix: | all;
|
http、server、location、limit_except
|
允许IP地址、CIDR格式的网段、unix套接字或所有来源访问
|
deny IP地址 | CIDR网段 | unix: | all;
|
http、server、location、limit_except
|
禁止IP地址、CIDR格式的网段、unix套接字或所有来源访问
|
1 [root@nginx01 ~]# mkdir /usr/share/nginx/ipaccess/ 2 [root@nginx01 ~]# echo '<h1>Ipaccess</h1>' > /usr/share/nginx/ipaccess/index.html 3 4 [root@nginx01 ~]# vi /etc/nginx/conf.d/ipaccess.conf 5 server { 6 server_name ipaccess.linuxds.com; 7 location / { 8 root /usr/share/nginx/ipaccess; 9 index index.html; 10 deny 115.205.87.7; #拒绝特定IP 11 allow all; #允许其他所有 12 } 13 }
1 [root@nginx01 ~]# nginx -t -c /etc/nginx/nginx.conf #检查配置文件 2 [root@nginx01 ~]# nginx -s reload #重载配置文件
语法
|
范围
|
说明
|
auth_basic 请输入你的帐号密码 | off;
(默认关闭)
|
http、server、location、limit_except
|
显示用户登录提示 (有些浏览器不显示提示)
|
auth_basic_user_file 存储帐号密码的文件路径;
|
http、server、location、limit_except
|
从文件中匹配帐号密码
|
1 [root@nginx01 ~]# mkdir /usr/share/nginx/account/ 2 [root@nginx01 ~]# mkdir /etc/nginx/passwd/ 3 [root@nginx01 ~]# echo '<h1>Account</h1>' > /usr/share/nginx/account/index.html
1 [root@nginx01 ~]# vi /etc/nginx/conf.d/account.conf 2 server { 3 server_name account.linuxds.com; 4 location / { 5 root /usr/share/nginx/account; 6 index index.html; 7 auth_basic "Auth access test! input your password!"; 8 auth_basic_user_file /etc/nginx/passwd/auth_conf; 9 } 10 }
1 [root@nginx01 ~]# yum -y install httpd-tools 2 [root@nginx01 ~]# htpasswd -c /etc/nginx/passwd/auth_conf xhy 3 [root@nginx01 ~]# nginx -t -c /etc/nginx/nginx.conf #检查配置文件 4 [root@nginx01 ~]# nginx -s reload #重载配置文件
语法
|
范围
|
说明
|
limit_rate rate;
默认值为0,即关闭限速。
|
http、server、location
|
限制向客户端传送响应的速率限制。
|
limit_rate_after size;
默认值为0,即关闭限速。
|
http、server、location
|
设置不限速传输的响应大小。
|
1 server { 2 …… 3 limit_rate_after 3m; 4 limit_rate 20k; 5 } 6 }
1 [root@nginx01 ~]# nginx -t -c /etc/nginx/nginx.conf #检查配置文件 2 [root@nginx01 ~]# nginx -s reload #重载配置文件
如果觉得我的文章对您有用,请随意打赏。你的支持将鼓励我继续创作!