如何限制Nginx上每个虚拟主机的请求?

时间:2013-08-12 21:10:04

标签: nginx dns limit

我有一台服务于网络应用程序的NGINX服务器,客户拥有他们访问其网站的域名(CNAMES)。

NGINX是否有办法在一段时间内限制对其中一个域的访问次数?

示例:

我需要的限制:2000个请求/域/分钟

所以,在一个特定的时间段内......

www.websiteA.com.br --- 1456请求/分钟OK!

www.websiteB.com.br --- 1822请求/分钟OK!

www.websiteC.com.br --- 2001年请求/分钟暂时锁定

有谁知道如何制定这样的限制?

2 个答案:

答案 0 :(得分:5)

我正在使用nginx的速率限制功能来定义一些速率限制规则,然后将这些规则应用于特定主机。
这里有一些hacks(使用if语句以及通过error_page处理程序重定向) - 我还没有找到 这样做的更好方法。如果你有更好的选择,请评论。

nginx.conf

http {

    # ...

    # Define rate-limit key to use
    map $http_authorization $rl_zone_key{
        default $binary_remote_addr;
        '~*.*'  $http_authorization;
    }

    # Define Rate Limits
    limit_req_zone $rl_zone_key zone=rateLimit_Standard:10m rate=1r/s;
    limit_req_zone $rl_zone_key zone=rateLimit_Class_A:10m rate=5r/s;
    limit_req_zone $rl_zone_key zone=rateLimit_Class_B:10m rate=10r/s;
    limit_req_zone $rl_zone_key zone=rateLimit_Class_C:10m rate=100r/s;

    # Define the rate Limit Category applied to the particular host
    map $http_host $apply_rate_limit{
        hostnames;
        default             rateLimit_Standard;
        example.com         rateLimit_Class_B;
        *.example.com       rateLimit_Class_A;
    }

    #upstream server definition for php fast-cgi using port 9000
    upstream phpfcgi {
        server 127.0.0.1:9000;
    }

    # ...

}

map $ http_authorization $ rl_zone_key

用于比较两个请求是否来自同一位置的密钥,使用$binary_remote_addr nginx变量 除非存在$http_authorization标头,否则它将使用该标头。这意味着在用户通过身份验证之前(使用摘要身份验证), 速率限制由ip应用 - 之后由登录用户会话应用。

limit_req_zone

这里我设置了速率限制缓存。

  • 标准:10MB缓存,每秒允许1个请求
  • A类:10MB缓存,每秒允许5个请求
  • B类:10MB缓存,每秒允许10个请求
  • C类:10MB缓存,每秒允许100个请求

map $ http_host $ apply_rate_limit

使用map指令,检查$http_host中的主机名以确定目标域,然后使用我想要应用的速率限制类的名称定义$apply_rate_limit。 p>

example.com.conf

server {
    listen              80;
    server_name         example.com;
    root                /var/www/example.com;

    location / {
        index               index.php;
        try_files           $uri =404;

        location ~ ^/index\.php(/|$) {
            error_page               420 =200 @rateLimit_Standard;
            error_page               421 =200 @rateLimit_Class_A;
            error_page               422 =200 @rateLimit_Class_B;
            error_page               423 =200 @rateLimit_Class_C;

            if ( $apply_rate_limit = 'rateLimit_Standard' ) {return 420;}
            if ( $apply_rate_limit = 'rateLimit_Class_A' ) {return 421;}
            if ( $apply_rate_limit = 'rateLimit_Class_B' ) {return 422;}
            if ( $apply_rate_limit = 'rateLimit_Class_C' ) {return 423;}
        }

    }

    location @rateLimit_Standard {
        limit_req                   zone=rateLimit_Standard burst=5;
        add_header                  X-Rate-Limit-Class $apply_rate_limit;

        include                     fastcgi_params;
        fastcgi_index               index.php;
        fastcgi_split_path_info     ^(.+\.php)(/.*)$;
        fastcgi_param               SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param               HTTPS off;
        fastcgi_pass                phpfcgi;

    }

    location @rateLimit_Class_A {
        limit_req                   zone=rateLimit_Class_A burst=10 nodelay;
        add_header                  X-Rate-Limit-Class $apply_rate_limit;

        include                     fastcgi_params;
        fastcgi_index               index.php;
        fastcgi_split_path_info     ^(.+\.php)(/.*)$;
        fastcgi_param               SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param               HTTPS off;
        fastcgi_pass                phpfcgi;
    }

    location @rateLimit_Class_B {
        limit_req                   zone=rateLimit_Class_B burst=100 nodelay;
        add_header                  X-Rate-Limit-Class $apply_rate_limit;

        include                     fastcgi_params;
        fastcgi_index               index.php;
        fastcgi_split_path_info     ^(.+\.php)(/.*)$;
        fastcgi_param               SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param               HTTPS off;
        fastcgi_pass                phpfcgi;
    }

    location @rateLimit_Class_C {
        limit_req                   zone=rateLimit_Class_C burst=1000;
        add_header                  X-Rate-Limit-Class $apply_rate_limit;

        include                     external_definition_of_fastcgi_parameters.conf
    }

}

