Getting forbidden error while accessing a directory in using nginx inside docker
I am using Docker version 4.14 to build a Django application and Nginx as a web service. I have two Dockerfiles, one for nginx and one for the Django app, The Django workflow and API serving are fine with nginx, and even static files are served well through nginx. I use Django logging to log the exceptions in the app to a directory named logs, but when I try to access the directory through nginx, I get a 403 Forbidden error.
**nginx docker file**
FROM nginx:1.21-alpine
RUN rm /etc/nginx/conf.d/default.conf
COPY nginx.conf /etc/nginx/conf.d
**nginx conf**
server {
listen 80;
include mime.types;
types {
text/plain log txt;
}
location / {
proxy_pass http://web:8000;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
proxy_redirect off;
}
location /static/ {
alias /code/static/;
}
location /logs {
alias /code/logs/;
}
}
\*\*root dockerfile
\*\*
FROM python:3.10.2-slim-bullseye
ENV PIP_DISABLE_PIP_VERSION_CHECK 1
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1
WORKDIR /code
COPY ./requirements.txt .
RUN pip install -r requirements.txt
# Copy project
COPY . .
\*\*docker compose
\*\*
version: "3.9"
services:
web:
build: .
ports:
\- "8000:8000"
command: gunicorn tech_plotz.wsgi:application --bind 0.0.0.0:8000
volumes:
\- static_volume:/code/static
\- logs_volume:/code/logs
expose:
\- 8000
depends_on:
\- db
db:
image: postgres:13
volumes:
\- postgres_data:/var/lib/postgresql/data/
environment:
\- "POSTGRES_HOST_AUTH_METHOD=trust"
nginx:
build: ./nginx
ports:
\- 1337:80
volumes:
\- static_volume:/code/static
\- logs_volume:/code/logs
depends_on:
\- web
volumes:
postgres_data:
static_volume:
logs_volume:
how can i access the logs directory from the nginx?
In a shared Docker compose, shared directories need to be served by nginx.
The 403 Forbidden error you're getting may be caused by Nginx not having the necessary permissions to access the logs directory. To resolve this issue, you can modify the logs location block in the Nginx configuration to include the necessary permissions.
For example:
location /logs {
alias /code/logs/;
autoindex on;
autoindex_exact_size off;
autoindex_localtime on;
}
This will enable the Nginx autoindex module and allow it to display the contents of the logs directory. You may also need to modify the owner and permissions of the logs directory to ensure that Nginx has the necessary permissions to access it.
You can modify the owner and permissions of the logs directory using the chown
and chmod
commands in the Dockerfile for your Django app.
FROM python:3.10.2-slim-bullseye
ENV PIP_DISABLE_PIP_VERSION_CHECK 1
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1
WORKDIR /code
COPY ./requirements.txt .
RUN pip install -r requirements.txt
# Copy project
COPY . .
# Modify owner and permissions of logs directory
RUN chown -R nginx:nginx /code/logs
RUN chmod 755 /code/logs
changing the owner of the logs directory to the nginx user and group, and giving the directory 755 permissions (rwx for the owner, r-x for others). You can modify the owner and permissions to match your specific requirements.