# AI 전문가 양성 과정 - 개발 환경용 Nginx 설정

# 업스트림 서버 정의
upstream jupyter {
    server ai-course-dev:8888;
}

upstream fastapi {
    server ai-course-dev:8000;
}

upstream mlflow {
    server mlflow:5000;
}

upstream kibana {
    server kibana:5601;
}

# HTTP 서버 설정
server {
    listen 80;
    server_name localhost;
    
    # 로그 설정
    access_log /var/log/nginx/ai_course_access.log;
    error_log /var/log/nginx/ai_course_error.log;
    
    # 클라이언트 업로드 크기 제한 (대용량 데이터셋 업로드 허용)
    client_max_body_size 1G;
    
    # 타임아웃 설정
    proxy_connect_timeout 60s;
    proxy_send_timeout 60s;
    proxy_read_timeout 60s;
    
    # 기본 라우팅 (메인 페이지)
    location / {
        return 301 /jupyter/;
    }
    
    # Jupyter Lab 프록시
    location /jupyter/ {
        proxy_pass http://jupyter/;
        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;
        
        # WebSocket 지원 (Jupyter Lab)
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_set_header Origin "";
        
        # 리다이렉트 처리
        proxy_redirect off;
    }
    
    # FastAPI/Flask 애플리케이션 프록시
    location /api/ {
        proxy_pass http://fastapi/;
        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;
        
        # CORS 헤더 추가
        add_header Access-Control-Allow-Origin *;
        add_header Access-Control-Allow-Methods "GET, POST, PUT, DELETE, OPTIONS";
        add_header Access-Control-Allow-Headers "DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range";
        
        # OPTIONS 요청 처리
        if ($request_method = 'OPTIONS') {
            add_header Access-Control-Allow-Origin *;
            add_header Access-Control-Allow-Methods "GET, POST, PUT, DELETE, OPTIONS";
            add_header Access-Control-Allow-Headers "DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range";
            add_header Access-Control-Max-Age 1728000;
            add_header Content-Type 'text/plain; charset=utf-8';
            add_header Content-Length 0;
            return 204;
        }
    }
    
    # MLflow UI 프록시
    location /mlflow/ {
        proxy_pass http://mlflow/;
        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;
    }
    
    # Kibana 대시보드 프록시
    location /kibana/ {
        proxy_pass http://kibana/;
        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;
    }
    
    # 정적 파일 서빙 (데이터셋, 모델 등)
    location /static/ {
        alias /app/static/;
        expires 1d;
        add_header Cache-Control "public, immutable";
    }
    
    # 데이터셋 다운로드
    location /datasets/ {
        alias /app/datasets/;
        autoindex on;
        autoindex_exact_size off;
        autoindex_localtime on;
    }
    
    # 모델 파일 서빙
    location /models/ {
        alias /app/models/;
        expires 1h;
        add_header Cache-Control "public";
    }
    
    # 헬스 체크 엔드포인트
    location /health {
        access_log off;
        return 200 "healthy\n";
        add_header Content-Type text/plain;
    }
    
    # 에러 페이지 커스터마이징
    error_page 404 /404.html;
    error_page 500 502 503 504 /50x.html;
    
    location = /404.html {
        root /usr/share/nginx/html;
    }
    
    location = /50x.html {
        root /usr/share/nginx/html;
    }
    
    # 보안 헤더 추가
    add_header X-Frame-Options DENY;
    add_header X-Content-Type-Options nosniff;
    add_header X-XSS-Protection "1; mode=block";
    add_header Referrer-Policy "strict-origin-when-cross-origin";
}

# 웹소켓 연결을 위한 추가 설정
map $http_upgrade $connection_upgrade {
    default upgrade;
    '' close;
} 