【问题标题】:Nginx how to whitelist location for rate limitingNginx如何将位置列入白名单以进行速率限制
【发布时间】:2018-02-22 14:35:58
【问题描述】:

我的 nginx.conf 中针对主机和 IP 设置了全局速率限制。但是,有一个特定位置我想忽略这些限制,就像这样。

limit_req_zone $binary_remote_addr zone=limitip:10m rate=10r/s;
limit_req_zone $host zone=limithost:10m rate=10r/s;

server {
    limit_req zone=limitip burst=5 nodelay;
    limit_req zone=limithost burst=5 nodelay;

    location /whitelisted_location {
        /* ignore the server limits */
    }
}

创建两个具有非常高价值的新区域并在该位置内使用它们的最佳方法是什么?

【问题讨论】:

    标签: nginx rate-limiting


    【解决方案1】:

    @agentshowers,您可能会在this topic 中找到如何做到这一点的想法。

    实际上,我们这样做是这样的:

    # Defining which limit zone to use (per URL)
    map $request_uri $pb_limit_req_zone {
        "~^/files(/.*)?"    "files";
        "~^/secret/health-check$"    "healthchecks";
    
         default                    "common";
    }
    
    # Define the key per zone name
    map $pb_limit_req_zone $limit_req_key_files {
        default             "";
        "files"    $binary_remote_addr;
    }
    map $pb_limit_req_zone $limit_req_key_common {
        default             "";
        "common"    $binary_remote_addr;
    }
    map $pb_limit_req_zone $limit_req_key_healthchecks {
        default             "";
        "healthchecks"    $binary_remote_addr;
    }
    
    # Defining the zones
    limit_req_zone $limit_req_key_files zone=pb-frontend-files:20m rate=10r/s;
    limit_req_zone $limit_req_key_common zone=pb-frontend-common:20m rate=25r/s;
    limit_req_zone $limit_req_key_healthchecks zone=pb-frontend-healthchecks:20m rate=100r/s;
    
    ...
    ...
    server {
    ...
        location / {
            # Rate Limit settings
            limit_req_dry_run off;
            limit_req zone=pb-frontend-files burst=1 delay=10;
            limit_req zone=pb-frontend-common  delay=10;
            limit_req zone=pb-frontend-healthchecks burst=50 delay=10;
            limit_req_status 429;
    
            try_files $uri $uri/ /index.php?$query_string;
        }
    ...
    ...
        # Pass all .php files to a php-fpm/php-fcgi server.
        location ~ [^/]\.php(/|$) {
            # Rate Limit settings
            limit_req_dry_run off;
            limit_req zone=pb-frontend-files burst=1 delay=10;
            limit_req zone=pb-frontend-common  delay=10;
            limit_req zone=pb-frontend-healthchecks burst=50 delay=10;
            limit_req_status 429;
        ...
        ...
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-29
      • 1970-01-01
      • 2021-10-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多