Fork me on GitHub

nginx ---对客户端进行限制的相关配置

  一、限制响应给客户端的传输速率,单位是bytes/second默认值0表示无限制

limit_rate rate;   语境:httpserverlocation,if in location

  1、生成一百兆的文件

dd if=/dev/zero of=test.img bs=1M count=100

2、直接下载

[18:47:21 root@localhost ~]#wget www.a.net/test.img
--2021-05-31 18:47:37--  http://www.a.net/test.img
Resolving www.a.net (www.a.net)... 192.168.1.5
Connecting to www.a.net (www.a.net)|192.168.1.5|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 104857600 (100M) [application/octet-stream]
Saving to: ‘test.img.1100%[=========================================================================>] 104,857,600  124MB/s   in 0.8s   

2021-05-31 18:47:38 (124 MB/s) - ‘test.img.1’ saved [104857600/104857600]

3、限定10k 默认是字节为单位

server {
        server_name www.a.net;
        root /data/site1;
        limit_rate 10k ;
        location /about {
                root /opt/testdir/;
                index test.html;
        }
        location /test1 {
        root /opt/;
        try_files $uri $uri.jpg =404;
        }

        error_page 404 =200  /404.html;
        location = /404.html {
        }
}           

4、测试下载

[18:50:39 root@localhost ~]#wget www.a.net/test.img
--2021-05-31 18:50:39--  http://www.a.net/test.img
Resolving www.a.net (www.a.net)... 192.168.1.5
Connecting to www.a.net (www.a.net)|192.168.1.5|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 104857600 (100M) [application/octet-stream]
Saving to: ‘test.img.20% [                                                                          ] 491,520     9.97KB/s  eta 2h 47m

  二、限制客户端使用除了指定的请求方法之外的其它方法

limit_except method ... { ... },仅用于location

1、测试访问,默认是不支持

curl -X OPTIONS  192.168.1.5
<html>
<head><title>405 Not Allowed</title></head>
<body>
<center><h1>405 Not Allowed</h1></center>
<hr><center>nginx/1.20.1</center>
</body>
</html>

2、允许192.168.1.4这个网段可以GET,其他都拒绝

server {
        server_name www.a.net;
        root /data/site1;
        limit_rate 10k ;
        location / {
            limit_except GET {
                allow 192.168.1.4;
                deny all;
                }
        }

        location /about {
                root /opt/testdir/;
                index test.html;
        }
        location /test1 {
        root /opt/;
        try_files $uri $uri.jpg =404;
        }

        error_page 404 =200  /404.html;
        location = /404.html {
        }
}

 

posted @ 2021-05-31 19:15  Alex-Lzy  阅读(192)  评论(0编辑  收藏  举报