알겠습니다. 해커들의 무작위 스캐닝을 원천 차단하고, 프론트엔드(hls.js)와의 통신을 위한 CORS 설정, 그리고 HLS 스트리밍 최적화까지 모두 반영된 실무용 Nginx 설정
알겠습니다. 해커들의 무작위 스캐닝을 원천 차단하고, 프론트엔드(hls.js)와의 통신을 위한 CORS 설정, 그리고 HLS 스트리밍 최적화까지 모두 반영된 실무용 Nginx 설정
알겠습니다. 해커들의 무작위 스캐닝을 원천 차단하고, 프론트엔드(hls.js)와의 통신을 위한 CORS 설정, 그리고 HLS 스트리밍 최적화까지 모두 반영된 실무용 Nginx 설정입니다.
프로젝트 루트 디렉토리에 nginx 폴더를 만들고 아래 파일을 작성하세요.
1. Nginx 보안 및 HLS 최적화 설정
nginx/nginx.conf
Nginx
worker_processes auto;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
# [보안] Nginx 버전 숨기기 (버전별 알려진 취약점 공격 방지)
server_tokens off;
# [최적화] 파일 전송 효율 극대화
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
server {
listen 80;
server_name localhost;
# ==========================================
# 1. 보안: 숨김 파일 및 디렉토리 접근 차단 (봇 방어 핵심)
# ==========================================
# .env, .git, .vscode 등 '.'으로 시작하는 모든 경로 요청을 403 Forbidden으로 쳐냅니다.
# access_log off를 통해 봇들이 찌르고 간 불필요한 에러 로그가 서버 용량을 차지하지 않게 합니다.
location ~ /\. {
deny all;
access_log off;
log_not_found off;
}
# ==========================================
# 2. HLS 미디어 스트리밍 서빙 (/videos/)
# ==========================================
location /videos/ {
# docker-compose에서 마운트한 내부 경로
alias /usr/share/nginx/html/videos/;
# [CORS 설정] React(14000 포트) 프론트엔드의 hls.js가 영상을 가져갈 수 있도록 허용
add_header 'Access-Control-Allow-Origin' '*' always;
add_header 'Access-Control-Allow-Methods' 'GET, OPTIONS' always;
add_header 'Access-Control-Allow-Headers' 'Origin, X-Requested-With, Content-Type, Accept, Authorization' always;
# OPTIONS 요청(Preflight)에 대한 빠른 응답 처리
if ($request_method = 'OPTIONS') {
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Methods' 'GET, OPTIONS';
add_header 'Access-Control-Allow-Headers' 'Origin, X-Requested-With, Content-Type, Accept, Authorization';
add_header 'Access-Control-Max-Age' 1728000;
add_header 'Content-Type' 'text/plain; charset=utf-8';
add_header 'Content-Length' 0;
return 204;
}
# [캐시 설정] m3u8(재생목록)은 캐싱하지 않고 항상 최신 상태 유지
location ~ \.m3u8$ {
add_header Cache-Control "no-cache, no-store, must-revalidate";
add_header 'Access-Control-Allow-Origin' '*' always;
}
# [캐시 설정] ts(영상 청크)는 내용이 변하지 않으므로 길게 캐싱 (서버 부하 감소)
location ~ \.ts$ {
add_header Cache-Control "public, max-age=3600";
add_header 'Access-Control-Allow-Origin' '*' always;
}
}
# 기타 잘못된 요청에 대한 기본 에러 처리
error_page 404 /404.html;
location = /404.html {
internal;
}
}
}
2. Docker Compose 파일 수정 (Nginx 마운트 추가)
작성한 nginx.conf를 Nginx 컨테이너가 읽을 수 있도록 docker-compose.yml 파일의 isp_edutube_media_server 부분을 아래와 같이 수정합니다.
docker-compose.yml (수정 부분)
YAML
# 5. Media Server (Nginx) - 정적 HLS 파일 서빙 전용
isp_edutube_media_server:
image: nginx:alpine
container_name: isp_edutube_media_server
restart: always
ports:
- "14004:80"
volumes:
# 위에서 작성한 커스텀 설정 파일을 컨테이너 내부의 설정 파일로 덮어씌움 (읽기 전용: ro)
- ./nginx/nginx.conf:/etc/nginx/nginx.conf:ro
# 영상 파일들이 저장되는 공유 볼륨
- video_storage:/usr/share/nginx/html/videos:ro
networks:
- edutube_net
이제 도커를 재시작(docker-compose down 후 docker-compose up -d)하면, .env나 .git 등 민감한 파일을 훔쳐보려는 외부 봇들의 스캐닝이 Nginx 단에서 완벽하게 차단되며(HTTP 상태 코드 403 반환), 불필요한 로그도 쌓이지 않아 안심하고 서비스할 수 있습니다.
댓글
댓글 쓰기