zoukankan      html  css  js  c++  java
  • 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 {
            }
    }
    ------------------------------------------------------------------------------------------------------------------------- ------------------------------------------------------- 博客均为个人笔记,无所追求,仅供参考~~~ QQ--2382990774
  • 相关阅读:
    【转载】Unity 合理安排增量更新(热更新)
    COCOS2D 释放资源的最佳时机
    【转载】利用Unity自带的合图切割功能将合图切割成子图
    用GL画出人物的移动路径
    使用行为树(Behavior Tree)实现游戏AI
    C#学习笔记
    题目:给定一数组 例如:a = [1,2,3,5,2,1] 现用户提供一个数字 请返回用户所提供的数字的所有下标
    算法: 归并排序
    题目:给定两个有序数组,对其进行合并
    数据结构 顺序表实现优先队列 回顾练习
  • 原文地址:https://www.cnblogs.com/alexlv/p/14832690.html
Copyright © 2011-2022 走看看