Mahdi.Kh
September 21, 2026
We learn how to block a specific IP, or block or whitelist the IPs we want, so that the web service can only be accessed from certain IPs.
Sometimes we need to restrict access to a specific path based on IP. To do this, we can use a combination of the allow and deny directives.
With the allow directive, we can specify who's allowed access, and with deny, we can restrict access for everyone else. Along with the allow and deny directives, we can use a single IP, a CIDR range, or the value all. (documentation)
In the example above, we first restricted all access with deny all, and then, with allow directives, granted access only to the IPs we wanted.
Sometimes we only need to restrict access for a specific HTTP method; in that case, we can use the limit_except directive. This directive specifies which methods should NOT be restricted!
Note: not restricting GET also means the HEAD method won't be restricted.
According to the documentation, the following methods can be used with the limit_except directive:
Using the satisfy directive, we can create several restrictions and specify whether the user is granted access if just one of them passes or if all of them need to pass (documentation). This directive is used together with any or all.
In the example above, we used any. This means that if the user's IP falls within the range we specified, or if they provide a correct username and password, they can access the content.
The source code for all the examples we covered above is available on GitHub.
Previous part: Setting a Password on a Path | Nginx from Scratch
Next part: Compressing Responses | Nginx from Scratch