# Nginx configuration for Support Board
# Domain: chat.betfolio.io
# This file should be placed in /etc/nginx/sites-available/chat.betfolio.io
# and symlinked to /etc/nginx/sites-enabled/

server {
    listen 80;
    listen [::]:80;
    server_name chat.betfolio.io;

    # Redirect HTTP to HTTPS (uncomment if you have SSL certificate)
    # return 301 https://$server_name$request_uri;

    # For HTTP only (development/testing)
    # Remove this block if using HTTPS redirect above
    location / {
        proxy_pass http://localhost:8080;
        proxy_http_version 1.1;
        
        # Proxy headers
        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 $server_name;
        
        # CORS headers (if needed)
        add_header Access-Control-Allow-Origin "*" always;
        add_header Access-Control-Allow-Methods "GET, POST, OPTIONS, PUT, DELETE" always;
        add_header Access-Control-Allow-Headers "Content-Type, Authorization, X-Requested-With" always;
        
        # Handle preflight requests
        if ($request_method = 'OPTIONS') {
            add_header Access-Control-Allow-Origin "*";
            add_header Access-Control-Allow-Methods "GET, POST, OPTIONS, PUT, DELETE";
            add_header Access-Control-Allow-Headers "Content-Type, Authorization, X-Requested-With";
            add_header Access-Control-Max-Age 1728000;
            add_header Content-Type "text/plain charset=UTF-8";
            add_header Content-Length 0;
            return 204;
        }
        
        # Timeouts
        proxy_connect_timeout 60s;
        proxy_send_timeout 60s;
        proxy_read_timeout 60s;
        
        # Buffer settings
        proxy_buffering on;
        proxy_buffer_size 4k;
        proxy_buffers 8 4k;
        proxy_busy_buffers_size 8k;
        
        # Disable proxy redirect
        proxy_redirect off;
    }

    # Logging
    access_log /var/log/nginx/chat.betfolio.io.access.log;
    error_log /var/log/nginx/chat.betfolio.io.error.log;
}

# HTTPS configuration (uncomment and configure when SSL certificate is available)
# server {
#     listen 443 ssl http2;
#     listen [::]:443 ssl http2;
#     server_name chat.betfolio.io;
# 
#     # SSL certificate paths (update these with your certificate paths)
#     ssl_certificate /etc/letsencrypt/live/chat.betfolio.io/fullchain.pem;
#     ssl_certificate_key /etc/letsencrypt/live/chat.betfolio.io/privkey.pem;
# 
#     # SSL configuration
#     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;
# 
#     location / {
#         proxy_pass http://localhost:8080;
#         proxy_http_version 1.1;
#         
#         # Proxy headers
#         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 $server_name;
#         
#         # CORS headers
#         add_header Access-Control-Allow-Origin "*" always;
#         add_header Access-Control-Allow-Methods "GET, POST, OPTIONS, PUT, DELETE" always;
#         add_header Access-Control-Allow-Headers "Content-Type, Authorization, X-Requested-With" always;
#         
#         # Handle preflight requests
#         if ($request_method = 'OPTIONS') {
#             add_header Access-Control-Allow-Origin "*";
#             add_header Access-Control-Allow-Methods "GET, POST, OPTIONS, PUT, DELETE";
#             add_header Access-Control-Allow-Headers "Content-Type, Authorization, X-Requested-With";
#             add_header Access-Control-Max-Age 1728000;
#             add_header Content-Type "text/plain charset=UTF-8";
#             add_header Content-Length 0;
#             return 204;
#         }
#         
#         # Timeouts
#         proxy_connect_timeout 60s;
#         proxy_send_timeout 60s;
#         proxy_read_timeout 60s;
#         
#         # Buffer settings
#         proxy_buffering on;
#         proxy_buffer_size 4k;
#         proxy_buffers 8 4k;
#         proxy_busy_buffers_size 8k;
#         
#         # Disable proxy redirect
#         proxy_redirect off;
#     }
# 
#     # Logging
#     access_log /var/log/nginx/chat.betfolio.io.access.log;
#     error_log /var/log/nginx/chat.betfolio.io.error.log;
# }

