# SZR Knowledge Hub - Nginx Configuration
# Add this to your szr.glocalis.com server block or use as a separate server

# Upstream definitions
upstream qingfou {
    server 127.0.0.1:8080;  # Existing Qingfou portal
}

upstream szr_hub {
    server 127.0.0.1:8001;  # Knowledge Hub API
}

server {
    listen 443 ssl http2;
    server_name szr.glocalis.com;

    # SSL certificates (update paths as needed)
    ssl_certificate /etc/nginx/ssl/szr.glocalis.com/fullchain.pem;
    ssl_certificate_key /etc/nginx/ssl/szr.glocalis.com/privkey.pem;

    # SSL settings
    ssl_protocols TLSv1.2 TLSv1.3;
    ssl_ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256;
    ssl_prefer_server_ciphers off;
    ssl_session_timeout 1d;
    ssl_session_cache shared:SSL:50m;

    # Security headers
    add_header X-Frame-Options SAMEORIGIN;
    add_header X-Content-Type-Options nosniff;
    add_header X-XSS-Protection "1; mode=block";

    # Logging
    access_log /var/log/nginx/szr.glocalis.com.access.log;
    error_log /var/log/nginx/szr.glocalis.com.error.log;

    # ===========================================
    # Knowledge Hub Routes (/hub/)
    # ===========================================

    # Knowledge Hub - Static files and frontend
    location /hub {
        proxy_pass http://szr_hub;
        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;

        # Rewrite for clean URLs
        rewrite ^/hub$ /hub/ permanent;
        rewrite ^/hub/(.*)$ /$1 break;
    }

    # Knowledge Hub - API routes
    location /hub/api {
        proxy_pass http://szr_hub/api;
        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;

        # For SSE streaming
        proxy_buffering off;
        proxy_cache off;
        proxy_read_timeout 300s;
    }

    # ===========================================
    # Qingfou Portal Routes (default)
    # ===========================================

    location / {
        proxy_pass http://qingfou;
        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;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";

        # WebSocket support for live features
        proxy_read_timeout 86400;
    }
}

# HTTP to HTTPS redirect
server {
    listen 80;
    server_name szr.glocalis.com;
    return 301 https://$server_name$request_uri;
}
