共计 4400 个字符,预计需要花费 11 分钟才能阅读完成。
最近想整个资源站点,用来转储一些墙外的资源和一些收藏的资源包,因为博主用的免费梯子,时常不稳,每次做实验总得重新下载,麻烦
一个简单的静态站点
[root@nginx-cluster conf.d]# cat www.xadocker.cn.conf
server {
listen 80;
server_name www.xadocker.cn;
location / {
root /home/wwwroot/static/;
autoindex_format html;
autoindex on;
autoindex_exact_size off;
autoindex_localtime on;
charset utf-8,gbk;
}
}
nginx限制传输速度
时间久了,发现有其他人也在下,与其说是下载,这TM是在刷。。。。所以得限制下客户下载网速。查了下nginx官方文档,发现其中的ngx_http_core_module
模块中有两个指令正好适用:limit_rate,limit_rate_after
limit_rate指令
指令使用方式
Syntax: limit_rate rate;
Default:
limit_rate 0;
Context: http, server, location, if in location
Limits the rate of response transmission to a client. The rate
is specified in bytes per second. The zero value disables rate limiting. The limit is set per a request, and so if a client simultaneously opens two connections, the overall rate will be twice as much as the specified limit.
大致意思是该指令可以限制每个请求的传输速度,注意点是:如果一个客户端同时发起n个请求时,限制也是对每个请求生效的,最终客户端的总请求带宽速率消耗n*rate
给静态站点添加上该指令
[root@nginx-cluster conf.d]# cat www.xadocker.cn.conf
server {
listen 80;
server_name www.xadocker.cn;
location / {
set $limit_rate 128k;
root /home/wwwroot/static/;
autoindex_format html;
autoindex on;
autoindex_exact_size off;
autoindex_localtime on;
charset utf-8,gbk;
}
}
测试看看
[root@nginx-cluster ~]# wget www.xadocker.cn/jdk-8u261-linux-x64.rpm
--2020-03-07 16:18:24-- http://www.xadocker.cn/jdk-8u261-linux-x64.rpm
Resolving www.xadocker.cn (www.xadocker.cn)... 192.168.44.145
Connecting to www.xadocker.cn (www.xadocker.cn)|192.168.44.145|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 117557932 (112M) [application/x-redhat-package-manager]
Saving to: ‘jdk-8u261-linux-x64.rpm’
0% [ ] 1,048,576 129KB/s eta 14m 45s
limit_rate_after指令
Syntax: limit_rate_after size;
Default:
limit_rate_after 0;
Context: http, server, location, if in location
This directive appeared in version 0.8.0.
Sets the initial amount after which the further transmission of a response to a client will be rate limited.
大致意思时当一个链接传输字节数到指定阈值后进行限速,也是对单个链接的限制
调整下我们的静态站点配置
[root@nginx-cluster conf.d]# cat www.xadocker.cn.conf
server {
listen 80;
server_name www.xadocker.cn;
location / {
limit_rate_after 10m;
limit_rate 128k;
root /home/wwwroot/static/;
autoindex_format html;
autoindex on;
autoindex_exact_size off;
autoindex_localtime on;
charset utf-8,gbk;
}
}
测试下效果
[root@nginx-cluster ~]# wget www.xadocker.cn/jdk-8u261-linux-x64.rpm
--2020-03-07 16:29:06-- http://www.xadocker.cn/jdk-8u261-linux-x64.rpm
Resolving www.xadocker.cn (www.xadocker.cn)... 192.168.44.145
Connecting to www.xadocker.cn (www.xadocker.cn)|192.168.44.145|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 117557932 (112M) [application/x-redhat-package-manager]
Saving to: ‘jdk-8u261-linux-x64.rpm.1’
9% [======> ] 10,878,976 3.50MB/s
9% [======> ] 11,403,264 1.56MB/s eta 65s
11% [========> ] 13,762,560 128KB/s eta 3m 9s
12% [=========> ] 14,548,992 128KB/s eta 3m 40s
12% [=========> ] 14,680,064 128KB/s eta 3m 44s
ngx_http_limit_req_module
到这里我们已经实现了对单个链接进行传输速度限制,但是若出现并发请求,依然还是会爆,所以任然需要限制下请求速率,此时就要用到ngx_http_limit_req_module来控制,其中有两个关键指令:
limit_req_zone
Syntax: limit_req_zone key zone=name:size rate=rate;
Default: —
Context: http
用于定义一个会话状态存储区域,用一块共享内存空间。示例:
limit_req_zone $binary_remote_addr zone=one:10m rate=1r/s;
- key:使用nginx内置变量作为key
- zone:定义一个共享区域内存,
- name为名称
- size指定大小
- rate:设置速率,每秒一次:1r/s,只能配置整数。1r/m,1r/h
limit_req
设置使用哪个共享内存限制域和允许被处理的最大请求数阀值。
Syntax: limit_req zone=name [burst=number] [nodelay];
Default: —
Context: http, server, location
- zone:指定用哪块共享内存空间,用limit_req_zone来创建一块内存共享空间
- brust:整数值。一个请求缓冲区(队列),当超过速率时,超过限制的请求数放在此处延迟处理,若依然超过brust数值,则超出的请求直接返回503
- nodelay:如果不希望超过的请求被延迟,可以使用 nodelay 参数
示例:
1.限制这个静态站点服务请求速率:5r/m,3个缓冲
[root@nginx-cluster conf.d]# cat www.xadocker.cn.conf
limit_req_zone $server_name zone=perserver:10m rate=5r/m;
server {
listen 80;
server_name www.xadocker.cn;
location / {
limit_req zone=perserver burst=3;
root /home/wwwroot/static/;
autoindex_format html;
autoindex on;
autoindex_exact_size off;
autoindex_localtime on;
charset utf-8,gbk;
}
}
2.限制每个客户端ip请求速率:5r/m,3个缓冲,nodelay不延迟处理
[root@nginx-cluster conf.d]# cat www.xadocker.cn.conf
limit_req_zone $binary_remote_addr zone=perip:10m rate=5r/m;
server {
listen 80;
server_name www.xadocker.cn;
location / {
limit_req zone=perip burst=3 nodelay;
root /home/wwwroot/static/;
autoindex_format html;
autoindex on;
autoindex_exact_size off;
autoindex_localtime on;
charset utf-8,gbk;
}
}
最后加上我们的请求限速,最终配置文件为:
[root@nginx-cluster conf.d]# cat www.xadocker.cn.conf
limit_req_zone $binary_remote_addr zone=perip:10m rate=5r/m;
server {
listen 80;
server_name www.xadocker.cn;
location / {
limit_req zone=perip burst=3 nodelay;
# rate_limit_after 10m;
rate_limit 64k;
root /home/wwwroot/static/;
autoindex_format html;
autoindex on;
autoindex_exact_size off;
autoindex_localtime on;
charset utf-8,gbk;
}
}