# Production Nginx routing for XiaoXia SaaS.
#
# Final domain roles:
# - saas.xiaoxiajianji.com: SaaS web UI
# - saas-api.xiaoxiajianji.com: SaaS API, docs, generated files
# - api.xiaoxiajianji.com: legacy-compatible SaaS API entry
# - git.xiaoxiajianji.com: Gitea code repository
# - xiaoxiajianji.com / www.xiaoxiajianji.com: brand/root entry, redirect to SaaS web

server {
    listen 443 ssl http2;
    server_name xiaoxiajianji.com www.xiaoxiajianji.com;

    ssl_certificate /etc/letsencrypt/live/xiaoxiajianji.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/xiaoxiajianji.com/privkey.pem;
    include /etc/letsencrypt/options-ssl-nginx.conf;
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;

    return 301 https://saas.xiaoxiajianji.com$request_uri;
}

server {
    listen 443 ssl http2;
    server_name saas.xiaoxiajianji.com;

    ssl_certificate /etc/letsencrypt/live/xiaoxiajianji.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/xiaoxiajianji.com/privkey.pem;
    include /etc/letsencrypt/options-ssl-nginx.conf;
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;

    client_max_body_size 2g;

    location / {
        proxy_pass http://127.0.0.1:3002/;
        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;
    }
}

server {
    listen 443 ssl http2;
    server_name saas-api.xiaoxiajianji.com api.xiaoxiajianji.com;

    ssl_certificate /etc/letsencrypt/live/xiaoxiajianji.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/xiaoxiajianji.com/privkey.pem;
    include /etc/letsencrypt/options-ssl-nginx.conf;
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;

    client_max_body_size 2g;

    location /api/ {
        proxy_pass http://127.0.0.1:8001;
        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;
    }

    location = /health {
        proxy_pass http://127.0.0.1:8001/health;
        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;
    }

    location /docs {
        proxy_pass http://127.0.0.1:8001/docs;
        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;
    }

    location /openapi.json {
        proxy_pass http://127.0.0.1:8001/openapi.json;
        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;
    }

    location /generated-files/ {
        alias /var/lib/xiaoxia-saas-production/generated/;
        types {
            video/mp4 mp4;
        }
        default_type application/octet-stream;
        add_header Cache-Control "public, max-age=3600";
    }

    location / {
        return 404;
    }
}

server {
    listen 443 ssl http2;
    server_name git.xiaoxiajianji.com;

    ssl_certificate /etc/letsencrypt/live/xiaoxiajianji.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/xiaoxiajianji.com/privkey.pem;
    include /etc/letsencrypt/options-ssl-nginx.conf;
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;

    client_max_body_size 2g;

    location / {
        proxy_pass http://127.0.0.1:3000/;
        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;
        proxy_read_timeout 300s;
    }
}

server {
    listen 80;
    server_name xiaoxiajianji.com www.xiaoxiajianji.com;
    return 301 https://saas.xiaoxiajianji.com$request_uri;
}

server {
    listen 80;
    server_name saas.xiaoxiajianji.com;
    return 301 https://saas.xiaoxiajianji.com$request_uri;
}

server {
    listen 80;
    server_name saas-api.xiaoxiajianji.com api.xiaoxiajianji.com;
    return 301 https://$host$request_uri;
}

server {
    listen 80;
    server_name git.xiaoxiajianji.com;
    return 301 https://git.xiaoxiajianji.com$request_uri;
}
