CICD

실제 운영환경에 가까운 nginx.conf 예제

마시멜로를찾아서 2025. 4. 2. 14:54
반응형

✅ 1. HTTPS 설정 예제 (ssl_certificate, ssl_certificate_key)

nginx
 
server {
    listen 443 ssl;                       # HTTPS 포트
    server_name example.com;             # 도메인명

    ssl_certificate     /etc/ssl/certs/example.com.crt;   # 인증서 경로
    ssl_certificate_key /etc/ssl/private/example.com.key; # 키 파일 경로
    ssl_protocols       TLSv1.2 TLSv1.3;  # 보안 프로토콜
    ssl_ciphers         HIGH:!aNULL:!MD5; # 암호화 알고리즘

    location / {
        root   /usr/share/nginx/html;
        index  index.html;
    }
}

Tip: Let's Encrypt로 무료 인증서를 자동 발급하려면 Certbot과 연동하세요.


✅ 2. Production용 Gzip 압축 + 캐시 설정

nginx
 
http {
    gzip on;
    gzip_comp_level 5;                    # 압축 레벨 (1~9)
    gzip_types text/plain application/json text/css application/javascript;

    server {
        location ~* \.(js|css|png|jpg|svg|woff2?)$ {
            expires 30d;                   # 30일 캐시
            add_header Cache-Control "public, max-age=2592000";
        }
    }
}

효과:

  • Gzip으로 응답 사이즈 최소화
  • 정적 자산에 강력한 캐시 적용 → 페이지 속도 향상

✅ 3. React / Vue 정적 배포용 Nginx 설정

nginx
 
server {
    listen 80;
    server_name yourdomain.com;

    root /usr/share/nginx/html;            # build 결과 위치 (예: React의 build/)
    index index.html;

    location / {
        try_files $uri $uri/ /index.html;  # SPA 라우팅 지원
    }

    location ~* \.(js|css|png|jpg|svg|woff2?)$ {
        expires 7d;
        add_header Cache-Control "public";
    }
}

📦 build 결과물을 /usr/share/nginx/html에 복사한 뒤 도커 컨테이너로 배포하거나, EC2에 nginx 설치 후 serve하면 됩니다.


✅ 4. FastAPI / NestJS 백엔드 리버스 프록시 설정

nginx
 
server {
    listen 80;
    server_name api.example.com;

    location / {
        proxy_pass         http://localhost:8000;   # FastAPI / NestJS 서버
        proxy_http_version 1.1;
        proxy_set_header   Host $host;
        proxy_set_header   X-Real-IP $remote_addr;
        proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header   X-Forwarded-Proto $scheme;
    }
}
  • NestJS 기본 포트: 3000, FastAPI 기본 포트: 8000
  • HTTPS 적용 시엔 위 예제와 ssl_certificate를 함께 설정하세요

✅ 요약


HTTPS 적용 ssl_certificate, listen 443 ssl
Gzip 압축 gzip on, gzip_types, gzip_comp_level
정적 프론트엔드 호스팅 root, try_files, `location ~* .(js
백엔드 API 연동 proxy_pass, proxy_set_header 등 리버스 프록시 설정
반응형