Error related to connecting Django from Docker to Nginx
I am currently working on Docker and have done the following:
- The Django test page was working fine on localhost.
- I connected it to Docker.
- I also connected Docker to Gunicorn. However, when I try to connect it through Nginx, only the default Nginx page is displayed, and it is not connecting to the application I created.
nginx/Dockerfile
FROM nginx:latest
RUN rm /etc/nginx/conf.d/default.conf
COPY nginx.conf /etc/nginx/conf.d
# nginx/nginx.conf
upstream daniel_blog {
server web:8000;
}
server {
listen 80;
server_name localhost;
# Default location
location / {
proxy_pass http://daniel_blog;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
proxy_redirect off;
}
# Static files
location /static/ {
alias /usr/src/app/_static/;
}
# Media files
location /media/ {
alias /usr/src/app/_media/;
}
}
---
# .env.dev
DEBUG="1"
SECRET_KEY="django-insecure-88ndzs0z0uzko4x5f=*ngu=#h+a(k=!#ooyvt*0h+78r1n=^oy"
DJANGO_ALLOWED_HOSTS = localhost 127.0.0.1 [::1]
SQL_ENGINE=django.db.backends.postgresql
SQL_DATABASE=daniel_blog_dev
SQL_USER=daniel_blog_user
SQL_PASSWORD=daniel_blog_db_password
SQL_HOST=db
SQL_PORT=5432
---
# .env.prod
DEBUG=0
SECRET_KEY=w4+=--yyyigm1-=na@9et*1ldt-q-x4x=57m2xkiru6!oxeh5b
DJANGO_ALLOWED_HOSTS=localhost 127.0.0.1 [::1]
SQL_ENGINE=django.db.backends.postgresql
SQL_DATABASE=daniel_blog_prod
SQL_USER=daniel_blog_user
SQL_PASSWORD=daniel_blog_db_password
SQL_HOST=db
SQL_PORT=5432
---
# .env.prod.db
POSTGRES_USER=daniel_blog_user
POSTGRES_PASSWORD=daniel_blog_db_password
POSTGRES_DB=daniel_blog_prod
---
# docker-compose.yml
services:
nginx:
build: ./nginx
volumes:
- static_volumes:/usr/src/app/_static
- media_volumes:/usr/src/app/_media
ports:
- 80:80
depends_on:
- web
web:
build: .
command: gunicorn daniel_blog.wsgi:application --bind 0.0.0.0:8000
volumes:
- static_volumes:/usr/src/app/_static
- media_volumes:/usr/src/app/_media
- ./:/usr/src/app/_media
expose:
- 8000
env_file:
- ./.env.prod
depends_on:
- db
db:
image: postgres:17.0-alpine
volumes:
- postgres_data:/var/lib/postgresql/data/
env_file:
- ./.env.prod.db
volumes:
postgres_data:
static_volumes:
media_volumes:
---
# Dockerfile
FROM python:3.11-slim
RUN apt-get update && apt-get install -y \
build-essential \
libffi-dev \
libssl-dev \
&& rm -rf /var/lib/apt/lists/*
RUN apt-get update && apt-get install -y libpq-dev gcc
WORKDIR /usr/src/app
COPY requirements.txt ./
RUN pip install --no-cache-dir -r requirements.txt
COPY requirements.txt ./
COPY . .
---
# settings.py - DATABASES
DATABASES = {
'default': {
'ENGINE': os.environ.get("SQL_ENGINE", 'django.db.backends.postgresql'),
'NAME': os.environ.get('SQL_DATABASE', 'daniel_blog_dev'),
'USER': os.environ.get('SQL_USER', 'daniel_blog_user'),
'PASSWORD': os.environ.get('SQL_PASSWORD', 'daniel_blog_db_password'), # 환경 변수에서 비밀번호 가져오기
'HOST': os.environ.get('SQL_HOST', 'localhost'), # db 컨테이너의 서비스 이름 사용
'PORT': os.environ.get('SQL_PORT', '5432'), # 기본 포트 5432
"OPTIONS": {
"client_encoding": "UTF8"
}
}
}