Docker-compose ) nginx로 배포 시, static 파일 깨지는 문제

boingboing·2022년 1월 10일
0

현상

  • docker compose로 gunicorn & nginx로 장고 프로젝트 배포.
  • static 파일 요청 주소 : /static/앱이름/image/이미지명.png으로 요청 시, 404 에러 발생함.

원인

  • nginx 컨테이너에는 장고 프로젝트 파일이 없음. 그러나 nginx가 장고의 static 파일을 읽어야 함.
    -> 장고의 static 파일을 nginx에 볼륨 마운트 해주어야 함.
  • 그러나 현재 nginx 컨테이너에 volume이 제대로 설정이 안 되어 있엇음.
  • nginx 파일 (nginx.conf)에서 location 설정이 되 있음.
docker-compose.yml 

nginx:
	volumes:
    	- ./nginx:/etc/nginx/conf.d   (로컬의 nginx 설정파일 위치: 도커 내부의 nginx 파일 위치)
        - ./project_name/collected_statics:/staticfiles (로컬의 static 파일 위치:도커의 static 파일 위치)

로컬의 nginx 폴더에 있는 default.conf

---default.conf

server {

  client_max_body_size 16M;

  # static 파일을 제공해야할 경우
  location /static/ { # url에 /static 요청 온 경우
    alias /staticfiles/; # nginx 컨테이너 내에서 django static 파일 갖고 있는 디렉토리
  }

  # media 파일 제공
  #location /media/ {
  #  autoindex on;
  #  alias /code/media/;
  #}

  # 프록시 설정, nginx 뒤에 WAS가 있을 경우
  location / {
    proxy_pass http://django/;
  }

  # 포트 설정
  listen 80;
  server_name localhost;
}

0개의 댓글