I'm trying to setup rate limiting option limit_req
for specific path in Kubernetes ingress-nginx to prevent brute-forcing authentication.
I've defined limit_req_zone
using ConfigMap:
http-snippet: |
limit_req_zone $the_real_ip zone=authentication_ratelimit:10m rate=1r/s;
Next, I'm using annotation to add a custom location block:
nginx.ingress.kubernetes.io/configuration-snippet: |
location ~* "^/authenticate$" {
limit_req zone=authentication_ratelimit nodelay;
more_set_headers "x-test: matched";
}
This produces 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;
}
The result is that /authenticate
always returns HTTP 503 (with x-test header). Message from ingress access logs:
<ip> - [<ip>] - - [04/Jan/2019:15:22:07 +0000] "POST /authenticate HTTP/2.0" 503 197 "-" "curl/7.54.0" 172 0.000 [-] - - - - 1a63c9825c9795be1378b2547e29992d
I suspect this might be because of conflict between nested location block and proxy_pass
(but this is just a wild guess).
What other options have I tried?
- use
server-snippet
annotation instead ofconfiguration-snippet
-/authenticate
returns 404 becauseproxy_pass
is not configured - use
nginx.ingress.kubernetes.io/limit-rpm
annotation - forces ratelimit on whole application which is not what I want.
Question is why custom location block responds with 503? How can I debug this? Will increasing nginx logging level give more details about 503? Or more general question: can I inject custom location blocks in ingress-nginx?