I'm looking for an approach to synchronize rate-limited IPs between nginx nodes. I want to log these IPs and after that pushing them into a database and developing an agent to update blocked IPs in nginx config files.

My challenge is to find a way to have IPs nginx limited with 429 status code.

So, Is it possible to log rate-limited IPs into an specific file in nginx or do you suggest any other approach to synchronize rate-limited IPs between nodes?


Yes, you can do that, and a similar example is even in the nginx documentation.

The access_log directive also takes an optional if= parameter which evaluates variables given to it, and logs only if the result is not 0 or an empty string. Combined with the fact that you can have more than one access_log in a level, you can log differently based on your needs.

First, though, you will need a map to map the HTTP response status you are interested in to a variable. Remember that map must be outside the server block.

map $status $rate_limited {
    default 0;
    429     1;

Then in the relevant server block you will declare your access_log.

access_log /var/log/nginx/rate_limited.log combined if=$rate_limited;

Remember that any appearance of access_log in one level overrides all others from higher levels, so you will want to copy (or better, include) the access_log directives from higher levels that you also want to use.

  • Thank you! For sure, I have to define different access_log formats. Thank you for your useful tips :) Sep 5 at 10:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.