Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Servers, nginx and Docker

nginx returns 403 and logs directory index is forbidden

Error403 Forbidden

Short 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.

  • Applies to nginx 1.26, nginx Docker image, Ubuntu 24.04
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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.log
  2. 2Match what you see, then jump to its fix

    1. 1directory index of "..." is forbiddenThere is no index file in that folderFix
    2. 2The 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. 3(13: Permission denied) in the log next to the pathThe worker user cannot read the folderFix

What does the error look like?

  • 403 Forbidden
  • directory index of "/usr/share/nginx/html/" is forbidden
  • open() "/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

Did this fix it?

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 nginx

3Verify

The page loads.

curl -I http://localhost:8080/

Confirmed by hub.docker.com

Did this fix it?

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.html

2Fix

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/mysite

3Verify

Request the page again.

curl -I http://localhost/

Confirmed by man7.org

Did this fix it?

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:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.