AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / server / 问题 / 554682
Accepted
Tom
Tom
Asked: 2013-11-14 04:06:14 +0800 CST2013-11-14 04:06:14 +0800 CST 2013-11-14 04:06:14 +0800 CST

如何防止 nginx 节流与维护模式冲突?

  • 772

我们使用HttpLimitReqModule nginx mondule 进行速率限制,发现它与我们的“维护模式”冲突,因为两个组件都使用http 状态码 503。

当限制被激活时(通过limit_req指令),nginx 通常会提供 503,但不幸的是使用了我们的维护模式位置,这导致我们的 Amazon S3 托管维护页面出现 302。限制请求的 302 不是一个好的结果。

我想知道其他人如何处理这个问题?例如,我是否应该为我们的维护页面使用不同的状态代码,但如果是这样,怎么办?

理想情况下,对于受限制的请求,我不希望提供任何页面,只需要 503 响应标头 - 它需要尽可能轻量级,因为关键是要阻止服务器不堪重负。


作为参考,这是我们用于“维护模式”的 nginx 配置:

server {
    ...

    # Redirect processing of 503 error pages into a named location:
    error_page 503 @maintenance;

    # "Maintenance Mode" is off by default - Use a nginx variable to track state.
    set $maintenance off;

    # Switch on "Maintenance Mode" if a certain file exists.
    if (-f /var/www/mysite/shared/maintenanceON) {
        set $maintenance on;
    }

    if ($maintenance = on) {
        return 503; # 503 - Service unavailable
    }

    location @maintenance {
        # Redirect the request to our maintenance page in Amazon S3.
        rewrite ^(.*)$ http://mysite.s3-website-us-east-1.amazonaws.com/ break;
    }
    ...
    # Process the php files - pass to php-fpm.
    location ~ \.php {
        # Limit aggressive bots and crawlers to 30 requests per minute.
        limit_req zone=bots;

        fastcgi_pass 127.0.0.1:$fastcgi_port;
    }
    ...
nginx
  • 2 2 个回答
  • 1775 Views

2 个回答

  • Voted
  1. Michael Hampton
    2013-11-14T08:08:04+08:002013-11-14T08:08:04+08:00

    使用 503 以外的状态代码作为“维护模式”。

    正如我们可以清楚地看到的那样,无论如何,当您使用“维护模式”时,用户实际上并没有得到 503,因此在您的配置内部使用该状态代码没有任何好处。制作另一个代码(593?)并使用它。


    或者更好的是,跳过额外的,当维护文件存在时直接location发送。rewrite

        if (-f /var/www/mysite/shared/maintenanceON) {
            rewrite ^(.*)$ http://mysite.s3-website-us-east-1.amazonaws.com/ redirect;
        }
    
    • 4
  2. Best Answer
    Tom
    2014-06-13T03:11:15+08:002014-06-13T03:11:15+08:00

    从 nginx 1.3.15 开始,有一个“ limit_req_status ”指令允许您指定限制器将返回的 http 响应代码。

    # Define a limit request zone called "bots" that will track requests by IP.
    limit_req_zone $binary_remote_addr zone=bots:20m rate=15r/s;
    
    # 429 = Too Many Requests
    limit_req_status 429;
    

    http 状态 429 表示“ Too Many Requests”——此代码已在RFC 6585 附加 HTTP 状态代码中被接受。例如,它用于Twitter REST API Rate Limiter。

    (迈克尔的回答也有效,因为在我的配置中 503 仅由 nginx 内部使用)。

    • 2

相关问题

  • Gzip 与反向代理缓存

  • nginx 作为代理的行为

  • Nginx 学习资源 [关闭]

  • 提供 70,000 个静态文件 (jpg) 的最佳方式?

  • 在 Apache、LightTPD 和 Nginx Web 服务器上提供 PHP 5.x 应用程序的现状?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    新安装后 postgres 的默认超级用户用户名/密码是什么?

    • 5 个回答
  • Marko Smith

    SFTP 使用什么端口?

    • 6 个回答
  • Marko Smith

    命令行列出 Windows Active Directory 组中的用户?

    • 9 个回答
  • Marko Smith

    什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同?

    • 3 个回答
  • Marko Smith

    如何确定bash变量是否为空?

    • 15 个回答
  • Martin Hope
    Tom Feiner 如何按大小对 du -h 输出进行排序 2009-02-26 05:42:42 +0800 CST
  • Martin Hope
    Noah Goodrich 什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同? 2009-05-19 18:24:42 +0800 CST
  • Martin Hope
    Brent 如何确定bash变量是否为空? 2009-05-13 09:54:48 +0800 CST
  • Martin Hope
    cletus 您如何找到在 Windows 中打开文件的进程? 2009-05-01 16:47:16 +0800 CST

热门标签

linux nginx windows networking ubuntu domain-name-system amazon-web-services active-directory apache-2.4 ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve