Skip to content

Commit 796bcef

Browse files
Merge pull request #330 from boostcampwm-2024/feature-shared-#284
Production 환경 구성
2 parents 191412c + 54827ca commit 796bcef

File tree

6 files changed

+224
-1
lines changed

6 files changed

+224
-1
lines changed

apps/backend/src/main.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,11 @@ async function bootstrap() {
2525
const documentFactory = () => SwaggerModule.createDocument(app, config);
2626
SwaggerModule.setup('api', app, documentFactory);
2727
app.enableCors({
28-
origin: process.env.origin,
28+
origin:
29+
process.env.NODE_ENV === 'production'
30+
? ['https://octodocs.com', 'https://www.octodocs.com']
31+
: process.env.origin,
32+
credentials: true,
2933
});
3034
app.use(cookieParser());
3135
await app.listen(3000);

compose.init.yml

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
version: "3.8"
2+
3+
services:
4+
nginx:
5+
image: nginx:alpine
6+
restart: always
7+
ports:
8+
- "80:80"
9+
volumes:
10+
- ./services/nginx/conf.d/prod_nginx_init.conf:/etc/nginx/conf.d/default.conf
11+
- ./data/certbot/www:/var/www/certbot
12+
networks:
13+
- frontend
14+
15+
certbot:
16+
image: certbot/certbot:latest
17+
volumes:
18+
- ./data/certbot/conf:/etc/letsencrypt
19+
- ./data/certbot/www:/var/www/certbot
20+
- ./data/certbot/log:/var/log/letsencrypt
21+
command: >
22+
certonly --webroot
23+
--webroot-path=/var/www/certbot
24+
25+
--agree-tos
26+
--no-eff-email
27+
-d octodocs.com
28+
-d www.octodocs.com
29+
30+
networks:
31+
frontend:
32+
driver: bridge

compose.prod.yml

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
version: "3.8"
2+
3+
services:
4+
nginx:
5+
image: nginx:alpine
6+
restart: always
7+
ports:
8+
- "80:80"
9+
- "443:443"
10+
volumes:
11+
- ./services/nginx/conf.d/prod_nginx.conf:/etc/nginx/conf.d/default.conf
12+
- ./data/certbot/conf:/etc/letsencrypt:ro
13+
- ./data/certbot/www:/var/www/certbot
14+
- ./apps/frontend/dist:/usr/share/nginx/html:ro
15+
networks:
16+
- frontend
17+
depends_on:
18+
- backend
19+
20+
backend:
21+
build:
22+
context: .
23+
dockerfile: ./services/backend/Dockerfile.prod
24+
image: backend:latest
25+
env_file:
26+
- .env.prod
27+
volumes:
28+
- .env.prod:/app/.env
29+
expose:
30+
- "3000"
31+
- "1234"
32+
networks:
33+
- frontend
34+
- backend
35+
depends_on:
36+
- redis
37+
38+
redis:
39+
image: redis:latest
40+
environment:
41+
REDIS_HOST: ${REDIS_HOST}
42+
REDIS_PORT: ${REDIS_PORT}
43+
networks:
44+
- backend
45+
46+
certbot-renewer:
47+
image: certbot/certbot:latest
48+
volumes:
49+
- ./data/certbot/conf:/etc/letsencrypt
50+
- ./data/certbot/www:/var/www/certbot
51+
- ./data/certbot/log:/var/log/letsencrypt
52+
entrypoint: "/bin/sh -c 'trap exit TERM; while :; do certbot renew --webroot --webroot-path=/var/www/certbot; sleep 12h & wait $${!}; done;'"
53+
54+
networks:
55+
frontend:
56+
driver: bridge
57+
backend:
58+
driver: bridge

services/backend/Dockerfile.prod

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# 빌드 스테이지
2+
FROM node:20-alpine as builder
3+
4+
WORKDIR /app
5+
6+
# 의존성 파일 복사
7+
COPY package.json yarn.lock ./
8+
COPY turbo.json ./
9+
COPY apps/backend/package.json ./apps/backend/
10+
COPY apps/frontend/package.json ./apps/frontend/
11+
12+
# 의존성 설치
13+
RUN yarn install --frozen-lockfile
14+
15+
# 소스 코드 복사
16+
COPY . .
17+
18+
# 백엔드 빌드
19+
RUN yarn turbo run build --filter=backend
20+
21+
# 실행 스테이지
22+
FROM node:20-alpine
23+
24+
WORKDIR /app
25+
26+
# 프로덕션에 필요한 파일만 복사
27+
COPY --from=builder /app/package.json /app/yarn.lock ./
28+
COPY --from=builder /app/apps/backend/package.json ./apps/backend/
29+
COPY --from=builder /app/apps/backend/dist ./apps/backend/dist
30+
31+
# 프로덕션 의존성만 설치
32+
RUN yarn install --frozen-lockfile --production
33+
34+
ENV NODE_ENV=production
35+
36+
EXPOSE 3000 1234
37+
38+
CMD ["yarn", "start"]
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
server {
2+
listen 80;
3+
server_name octodocs.com www.octodocs.com;
4+
5+
# Certbot 인증용 경로 (최상단에 위치)
6+
location ^~ /.well-known/acme-challenge/ {
7+
root /var/www/certbot;
8+
try_files $uri =404;
9+
break;
10+
}
11+
12+
# 나머지 모든 HTTP 트래픽은 HTTPS로 리다이렉트
13+
location / {
14+
return 301 https://$server_name$request_uri;
15+
}
16+
}
17+
18+
server {
19+
listen 443 ssl;
20+
server_name octodocs.com www.octodocs.com;
21+
22+
# Let's Encrypt 인증서 경로
23+
ssl_certificate /etc/letsencrypt/live/octodocs.com/fullchain.pem;
24+
ssl_certificate_key /etc/letsencrypt/live/octodocs.com/privkey.pem;
25+
26+
# SSL 설정
27+
ssl_protocols TLSv1.2 TLSv1.3;
28+
ssl_ciphers HIGH:!aNULL:!MD5;
29+
ssl_prefer_server_ciphers on;
30+
31+
# 인증서가 없을 때 fallback
32+
ssl_trusted_certificate /etc/letsencrypt/live/octodocs.com/chain.pem;
33+
ssl_stapling on;
34+
ssl_stapling_verify on;
35+
36+
# 에러 페이지 설정
37+
error_page 497 https://$server_name$request_uri;
38+
39+
# gzip 압축 설정
40+
gzip on;
41+
gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
42+
43+
# 프론트엔드 정적 파일
44+
location / {
45+
root /usr/share/nginx/html;
46+
try_files $uri $uri/ /index.html;
47+
expires 30d;
48+
}
49+
50+
# API 프록시
51+
location /api {
52+
proxy_pass http://backend:3000;
53+
proxy_http_version 1.1;
54+
proxy_set_header Upgrade $http_upgrade;
55+
proxy_set_header Connection 'upgrade';
56+
proxy_set_header Host $host;
57+
proxy_cache_bypass $http_upgrade;
58+
}
59+
60+
# Socket.IO 프록시 (일반 웹소켓)
61+
location /socket.io {
62+
proxy_pass http://backend:1234;
63+
proxy_http_version 1.1;
64+
proxy_set_header Upgrade $http_upgrade;
65+
proxy_set_header Connection "Upgrade";
66+
proxy_set_header Host $host;
67+
}
68+
69+
# Y-Socket.IO 프록시 (YJS 웹소켓)
70+
location /flow-room {
71+
proxy_pass http://backend:1234;
72+
proxy_http_version 1.1;
73+
proxy_set_header Upgrade $http_upgrade;
74+
proxy_set_header Connection "Upgrade";
75+
proxy_set_header Host $host;
76+
}
77+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
server {
2+
listen 80;
3+
server_name octodocs.com www.octodocs.com;
4+
5+
# Certbot 인증용 경로
6+
location ^~ /.well-known/acme-challenge/ {
7+
root /var/www/certbot;
8+
}
9+
10+
# 나머지 요청에 대한 처리 (필요에 따라 수정)
11+
location / {
12+
return 200 '인증서 발급을 위한 임시 nginx 서버입니다.';
13+
}
14+
}

0 commit comments

Comments
 (0)