# Synor Storage Gateway - nginx configuration
# Load balances requests across storage nodes

worker_processes auto;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;

events {
    worker_connections 1024;
    use epoll;
    multi_accept on;
}

http {
    include /etc/nginx/mime.types;
    default_type application/octet-stream;

    # Logging
    log_format main '$remote_addr - $remote_user [$time_local] "$request" '
                    '$status $body_bytes_sent "$http_referer" '
                    '"$http_user_agent" "$http_x_forwarded_for" '
                    'rt=$request_time uct="$upstream_connect_time" '
                    'uht="$upstream_header_time" urt="$upstream_response_time"';

    access_log /var/log/nginx/access.log main;

    # Performance
    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 65;
    types_hash_max_size 2048;

    # Gzip compression
    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 application/xml+rss text/javascript application/wasm;

    # Rate limiting
    limit_req_zone $binary_remote_addr zone=gateway_limit:10m rate=10r/s;
    limit_conn_zone $binary_remote_addr zone=conn_limit:10m;

    # Storage node upstream (round-robin with health checks)
    upstream storage_nodes {
        least_conn;

        server storage-node-1:8080 weight=1 max_fails=3 fail_timeout=30s;
        server storage-node-2:8080 weight=1 max_fails=3 fail_timeout=30s;
        server storage-node-3:8080 weight=1 max_fails=3 fail_timeout=30s;

        keepalive 32;
    }

    # Cache configuration
    proxy_cache_path /var/cache/nginx/storage levels=1:2
                     keys_zone=storage_cache:100m max_size=10g
                     inactive=60m use_temp_path=off;

    server {
        listen 80;
        server_name _;

        # Security headers
        add_header X-Content-Type-Options "nosniff" always;
        add_header X-Frame-Options "DENY" always;
        add_header X-XSS-Protection "1; mode=block" always;
        add_header Referrer-Policy "strict-origin-when-cross-origin" always;

        # CORS headers
        add_header Access-Control-Allow-Origin "*" always;
        add_header Access-Control-Allow-Methods "GET, HEAD, OPTIONS" always;
        add_header Access-Control-Allow-Headers "Content-Type, Authorization" always;

        # Handle preflight requests
        if ($request_method = 'OPTIONS') {
            add_header Access-Control-Allow-Origin "*";
            add_header Access-Control-Allow-Methods "GET, HEAD, OPTIONS";
            add_header Access-Control-Allow-Headers "Content-Type, Authorization";
            add_header Access-Control-Max-Age 86400;
            add_header Content-Length 0;
            add_header Content-Type text/plain;
            return 204;
        }

        # Health check endpoint
        location /health {
            access_log off;
            return 200 'OK';
            add_header Content-Type text/plain;
        }

        # Metrics endpoint (internal only)
        location /metrics {
            allow 172.21.0.0/16;
            deny all;
            stub_status;
        }

        # Gateway info
        location /info {
            default_type application/json;
            return 200 '{"gateway":"synor-storage","version":"0.1.0","nodes":3}';
        }

        # Content requests by CID (synor1...)
        location ~ ^/synor1[A-Za-z0-9]+$ {
            limit_req zone=gateway_limit burst=20 nodelay;
            limit_conn conn_limit 10;

            proxy_pass http://storage_nodes;
            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 Connection "";

            # Caching
            proxy_cache storage_cache;
            proxy_cache_valid 200 1h;
            proxy_cache_valid 404 1m;
            proxy_cache_use_stale error timeout updating http_500 http_502 http_503 http_504;
            proxy_cache_lock on;
            add_header X-Cache-Status $upstream_cache_status;

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

            # Buffer settings
            proxy_buffering on;
            proxy_buffer_size 4k;
            proxy_buffers 8 16k;
            proxy_busy_buffers_size 32k;

            # Max content size (100MB)
            client_max_body_size 100m;
        }

        # Content with subpath (synor1.../path/to/file)
        location ~ ^/synor1[A-Za-z0-9]+/.+ {
            limit_req zone=gateway_limit burst=20 nodelay;
            limit_conn conn_limit 10;

            proxy_pass http://storage_nodes;
            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 Connection "";

            proxy_cache storage_cache;
            proxy_cache_valid 200 1h;
            add_header X-Cache-Status $upstream_cache_status;

            proxy_connect_timeout 10s;
            proxy_send_timeout 60s;
            proxy_read_timeout 60s;
        }

        # API endpoints (pass through to nodes)
        location /api/ {
            limit_req zone=gateway_limit burst=10 nodelay;

            proxy_pass http://storage_nodes;
            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 Connection "";

            proxy_connect_timeout 5s;
            proxy_send_timeout 30s;
            proxy_read_timeout 30s;
        }

        # Default - 404
        location / {
            return 404 '{"error":"Not found","hint":"Use /synor1<cid> to fetch content"}';
            add_header Content-Type application/json;
        }

        # Error pages
        error_page 500 502 503 504 /50x.html;
        location = /50x.html {
            root /usr/share/nginx/html;
            internal;
        }
    }
}
