# AeThex Nginx Configuration
# Place this file at: /etc/nginx/sites-available/aethex-domains
# Then symlink: ln -s /etc/nginx/sites-available/aethex-domains /etc/nginx/sites-enabled/

# Upstream backend server
upstream aethex_backend {
    server localhost:5000;
    keepalive 64;
}

# Rate limiting zones
limit_req_zone $binary_remote_addr zone=api_limit:10m rate=10r/s;
limit_req_zone $binary_remote_addr zone=auth_limit:10m rate=5r/s;
limit_req_zone $binary_remote_addr zone=general:10m rate=20r/s;

# ===================================================================
# WEB APPLICATION DOMAINS (React SPA)
# ===================================================================

server {
    listen 80;
    listen [::]:80;
    server_name aethex.app aethex.co aethex.online aethex.site
                aethex.education aethex.studio aethex.shop aethex.support
                aethex.dev aethex.info aethex.blog aethex.fun aethex.space
                aethex.bio aethex.me aethex.biz aethex.pro aethex.foundation
                aethex.us aethex.sbs aethex.live;

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

server {
    listen 443 ssl http2;
    listen [::]:443 ssl http2;
    server_name aethex.app aethex.co aethex.online aethex.site
                aethex.education aethex.studio aethex.shop aethex.support
                aethex.dev aethex.info aethex.blog aethex.fun aethex.space
                aethex.bio aethex.me aethex.biz aethex.pro aethex.foundation
                aethex.us aethex.sbs aethex.live;

    # SSL Configuration
    ssl_certificate /etc/letsencrypt/live/aethex.app/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/aethex.app/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;
    ssl_session_cache shared:SSL:10m;
    ssl_session_timeout 10m;
    ssl_stapling on;
    ssl_stapling_verify on;

    # Security Headers
    add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;
    add_header X-Frame-Options "SAMEORIGIN" always;
    add_header X-Content-Type-Options "nosniff" always;
    add_header X-XSS-Protection "1; mode=block" always;
    add_header Referrer-Policy "no-referrer-when-downgrade" always;

    # Document root
    root /var/www/aethex/dist/public;
    index index.html;

    # Rate limiting
    limit_req zone=general burst=50 nodelay;

    # Compression
    gzip on;
    gzip_vary on;
    gzip_min_length 1024;
    gzip_types text/plain text/css text/xml text/javascript application/javascript application/x-javascript application/xml+rss application/json;

    # Logging
    access_log /var/log/nginx/aethex-web-access.log;
    error_log /var/log/nginx/aethex-web-error.log;

    # API proxy to backend
    location /api/ {
        proxy_pass http://aethex_backend;
        proxy_http_version 1.1;

        # Headers
        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_set_header X-Forwarded-Host $host;

        # Timeouts
        proxy_connect_timeout 60s;
        proxy_send_timeout 60s;
        proxy_read_timeout 60s;

        proxy_cache_bypass $http_upgrade;
    }

    # Auth endpoints
    location /auth/ {
        limit_req zone=auth_limit burst=10 nodelay;

        proxy_pass http://aethex_backend;
        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;
    }

    # WebSocket support
    location /socket.io/ {
        proxy_pass http://aethex_backend;
        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;

        # WebSocket timeouts
        proxy_connect_timeout 7d;
        proxy_send_timeout 7d;
        proxy_read_timeout 7d;
    }

    # Health check
    location /health {
        proxy_pass http://aethex_backend;
        access_log off;
    }

    # Static assets caching
    location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg|woff|woff2|ttf|eot|webp|avif)$ {
        expires 1y;
        add_header Cache-Control "public, immutable";
        access_log off;
    }

    # SPA routing - serve index.html for all routes
    location / {
        try_files $uri $uri/ /index.html;
    }
}

# ===================================================================
# API & NETWORK SERVICES
# ===================================================================

server {
    listen 80;
    listen [::]:80;
    server_name aethex.network aethex.net api.aethex.cloud;
    return 301 https://$host$request_uri;
}

