nginx对客户端进行限制的相关配置
1、limit_rate rate;
限制响应给客户端的传输速率,单位是bytes/second
默认值0表示无限制
Default: limit_rate 0;
Context: http, server, location, if in location
[root@centos7.6 ~]# wget www.magedu.tech/test.log --2021-03-03 13:32:29-- http://www.magedu.tech/test.log Resolving www.magedu.tech (www.magedu.tech)... 10.0.0.125 Connecting to www.magedu.tech (www.magedu.tech)|10.0.0.125|:80... connected. HTTP request sent, awaiting response... 200 OK Length: 573623 (560K) [application/octet-stream] Saving to: ‘test.log.1’ 100%[=================================================>] 573,623 --.-K/s in 0s 2021-03-03 13:32:29 (1.37 GB/s) - ‘test.log.1’ saved [573623/573623] 修改配置:
[root@centos7.6 ~]# vi /etc/nginx/conf.d/test.conf server { listen 80; server_name www.magedu.tech; root /opt/testdir/; limit_rate 2k; } 验证: [root@centos7.6 ~]# wget www.magedu.tech/test.log --2021-03-03 13:34:19-- http://www.magedu.tech/test.log Resolving www.magedu.tech (www.magedu.tech)... 10.0.0.125 Connecting to www.magedu.tech (www.magedu.tech)|10.0.0.125|:80... connected. HTTP request sent, awaiting response... 200 OK Length: 573623 (560K) [application/octet-stream] Saving to: ‘test.log.2’ 6% [=========> ] 36,864 1.99KB/s eta 4m 9s
2、limit_except method ... { ... },仅用于location
限制客户端使用除了指定的请求方法之外的其它方法
Default: —
Context: location
method:GET, HEAD, POST, PUT, DELETE,MKCOL, COPY, MOVE, OPTIONS, PROPFIND, PROPPATCH, LOCK, UNLOCK, PATCH
示例:允许所有客户端使用GET方法都访问,其他方法只有10.0.0.126客户端允许可以允许
[root@centos7.6 ~]# vi /etc/nginx/conf.d/test.conf server { listen 80; server_name www.magedu.tech; root /opt/testdir/; limit_rate 2k; location / { limit_except GET { allow 10.0.0.126; deny all; } } } [root@10.0.0.126 ~]# wget www.magedu.tech/test.log --2021-03-03 13:43:39-- http://www.magedu.tech/test.log Resolving www.magedu.tech (www.magedu.tech)... 10.0.0.125 Connecting to www.magedu.tech (www.magedu.tech)|10.0.0.125|:80... connected. HTTP request sent, awaiting response... 200 OK Length: 573623 (560K) [application/octet-stream] Saving to: ‘test.log’ 1% [==> ] 11,088 2.62KB/s eta 3m 46s [root@10.0.0.126 ~]# curl -XPUT www.magedu.tech/ <html> <head><title>405 Not Allowed</title></head> #表示不支持PUT方法,不是拒绝PUT方法 <body> <center><h1>405 Not Allowed</h1></center> <hr><center>nginx/1.16.1</center> </body> </html> [root@10.0.0.125 ~]# curl -XPUT www.magedu.tech/ <html> <head><title>403 Forbidden</title></head> #拒绝PUT方法 <body> <center><h1>403 Forbidden</h1></center> <hr><center>nginx/1.16.1</center> </body> </html>
· 35+程序员的转型之路:经济寒冬中的希望与策略
· JavaScript中如何遍历对象?
· 领域模型应用
· 记一次 ADL 导致的 C++ 代码编译错误
· MySQL查询执行顺序:一张图看懂SQL是如何工作的
· 35+程序员的转型之路:经济寒冬中的希望与策略
· 全球首位 AI 程序员 Devin 诞生了,对于程序员的影响到底多大?
· 使用 OpenAuth.Net 快速搭建 .NET 企业级权限工作流系统
· 做这么个免费在线拼图工具,如何赚钱呢
· .NET 9 + React 开发的企业级后台权限管理系统,文档齐全,轻松上手