【发布时间】:2019-05-31 05:34:37
【问题描述】:
我正在尝试为 Kubernetes ingress-nginx 中的特定路径设置速率限制选项limit_req,以防止暴力验证。
我已经使用 ConfigMap 定义了limit_req_zone:
http-snippet: |
limit_req_zone $the_real_ip zone=authentication_ratelimit:10m rate=1r/s;
接下来,我使用注解来添加自定义位置块:
nginx.ingress.kubernetes.io/configuration-snippet: |
location ~* "^/authenticate$" {
limit_req zone=authentication_ratelimit nodelay;
more_set_headers "x-test: matched";
}
这会产生 nginx.conf:
server {
# - - 8< - -
location / {
# - - 8< - -
location ~* "^/authenticate$" {
limit_req zone=authentication_ratelimit nodelay;
more_set_headers "x-test: matched";
}
proxy_pass http://upstream_balancer;
proxy_redirect off;
}
结果是/authenticate 总是返回 HTTP 503(带有 x-test 标头)。来自入口访问日志的消息:
<ip> - [<ip>] - - [04/Jan/2019:15:22:07 +0000] "POST /authenticate HTTP/2.0" 503 197 "-" "curl/7.54.0" 172 0.000 [-] - - - - 1a63c9825c9795be1378b2547e29992d
我怀疑这可能是因为嵌套位置块和proxy_pass 之间的冲突(但这只是一个疯狂的猜测)。
我还尝试了哪些其他选项?
- 使用
server-snippet注释而不是configuration-snippet-/authenticate返回 404,因为proxy_pass未配置 - 使用
nginx.ingress.kubernetes.io/limit-rpm注释 - 强制对整个应用程序进行速率限制,这不是我想要的。
问题是为什么自定义位置块会以 503 响应?我该如何调试呢?增加 nginx 日志记录级别会提供有关 503 的更多详细信息吗? 或更一般的问题:我可以在 ingress-nginx 中注入自定义位置块吗?
【问题讨论】:
标签: nginx kubernetes kubernetes-ingress nginx-ingress