How to Fix 403 Forbidden Error in Nginx Inside Docker
The 403 forbidden nginx error in a Docker container usually occurs when the web server does not have read permissions to access the files of the mounted local volume, or when it does not find a valid indexed start file in the root directory of the service path.
Quick Diagnostics
chmod -R 755 on dirs and 644 on filesThe 403 forbidden nginx error in a Docker container usually occurs when the web server does not have read permissions to access the files of the mounted local volume, or when it does not find a valid indexed start file in the root directory of the service path.
Step-by-Step Solution
-
1
Step 1: Corregir los permisos del sistema de archivos local
The user inside the Nginx container (usually
nginxorwww-datawith UID 101 or 82) requires read permissions on files and execution permissions on directories in your shared folder. On your Linux server, adjust the privileges by running:BASH# Cambiar la propiedad al usuario y grupo del servidor web si es necesario, # o asegurar permisos de lectura globales (lectura para archivos, lectura+ejecución para carpetas): sudo find /ruta/local/a/tus/archivos/web -type d -exec chmod 755 {} \; sudo find /ruta/local/a/tus/archivos/web -type f -exec chmod 644 {} \; -
2
Step 2: Verificar el archivo de inicio (Index)
Make sure that the
serverblock in your Nginx configuration (nginx.confor the file inconf.d) looks for an existing file. Check that the main file of your directory is named exactly as follows and in lowercase:index.htmlindex.php
A mismatch in capitalization (such as
Index.htmlorINDEX.HTML) will cause the 403 failure immediately on Linux systems because the filesystem is case-sensitive.
Prevention Advice
Recommended security practices:
- When working on Red Hat-based distributions (such as CentOS, Rocky Linux, or Fedora), prevent SELinux security policies on the host system from blocking access to the container's file descriptors. Whenever you configure a local volume mount, add the shared security flag
:Zto the end of the volume path in your command or Docker Compose file:YAMLvolumes: - /ruta/local:/usr/share/nginx/html:Z