nginx returns 403 and logs directory index is forbidden
403 ForbiddenShort answer
nginx got a request for a folder, found none of the index files it was told to look for, and listing the folder is off by default. So it answers 403. Check the error log for the exact path, then make sure index.html exists there, root points at the right folder, and the worker user can read it.
Fix it fast
1Find out which cause you have
Read the newest error log lines. They show the exact folder or file nginx tried and whether the reason was a missing index or a permission.
sudo tail -n 20 /var/log/nginx/error.log2Match what you see, then jump to its fix
- 1
directory index of "..." is forbiddenThere is no index file in that folderFix - 2
The path in the log is /usr/share/nginx/html/ and the folder listing is emptyThe folder is empty or wrong in a Docker nginx containerFix - 3
(13: Permission denied) in the log next to the pathThe worker user cannot read the folderFix
- 1
What does the error look like?
403 Forbiddendirectory index of "/usr/share/nginx/html/" is forbiddenopen() "/path/index.html" failed (13: Permission denied)
Cause 1 of 3
There is no index file in that folder
1Check
The index directive defaults to index.html. If no listed file exists in the folder, nginx falls through to the directory listing module, and autoindex is off unless you set it.
ls -l /usr/share/nginx/html
grep -rn "index\|autoindex" /etc/nginx/2Fix
Put the index file in the folder, or name the file your site really uses in the index directive. Turn on autoindex only if a public file listing is what you want.
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}3Verify
Test the config, reload and request the folder.
sudo nginx -t && sudo systemctl reload nginx
curl -I http://localhost/Confirmed by nginx.org
Cause 2 of 3
The folder is empty or wrong in a Docker nginx container
1Check
A bind mount to the web root with a wrong or empty host path hides the content the image had. The same happens when the COPY source in the Dockerfile is wrong.
docker exec <container> ls -l /usr/share/nginx/html
docker inspect --format '{{json .Mounts}}' <container>2Fix
Mount or copy the folder that holds index.html into /usr/share/nginx/html.
docker run --name web -p 8080:80 -d \
-v "$(pwd)/site":/usr/share/nginx/html:ro nginx3Verify
The page loads.
curl -I http://localhost:8080/Confirmed by hub.docker.com
Cause 3 of 3
The worker user cannot read the folder
1Check
Worker processes run as the user set by the user directive (default nobody). That account needs read on the files and, as path_resolution(7) explains, search (execute) permission on every parent folder.
grep -n "^user" /etc/nginx/nginx.conf
namei -l /var/www/mysite/index.html2Fix
Give the worker user read access to the site files and execute on each folder above them, or move the site to a path it can reach, such as /var/www.
sudo chmod o+x /var/www
sudo chmod -R o+rX /var/www/mysite3Verify
Request the page again.
curl -I http://localhost/Confirmed by man7.org
How do I stop it coming back?
- Keep sites under /var/www or /usr/share/nginx/html, not in a home folder.
- Run nginx -t before every reload and read the error log after it.
- Add a build step that fails when index.html is missing from the output folder.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- Dispatch ProA real-time logistics platform: Flutter apps, Spring Boot services and a Kafka event pipeline on AWS.
- stegnor.comMy company site: 804 pages from a Python and Jinja generator, with 15 free browser tools.
Related fixes
Servers
nginx 502 Bad Gateway in front of a Node.js app
connect() failed (111: Connection refused) while connecting to upstream
A 502 means nginx reached for your Node.js app and got nothing usable back.
5 causes, each with a fixServers
Angular routes return 404 from nginx when you refresh or open a link
The home page loads, but refreshing /some/route shows an nginx 404
Angular routes like /users/42 are not files, so nginx looks for them on disk and returns 404.
3 causes, each with a fixServers
Apache logs SIGWINCH and exits on its own
the httpd container stops although nothing crashed
SIGWINCH is Apache's graceful-stop signal.
2 causes, each with a fixServers
sam local fails: Running AWS SAM projects locally requires Docker
Cannot connect to the Docker daemon. Is the docker daemon running on this host?
sam local start-api and sam local invoke run your function inside a Docker container, so SAM stops if it cannot talk to Docker.
3 causes, each with a fixServers
dnf fails to download repo metadata in a CentOS 8 Docker build
The same Dockerfile built fine before 2022
CentOS Linux 8 reached end of life on 31 December 2021 and its content left the mirrors.
2 causes, each with a fixServers
A Let's Encrypt certificate did not renew with Certbot
Type: connection ... Timeout during connect (likely firewall problem)
Run sudo certbot renew --dry-run and read the Type and Detail lines.
6 causes, each with a fix