
events {
    worker_connections 1024;
}

http {
    include       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"';

    access_log logs/access.log main;
    error_log logs/error.log;

    # Basic settings
    sendfile        on;
    tcp_nopush      on;
    tcp_nodelay     on;
    keepalive_timeout 65;
    types_hash_max_size 2048;

    # Gzip compression
    gzip on;
    gzip_vary on;
    gzip_min_length 1024;
    gzip_types
        text/plain
        text/css
        text/xml
        text/javascript
        application/json
        application/javascript
        application/xml+rss
        application/atom+xml;

    # Rate limiting (optional)
    limit_req_zone $binary_remote_addr zone=api:10m rate=10r/s;
    limit_req_zone $binary_remote_addr zone=login:10m rate=5r/m;

    # Upstream server (your Node.js API)
    upstream market_data_api {
        server localhost:3001;
        keepalive 32;
    }

    # HTTP server block for port 8080 (testing) and port 80 (production)
    server {
        listen       8080;
        listen       80;
        server_name  localhost market-price.insightbull.io 127.0.0.1;

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

        # Proxy settings for API calls
        location /api/ {
            # Apply rate limiting
            limit_req zone=api burst=20 nodelay;

            # Proxy to Node.js server
            proxy_pass http://market_data_api;
            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_cache_bypass $http_upgrade;

            # Timeout settings
            proxy_connect_timeout 60s;
            proxy_send_timeout 60s;
            proxy_read_timeout 60s;

            # Buffer settings for large payloads
            proxy_buffering on;
            proxy_buffer_size 4k;
            proxy_buffers 8 4k;
            proxy_busy_buffers_size 8k;
            client_max_body_size 50M;
        }

        # Health check endpoint (optional)
        location /health {
            proxy_pass http://market_data_api/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;
        }

        # Root location - serve a simple status page or redirect
        location / {
            return 200 "Nginx is running. API endpoints available at /api/\n";
            add_header Content-Type text/plain;
        }

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

    # Optional HTTPS server block (for future use)
    # server {
    #     listen       443 ssl http2;
    #     server_name  localhost;
    #
    #     # SSL configuration would go here
    #
    #     location /api/ {
    #         proxy_pass http://market_data_api/;
    #         # ... same proxy settings as above
    #     }
    # }
}
