Nginx 如何将位置列入白名单以进行速率限制

标签 nginx rate-limiting

我在我的 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 */
    }
}

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

最佳答案

@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;
    ...
    ...
    }
}

关于Nginx 如何将位置列入白名单以进行速率限制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48930056/

相关文章:

docker - nginx 通过 docker - 错误 :1408F10B:SSL routines:ssl3_get_record:wrong version number

docker - 如何对 docker 容器上的网络流量进行速率限制

c# - 如果 API 速率限制超过使用 WebApiThrottle - C# Web API,则阻止 API 请求 5 分钟

nginx - 扩展默认的 nginx mime.types 文件

linux - curl: (7) 无法连接到端口 80 和 443 - 在一个域上

ruby-on-rails - Carrierwave + 雾 + aws s3 和 Rails 正在生产中

multithreading - 当客户端是多线程时,客户端对 HTTP 429 的正确 react 是什么?

nginx - 如何根据请求中的 Origin header 正确将 nginx Access-Control-Allow-Origin 设置到响应 header 中?

node.js - 达到速率限制后的退避策略

javascript - 如何在 Node js 中使用 twitter 管理速率限制?