From 4924122cee4b93c06d02f8223ad391e9f8039dc4 Mon Sep 17 00:00:00 2001 From: minjungw00 Date: Thu, 7 Nov 2024 14:06:26 +0900 Subject: [PATCH] =?UTF-8?q?build:=20NginX=20Docker=20=EC=9D=B4=EB=AF=B8?= =?UTF-8?q?=EC=A7=80=20=EC=83=9D=EC=84=B1=EC=97=90=20=EB=8C=80=ED=95=9C=20?= =?UTF-8?q?Dockerfile=20=EC=83=9D=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - NginX 이미지 Dockerfile 생성 - NginX 설정 파일 생성 #005 --- nginx/Dockerfile | 8 ++++++++ nginx/Dockerfile.dev | 9 +++++++++ nginx/default.conf | 32 ++++++++++++++++++++++++++++++++ nginx/default.dev.conf | 36 ++++++++++++++++++++++++++++++++++++ 4 files changed, 85 insertions(+) create mode 100644 nginx/Dockerfile create mode 100644 nginx/Dockerfile.dev create mode 100644 nginx/default.conf create mode 100644 nginx/default.dev.conf diff --git a/nginx/Dockerfile b/nginx/Dockerfile new file mode 100644 index 00000000..eddbc882 --- /dev/null +++ b/nginx/Dockerfile @@ -0,0 +1,8 @@ +FROM nginx:alpine +WORKDIR /usr/share/nginx/html + +# Nginx 기본 설정 복사 +COPY ./nginx/default.conf /etc/nginx/conf.d/default.conf + +EXPOSE 80 +CMD ["nginx", "-g", "daemon off;"] \ No newline at end of file diff --git a/nginx/Dockerfile.dev b/nginx/Dockerfile.dev new file mode 100644 index 00000000..7c4aafb3 --- /dev/null +++ b/nginx/Dockerfile.dev @@ -0,0 +1,9 @@ +FROM nginx:alpine +WORKDIR /usr/share/nginx/html + +# Nginx 기본 설정 복사 +COPY ./nginx/default.dev.conf /etc/nginx/conf.d/default.conf + +# 정적 파일을 호스트에서 복사 (개발 환경에서 핫 리로딩 사용 가능) +EXPOSE 80 +CMD ["nginx", "-g", "daemon off;"] \ No newline at end of file diff --git a/nginx/default.conf b/nginx/default.conf new file mode 100644 index 00000000..268c9e3c --- /dev/null +++ b/nginx/default.conf @@ -0,0 +1,32 @@ +# 백엔드 서버 정의 +upstream backend { + server backend:3000; # 백엔드 서버 (NestJS) +} + +server { + listen 80; + + # /api 경로로 들어오는 요청은 백엔드로 전달 + location /api { + proxy_pass http://backend; # 백엔드로 요청 전달 + proxy_http_version 1.1; + proxy_set_header Upgrade $http_upgrade; + proxy_set_header Connection "Upgrade"; + proxy_set_header Host $host; + proxy_set_header X-Real-IP $remote_addr; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + } + + # 정적 파일을 제공하는 기본 경로 설정 + location / { + root /usr/share/nginx/html; # React 빌드 결과물이 위치한 디렉터리 + index index.html; # 기본 진입점 파일 + try_files $uri /index.html; # SPA 라우팅 지원 + } + + # 404 에러 페이지 설정 + error_page 404 /404.html; + location = /404.html { + root /usr/share/nginx/html; + } +} diff --git a/nginx/default.dev.conf b/nginx/default.dev.conf new file mode 100644 index 00000000..f5c8c8f7 --- /dev/null +++ b/nginx/default.dev.conf @@ -0,0 +1,36 @@ +# 백엔드와 프론트엔드에 대한 업스트림 서버 정의 +upstream backend { + server backend:3000; # 백엔드 서버 (NestJS) +} + +upstream frontend { + server frontend:5173; # 프론트엔드 서버 (React) +} + +server { + listen 80; + + # /api 경로로 들어오는 요청은 백엔드로 전달 + location /api { + proxy_pass http://backend; # 백엔드로 요청 전달 + proxy_http_version 1.1; + proxy_set_header Upgrade $http_upgrade; + proxy_set_header Connection "Upgrade"; + proxy_set_header Host $host; + proxy_set_header X-Real-IP $remote_addr; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + } + + # 기본 경로는 프론트엔드로 전달 + location / { + proxy_pass http://frontend; + } + + # /sockjs-node 경로 (React의 핫 리로딩 웹소켓 연결) + location /sockjs-node { + proxy_pass http://frontend; + proxy_http_version 1.1; + proxy_set_header Upgrade $http_upgrade; + proxy_set_header Connection "Upgrade"; + } +} \ No newline at end of file