在我的示例vhost文件中,我使用先前定义的速率限制类。这可以以更直接的方式完成 但在我们的场景中,我们有一个服务于多个域的vhost文件,因此定义哪个域使用哪个速率限制类更好 在vhost文件之外完成。
我将在最后提供一个简化的示例vhost文件,删除此抽象并使vhost速率限制应用程序更加手动。

error_page 420 = 200 @rateLimit_Standard

在nginx中,没有方便的方法可以重定向到try_files指令之外的命名位置。完成这项任务的黑客就是 定义错误处理程序(对于不常用的错误代码),然后提供命名位置作为error_page处理程序 在这一特定行中,我们说当触发错误代码420时,错误处理程序应重定向到@rateLimit_Standard 位置并将HTTP代码重置为200。

if($ apply_rate_limit ='rateLimit_Standard'){return 420;}

在我们的nginx.conf文件中,$apply_rate_limit是基于$http_host标头定义的。如果它设置为rateLimit_Standard,则此if语句将完成 从http请求返回错误代码420.这将被我们之前定义的error_page处理程序捕获,并将被重定向到指定位置@rateLimit_Standard

location @rateLimit_Standard

将请求重新路由到@rateLimit_Standard后,它将应用速率限制并设置突发值:limit_req zone=rateLimit_Standard burst=5; 然后它会按照正常情况继续处理php请求 为了更好地衡量,我还添加了一个标题来跟踪应用的费率限制:add_headerX-Rate-Limit-Class $apply_rate_limit;

包括external_definition_of_fastcgi_parameters.conf

您会注意到每个指定位置,包含的fcgi标题是相同的。 nginx配置中没有正式的继承 块,所以这必须为每个位置重复,以便将其转发到php-fpm 但是,可以在外部文件中定义公共属性,并使用上面的include语句从中获取所有这些重复的配置选项 一个外部文件。我把它留在这里,因为这是你真正应该做的。

简化示例vhost

上面的 example.conf 文件完全展示了我如何抽象主机< - >我们在环境中使用它的配对等级配对。
如果您只为单个域使用单个vhost条目,则可以简单得多:

简单example.com.conf

server {
    listen              80;
    server_name         simple-example.com;
    root                /var/www/simple-example.com;

    location / {
        index               index.php;
        try_files           $uri =404;

        location ~ ^/index\.php(/|$) {
            limit_req                   zone=rateLimit_Class_A burst=10 nodelay;
            add_header                  X-Rate-Limit-Class rateLimit_Class_A;

            include                     fastcgi_params;
            fastcgi_index               index.php;
            fastcgi_split_path_info     ^(.+\.php)(/.*)$;
            fastcgi_param               SCRIPT_FILENAME $document_root$fastcgi_script_name;
            fastcgi_param               HTTPS off;
            fastcgi_pass                phpfcgi;
        }

    }

}

免责声明: 上面的所有代码都是从我自己的nginx配置构建的,目的是创建一个示例。这可能无法开箱即用 并且您需要将这些片段按到您的环境中才能使它们正常工作。

如果您认为这有用,请投票。

答案 1 :(得分:2)

您可以参考nginx中的HttpLimitReqModule。 limit_req和limit_req_zone可能会有所帮助。