# =============================================================================
# Genex Portal - gogenex.com / www.gogenex.com
# 官网门户 (Next.js SSG)
# 部署端口: 3001 (Docker 容器内部 3000)
# =============================================================================

# HTTP → HTTPS 重定向
server {
    listen 80;
    listen [::]:80;
    server_name gogenex.com www.gogenex.com;

    # Let's Encrypt ACME 验证
    location /.well-known/acme-challenge/ {
        root /var/www/certbot;
    }

    location / {
        return 301 https://$host$request_uri;
    }
}

# www → 裸域重定向
server {
    listen 443 ssl http2;
    listen [::]:443 ssl http2;
    server_name www.gogenex.com;

    ssl_certificate     /etc/letsencrypt/live/gogenex.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/gogenex.com/privkey.pem;
    ssl_protocols       TLSv1.2 TLSv1.3;

    return 301 https://gogenex.com$request_uri;
}

# 主站 HTTPS
server {
    listen 443 ssl http2;
    listen [::]:443 ssl http2;
    server_name gogenex.com;

    # ---- SSL ----
    ssl_certificate     /etc/letsencrypt/live/gogenex.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/gogenex.com/privkey.pem;
    ssl_protocols       TLSv1.2 TLSv1.3;
    ssl_ciphers         ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384;
    ssl_prefer_server_ciphers off;

    # ---- 安全头 ----
    add_header Strict-Transport-Security "max-age=63072000" always;
    add_header X-Frame-Options "SAMEORIGIN" always;
    add_header X-Content-Type-Options "nosniff" always;
    add_header Referrer-Policy "strict-origin-when-cross-origin" always;

    # ---- Gzip ----
    gzip on;
    gzip_vary on;
    gzip_proxied any;
    gzip_comp_level 6;
    gzip_types text/plain text/css text/xml application/json application/javascript application/xml+rss image/svg+xml;

    # ---- 日志 ----
    access_log /var/log/nginx/gogenex.com.access.log;
    error_log  /var/log/nginx/gogenex.com.error.log;

    # ---- Next.js 静态资源 (长期缓存) ----
    location /_next/static/ {
        proxy_pass http://127.0.0.1:4080;
        proxy_cache_valid 200 365d;
        add_header Cache-Control "public, max-age=31536000, immutable";
    }

    # ---- 反向代理到 Docker ----
    location / {
        proxy_pass http://127.0.0.1:4080;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        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_connect_timeout 60s;
        proxy_send_timeout 60s;
        proxy_read_timeout 60s;
    }

    # ---- 健康检查 ----
    location = /nginx-health {
        access_log off;
        return 200 '{"status":"ok","service":"genex-portal-nginx"}';
        add_header Content-Type application/json;
    }
}
