Score:1

Logging nginx rate-limited IPs into a specific file

in flag

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?

Score:0
cz flag

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.

Little Apocrypha avatar
in flag
Thank you! For sure, I have to define different access_log formats. Thank you for your useful tips :)
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.