# Reference template — the installer generates the actual Nginx config.
# If configuring manually, copy this to /etc/nginx/sites-available/servermon
# then: ln -s /etc/nginx/sites-available/servermon /etc/nginx/sites-enabled/
# Replace YOUR_DOMAIN and reload: sudo nginx -t && sudo systemctl reload nginx

server {
    listen 80;
    server_name YOUR_DOMAIN;
    client_max_body_size 2m;

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

    # Health check — fast timeout, no access log noise
    location /api/health {
        proxy_pass http://127.0.0.1:8912;
        proxy_read_timeout 5s;
        access_log off;
    }

    # SSE stream — buffering MUST be off or events will stall (causes 502-like symptoms)
    location /api/metrics/stream {
        proxy_pass http://127.0.0.1:8912;
        proxy_http_version 1.1;
        proxy_set_header Connection "";
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_buffering off;
        proxy_cache off;
        proxy_read_timeout 86400s;
        proxy_send_timeout 86400s;
        chunked_transfer_encoding off;
    }

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

    access_log /var/log/nginx/servermon_access.log;
    error_log /var/log/nginx/servermon_error.log;
}

# After running certbot, an HTTPS block will be auto-generated.
# To set up SSL: sudo certbot --nginx -d YOUR_DOMAIN
