我之所以要这样做,是因为用户使用 JavaScript 针对我们的 API 进行开发,并且一些开发人员搞砸了并导致访问者使用 AJAX 请求猛烈抨击服务器。发生这种情况时,我希望能够将 API 请求限制为每分钟 50 个请求,或者类似的效果。
注意:(特别是数据库密集型资源,所以可能在路径级别,而不是服务器范围内(例如,节流“/json_api/”,但不是“/static/”)。
如果我需要使用秘密标头绕过速率限制,我该如何实现?
参考:
http {
geo $whitelist {
default 0;
# CIDR in the list below are not limited
1.2.3.0/24 1;
9.10.11.12/32 1;
127.0.0.1/32 1;
}
map $whitelist $limit {
0 $binary_remote_addr;
1 "";
}
limit_conn_zone $limit zone=connlimit:10m;
limit_conn connlimit 5;
limit_conn_log_level warn; # logging level when threshold exceeded
limit_conn_status 503; # the error code to return
Run Code Online (Sandbox Code Playgroud)