php - 重写后 NGINX 第二个 limit_req 不工作

标签 php nginx rate-limiting

如果我直接访问 php 页面 (index.php),此 limit_req 配置有效,但如果它访问/[pretty urls] 并将其重写为 index.php?$args,则无效。

limit_req_zone $binary_remote_addr zone=dynamic:10M rate=1r/s;
limit_req_zone $binary_remote_addr zone=static:10M rate=60r/s;

location / {
            limit_req zone=static burst=180;
            try_files $uri $uri/ /index.php?$args;
    }

location ~ \.php$ {
            limit_req zone=dynamic burst=5;
            fastcgi_split_path_info ^(.+\.php)(/.+)$;

            set $fsn /index.php;
            if (-f $document_root$fastcgi_script_name){
                    set $fsn $fastcgi_script_name;
            }

            fastcgi_pass unix:/var/run/php5-fpm.sock;
            include fastcgi_params;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fsn;
            fastcgi_param  PATH_INFO        $fastcgi_path_info;
            fastcgi_param  PATH_TRANSLATED  $document_root$fsn;
    }

它显然到达了正确的位置,因为它传递到后端,但似乎没有使用 limit_req。我在文档中找不到任何讨论此限制的内容。有人有什么想法吗?

编辑:用 zone=static 注释掉第一个允许 php 处理的工作。对我来说,这看起来像是 limit_req 中的错误。

最佳答案

看起来这就是 NGINX 和 limit_req 模块的工作方式......也许在未来它会变得更好。

经过大量挖掘,我发现了一个 nginx 论坛帖子,讨论了这个 http://forum.nginx.org/read.php?2,223426,223431并且似乎以下一项或两项为真(我没有兴趣进一步深入研究)。

  • 看起来如果使用 try_files 指令在“位置”使用 limit_req,则不会使用 future 的 limit_req 指令。
  • 可能一旦某个位置处理了 limit_req 指令,它们将不会在另一个“位置”中处理,但单个“位置”中的多个 limit_req 应该可以正常工作。

到目前为止,我的配置的解决方案是从/中删除 limit_req,并为静态文件设置一个新的位置正则表达式匹配,然后在其中放入静态数据的 limit_req。

关于php - 重写后 NGINX 第二个 limit_req 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27073091/

相关文章:

lua - 使用 lua 进行速率限制

php - 如何实现字符串值到日期格式

php - 变量引用范围

ruby-on-rails - Rails 应用程序在使用 Unicorn/nginx 组合的 DigitalOcean 上没有响应

nginx - 如何配置 nginx 反向代理以使用 SECURE websockets 上游?

twitter - Twitter 搜索 API 中的速率限制如何工作

grails - 如何在不访问数据库的情况下 Hook Grails Spring Security的登录速率限制器?

php - ffmpeg:如何使用 PHP 获取可用编解码器列表?

PHP Round() 无法正常工作

linux - Nginx - 从源代码构建 - 不工作