allow and deny rules
- What the nginx docs say
- Rules are checked in sequence until the first match
- What to check
- The location block and its parents; the order of allow and deny; a final deny all
Trace an nginx 403 to its cause: allow/deny rules, auth_request, referer checks, missing index files or worker permissions. Includes config examples.
An nginx 403 means nginx, or an app behind it, refused the request. In nginx itself the usual causes are allow and deny rules, an auth_request subrequest answering 403, a referer check that returns 403, a directory with no index file and autoindex off, or files the worker user can't read. Read the error log for the line that names the cause.
Four quick questions, then a straight answer.
From your answers, this is quietly costing you traffic and nobody has the hours to chase it. Scaplet finds every affected page, drafts the fixes and tracks how AI answers mention you.
A data platform shipped 46 fixes without a ticket.
This looks handled. Re-check after a redesign, a migration or a big content push; that is when it breaks.
Your site is small and you have the time. Work through the steps on this page, starting with the pages that get the most traffic.
Every row comes from the nginx documentation for the named module, except the last, which is simply what a reverse proxy does with an upstream response. The error log usually names the check that refused the request. For a directory with no index file, nginx's source logs directory index of "/path/" is forbidden.
Per the docs, access is allowed only for those networks, excluding 192.168.1.1. Put the specific rules first and deny all last. If you reach the end with deny all, anyone not listed is refused.
When you protect a path by both address and password, the satisfy directive decides how they combine. The default is all, so a visitor must pass every check. This example allows a visitor who either comes from the allowed address or supplies a valid password:
location / {
satisfy any;
allow 192.168.1.0/32;
deny all;
auth_basic "closed site";
auth_basic_user_file conf/htpasswd;
}location / {
deny 192.168.1.1;
allow 192.168.1.0/24;
allow 10.1.1.0/16;
allow 2001:0db8::/32;
deny all;
}A request ending in a slash is handled by the index module, which looks for the files named in index (default index.html). The nginx docs say a request usually goes on to the autoindex module when no index file is found. That module produces a directory listing, but autoindex is off by default. With no index file and the default settings, no handler serves the request, and nginx's source returns 403 and logs directory index of "/path/" is forbidden. Either add the index file or turn the listing on for that location only:
location /downloads/ {
autoindex on;
}The index module also does an internal redirect, so the request can be processed in a different location from the one you expected. A location = / block with index index.html sends "/" to the second location as "/index.html". Check the location that finally handles the request, not only the first.
File permissions come last. When nginx can't open a file because of permissions, it returns 403 and the error log line ends in (13: Permission denied) on Linux. Worker processes run as the user set by the user directive (the default is nobody). That account needs execute permission on every directory in the path and read permission on the file. Test it as that user:
# Show owner and mode for every component of the path
namei -l /var/www/example/index.html
# Try to read the file as the worker user (replace www-data)
sudo -u www-data cat /var/www/example/index.htmlnamei is part of util-linux; the account name varies by distribution. Look at the user line in nginx.conf for yours.
Request the URL with a header dump and compare it to a direct request to the upstream. A 403 only through nginx is an nginx rule.
Open the file set by error_log in your configuration (default logs/error.log, level error). The lines above are logged at the error level, so the default setting shows them.
Access rule, auth_request, referer, index, or permissions. Change only that one.
Reload nginx after the change, then retry with the same IP, Referer and credentials the failing client used.
You can also customize what visitors see. error_page 403 /forbidden.html serves a page of your choice, and the nginx docs show the same directive with a full URL, such as error_page 403 http://example.com/forbidden.html. That changes the page, not the cause.
4 questions people ask next, answered from the sources on this page.
The index module looks for an index file (default index.html). Without one, the request usually falls to the autoindex module, which is off by default, and nginx answers 403. Add the index file, set the index directive to the file you have, or enable autoindex for that location.
Worker processes use the user and group set by the user directive. Make sure that account can read the file and traverse every directory above it, then check the error log for the line that names the cause.
The auth_basic module returns 401 with a Basic challenge when credentials are missing or wrong. A 403 comes from refusals such as allow and deny rules, an auth_request subrequest returning 403, or a referer check using return 403.
satisfy defaults to all, so an address rule and a password must both pass. Add satisfy any if either one should be enough, and check that deny all isn't refusing the visitor first.
Run the free audit, then approve the fixes. Every change can be undone.