I don’t think this is thinking outside the box, but I could not do a series of posts about useful commands without talking about map, arguably one of the most useful commands on NGINX, especially that I’m sure map will be used in most of our posts. You should think about map as a “switch” statement like many programming languages have, it’s how you will be able to take advantage of it.
Basically, the syntax for map is the following:
Where the variable_to_evaluate can be ANY variable accessible by NGINX, which includes HTTP headers, Cookies, etc.
Let’s review our rate limiting example that we used geo to create a whitelist, now we will use map to do the same thing, if the client sends a cookie called “whitelistrl”, he will bypass the rate limiting, but remember, this is not a good idea for rate limiting control as cookies can be easily injected to any HTTP request.
One thing to note thou, the order of the nesting matters, map accepts nesting other commands, but this is not true for all commands, for example, if we had tried to call map from geo, it was not going to work, so always test before deploying to production!
Also, you can see examples of map usage in posts right here on my blog!
Some examples are:
- Persistent backend selection using cookies with NGINX
- Using map for upstream configuration on NGINX
- Disable NGINX cache based on cookies
- Avoid caching 0-byte files on NGINX
See you on the next post!
If you have any question or suggestion, use the Ask-O-matic link on the top of the page.