server {
    listen 443 ssl http2;
    listen [::]:443 ssl http2;
    server_name aethex.network aethex.net api.aethex.cloud;

    # SSL Configuration
    ssl_certificate /etc/letsencrypt/live/aethex.network/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/aethex.network/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';

    # Security Headers
    add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;
    add_header X-Content-Type-Options "nosniff" always;

    # Logging
    access_log /var/log/nginx/aethex-api-access.log;
    error_log /var/log/nginx/aethex-api-error.log;

    # Rate limiting for API
    limit_req zone=api_limit burst=20 nodelay;
    limit_req_status 429;

    # All requests go to backend
    location / {
        proxy_pass http://aethex_backend;
        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;

        # API timeouts
        proxy_connect_timeout 30s;
        proxy_send_timeout 30s;
        proxy_read_timeout 30s;
    }

    # WebSocket
    location /socket.io/ {
        proxy_pass http://aethex_backend;
        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;
    }
}

# ===================================================================
# AUTHENTICATION SERVICES
# ===================================================================

server {
    listen 80;
    listen [::]:80;
    server_name aethex.tech aethex.id;
    return 301 https://$host$request_uri;
}

server {
    listen 443 ssl http2;
    listen [::]:443 ssl http2;
    server_name aethex.tech aethex.id;

    # SSL Configuration
    ssl_certificate /etc/letsencrypt/live/aethex.tech/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/aethex.tech/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';

    # Security Headers
    add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;
    add_header X-Frame-Options "DENY" always;
    add_header X-Content-Type-Options "nosniff" always;

    # Logging
    access_log /var/log/nginx/aethex-auth-access.log;
    error_log /var/log/nginx/aethex-auth-error.log;

    # Rate limiting for auth endpoints
    limit_req zone=auth_limit burst=10 nodelay;

    location / {
        proxy_pass http://aethex_backend;
        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;

        # Auth timeouts
        proxy_connect_timeout 60s;
        proxy_send_timeout 60s;
        proxy_read_timeout 60s;
    }
}

# ===================================================================
# CLOUD SERVICES & KERNEL
# ===================================================================

server {
    listen 80;
    listen [::]:80;
    server_name aethex.cloud;
    return 301 https://$host$request_uri;
}

server {
    listen 443 ssl http2;
    listen [::]:443 ssl http2;
    server_name aethex.cloud;

    # SSL Configuration
    ssl_certificate /etc/letsencrypt/live/aethex.cloud/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/aethex.cloud/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';

    # Logging
    access_log /var/log/nginx/aethex-cloud-access.log;
    error_log /var/log/nginx/aethex-cloud-error.log;

    limit_req zone=api_limit burst=20 nodelay;

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

# ===================================================================
# BOT SERVICES
# ===================================================================

server {
    listen 80;
    listen [::]:80;
    server_name aethex.bot;
    return 301 https://$host$request_uri;
}

server {
    listen 443 ssl http2;
    listen [::]:443 ssl http2;
    server_name aethex.bot;

    # SSL Configuration
    ssl_certificate /etc/letsencrypt/live/aethex.bot/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/aethex.bot/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';

    # Logging
    access_log /var/log/nginx/aethex-bot-access.log;
    error_log /var/log/nginx/aethex-bot-error.log;

    limit_req zone=api_limit burst=30 nodelay;

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

# ===================================================================
# STORAGE SERVICES (aethex.locker)
# ===================================================================

server {
    listen 80;
    listen [::]:80;
    server_name aethex.locker;
    return 301 https://$host$request_uri;
}

server {
    listen 443 ssl http2;
    listen [::]:443 ssl http2;
    server_name aethex.locker;

    # SSL Configuration
    ssl_certificate /etc/letsencrypt/live/aethex.locker/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/aethex.locker/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';

    # Logging
    access_log /var/log/nginx/aethex-locker-access.log;
    error_log /var/log/nginx/aethex-locker-error.log;

    # Allow large file uploads
    client_max_body_size 500M;
    client_body_timeout 300s;

    limit_req zone=general burst=20 nodelay;

    location / {
        proxy_pass http://aethex_backend;
        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;

        # Extended timeouts for file uploads
        proxy_connect_timeout 300s;
        proxy_send_timeout 300s;
        proxy_read_timeout 300s;
    }
}
