IP access rules
Start with an explicit allowlist
An IP access policy decides whether the address Nginx associates with a request is permitted to reach a protected handler. It is not proof of a person’s identity. A shared NAT gateway can represent many users, and an authorized user may move networks. Use address rules as a layer of defense alongside application authorization. In this example the content is a lab-owned static report, not a real administrative service.
location /reports/ { deny 192.0.2.19; allow 192.0.2.0/24; allow 2001:db8:20::/64; deny all; root /srv/demo;}Evaluate in order
The first matching rule decides the access-module result. A deny for one address must come before a broader allow that includes it. If no rule matches, the address module does not impose a denial; the final deny makes the policy explicitly closed. The file handler still has to find the requested resource after access is granted. These documentation addresses are fictional, not addresses to deploy.
192.0.2.19 deny192.0.2.20 allow198.51.100.10 deny2001:db8:20::7 allow2001:db8:21::7 denyKeep inherited policy intact
Rules defined in a child context replace the inherited list rather than append to it. A child containing only one allow is not a complete allowlist. Repeat the necessary full policy or use a reviewed include. The following example protects the private path while intentionally allowing public content at a separate location. No early return is used in the protected handler: rewrite-phase return can end a request before the access phase.
location / { root /srv/demo/public;}location /private/ { allow 192.0.2.20; deny all; root /srv/demo;}Require both controls when appropriate
With satisfy all (the default), each applicable access/authentication control must permit the request. With satisfy any, a successful applicable control can suffice; this can intentionally allow an office network without a password, but is inappropriate when both requirements are mandatory. The password file below is a placeholder for a locally provisioned hashed credential file. Never store plaintext credentials in it. A passing network check does not imply the backend grants the requested operation.
location /staff/ { satisfy all; allow 192.0.2.0/24; deny all; auth_basic "Staff reports"; auth_basic_user_file /srv/demo/staff.htpasswd; root /srv/demo;}Practice and next steps
Practice: decide which of five fictional clients in the worksheet are allowed, then explain why switching to satisfy any changes the requirements. Check IPv6 coverage and the final deny before deployment. See the chapter lab for direct peer tests.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
IP access rules
Predict address-based access decisions and combine network rules with authentication without accidentally weakening the policy.
Which rule wins when a client matches a deny before a broader allow?
Why place deny all after allowed ranges?
A child location defines a new allow rule. What happens to the parent rule list?
Which satisfy value requires both an active IP rule and authentication to permit access?
Why avoid an immediate return in an access-control test?