nginx - Nginx auth_request 限制不受尊重
问题描述
我有一个如下的 nginx 配置(省略了一些细节):
http {
map $request_uri $guid {
default "unknown";
~^/out/v\d+/(?P<id>.+?)/.+$ $id;
}
map $http_x_forwarded_for $last_client_ip {
default $http_x_forwarded_for;
~,\s*(?P<last_ip>[\.\d]+?)\s*$ $last_ip;
}
limit_req_zone $guid zone=content:20m rate=500r/s;
limit_req_zone $guid zone=auth:20m rate=100r/s;
server {
location /out/ {
auth_request /auth;
set $request_type "unknown";
proxy_pass $upstream_server;
proxy_cache content_cache;
set $cache_key "${request_path}";
proxy_cache_key $cache_key;
proxy_cache_valid 200 301 302 10s;
#Throttling
limit_req zone=content burst=50 nodelay;
limit_req_status 429;
}
location /auth {
internal;
set $auth_type 1;
proxy_pass_request_body off;
proxy_pass $upstream_server/auth?id=$guid&requestor=$last_client_ip;
proxy_cache auth_cache;
set $auth_cache_key "${guid}|${last_client_ip}";
proxy_cache_key $auth_cache_key;
proxy_cache_valid 200 301 302 5m;
proxy_cache_valid 401 403 404 5m;
#Throttling
limit_req zone=auth burst=50 nodelay;
limit_req_status 429;
}
}
}
/out/
正如我预期的那样,请求限制了工作 - 超过 500r/s 的请求会受到限制。但是,我似乎无法启动/auth
节流。
我猜这可能是由于 and 之间的一些相互作用auth_request
,或者可能是由于请求是子请求limit_req
的事实。/auth
我将不胜感激任何帮助或解释!
解决方案
我怀疑这与您用作键的变量有关:$guid. 这不是 nginx 变量,遗憾的是您的代码段没有显示您如何定义它。但是你将它传递给你的代理的方式让我怀疑对 /auth 的每个请求都有不同的 $guid。然后您将永远不会遇到限制,因为 nginx 会计算每个键的请求数,请参见此处:https ://www.nginx.com/blog/rate-limiting-nginx/
因此,如果根本没有为 /out/ 设置 $guid,那么所有请求都将计入限制。没问题。但是,如果对 /auth 的每个请求确实不同,那么它将不起作用。
推荐阅读
- sql - 公开一个 DB2 Schema,以便所有表对 PUBLIC 都是可读的
- javascript - 将小数四舍五入到前两位有效数字
- tfs - TFS - 下载的安全文件的参考文件路径
- c# - 从不同模型加载数据
- javascript - 如何使用 React 和 Webpack 4 优化 Monaco Editor
- asp.net - 在用户空闲超过一定时间后处理 NullReferenceException
- php - 在同一页面上读取和显示数据时无法解决错误“找不到对象”
- reactjs - 在 React 中下载和压缩文件?
- javascript - Pixi JS 老虎机游戏计数特定精灵
- android - 当用户单击 Kotlin 中的后退按钮时如何显示自定义对话框