nginx proxy 설정

By | 2025년 9월 10일
Table of Contents

nginx proxy 설정

https://web.skyer9.pe.kr -> https://dev-api.skyer9.pe.kr:8443 -> http://localhost:9000 으로의 호출 전달을 처리합니다.

nginx 에서는 ssl 처리를 해야 하고, CORS 를 처리합니다.

설정

# dev-api.skyer9.pe.kr:8443 -> localhost:9000 프록시 설정
server {
    listen 8443 ssl;
    server_name dev-api.skyer9.pe.kr;

    # SSL 인증서 설정
    ssl_certificate /home/skyer9/cert/fullchain.pem;
    ssl_certificate_key /home/skyer9/cert/privkey.pem;

    # SSL 보안 설정
    ssl_protocols TLSv1.2 TLSv1.3;
    ssl_ciphers ECDHE-RSA-AES256-GCM-SHA512:DHE-RSA-AES256-GCM-SHA512:ECDHE-RSA-AES256-GCM-SHA384:DHE-RSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-SHA384;
    ssl_prefer_server_ciphers off;
    ssl_session_cache shared:SSL:10m;
    ssl_session_timeout 10m;

    # 프록시 설정
    location / {
        proxy_pass http://localhost:9000;
        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;
        proxy_set_header X-Forwarded-Port $server_port;
        # proxy_set_header User-Agent $http_user_agent; # 자동으로 전달됨

        # CORS 헤더 추가
        add_header 'Access-Control-Allow-Origin' 'https://web.skyer9.pe.kr' always;
        add_header 'Access-Control-Allow-Methods' 'GET, POST, PUT, DELETE, OPTIONS, PATCH' always;
        add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range,Authorization' always;
        add_header 'Access-Control-Allow-Credentials' 'true' always;

        # Preflight 요청 처리
        if ($request_method = 'OPTIONS') {
            add_header 'Access-Control-Allow-Origin' 'https://web.skyer9.pe.kr';
            add_header 'Access-Control-Allow-Methods' 'GET, POST, PUT, DELETE, OPTIONS, PATCH';
            add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range,Authorization';
            add_header 'Access-Control-Max-Age' 1728000;
            add_header 'Content-Type' 'text/plain; charset=utf-8';
            add_header 'Content-Length' 0;
            return 204;
        }

        # 타임아웃 설정
        proxy_connect_timeout 30s;
        proxy_send_timeout 30s;
        proxy_read_timeout 30s;

        # 버퍼 설정
        proxy_buffering on;
        proxy_buffer_size 4k;
        proxy_buffers 8 4k;
    }

    # 로그 설정
    # access_log /var/log/nginx/dev-api.skyer9.pe.kr.access.log;
    # error_log /var/log/nginx/dev-api.skyer9.pe.kr.error.log;
}

SSL/TLS 보안 설정

  • ssl_certificate: SSL 인증서 파일 경로 (공개키 포함)
  • ssl_certificate_key: SSL 개인키 파일 경로
  • ssl_protocols TLSv1.2 TLSv1.3: 사용할 TLS 프로토콜 버전 제한 (보안상 구버전 차단)
  • ssl_ciphers: 허용할 암호화 알고리즘 목록 (강력한 암호화만 허용)
  • ssl_prefer_server_ciphers off: 클라이언트가 선호하는 암호화 방식 우선 사용
  • ssl_session_cache shared:SSL:10m: SSL 세션을 10MB 메모리에 캐시하여 성능 향상
  • ssl_session_timeout 10m: SSL 세션 캐시 유지 시간 (10분)

프록시 설정

  • proxy_pass http://localhost:9000: 요청을 로컬 9000번 포트로 전달
  • proxy_set_header Host $host: 원본 호스트명 전달
  • proxy_set_header X-Real-IP $remote_addr: 클라이언트의 실제 IP 주소 전달
  • proxy_set_header X-Forwarded-For: 프록시 체인을 통과한 모든 IP 주소 기록
  • proxy_set_header X-Forwarded-Proto $scheme: 원본 프로토콜(http/https) 정보 전달
  • proxy_set_header X-Forwarded-Port $server_port: 원본 포트 번호 전달

CORS(Cross-Origin Resource Sharing) 설정

  • Access-Control-Allow-Origin: https://web.skyer9.pe.kr 에서만 API 접근 허용
  • Access-Control-Allow-Methods: 허용할 HTTP 메서드 지정
  • Access-Control-Allow-Headers: 허용할 요청 헤더 지정
  • Access-Control-Allow-Credentials: 인증 정보 포함 요청 허용

OPTIONS 요청 처리 (Preflight)

  • if ($request_method = ‘OPTIONS’): CORS preflight 요청 처리
  • Access-Control-Max-Age 1728000: Preflight 결과를 20일간 캐시
  • return 204: 빈 응답으로 OPTIONS 요청 완료

성능 및 타임아웃 설정

  • proxy_connect_timeout 30s: 백엔드 서버 연결 대기 시간
  • proxy_send_timeout 30s: 백엔드로 데이터 전송 제한 시간
  • proxy_read_timeout 30s: 백엔드로부터 응답 대기 시간
  • proxy_buffering on: 응답 버퍼링 활성화 (성능 향상)
  • proxy_buffer_size 4k: 초기 응답 버퍼 크기
  • proxy_buffers 8 4k: 응답 데이터용 버퍼 개수와 크기

로그 설정

  • access_log: 접근 로그 파일 경로
  • error_log: 에러 로그 파일 경로

답글 남기기