# nginx configuration template for ANET BBS
# Replace DOMAIN_NAME and adjust paths as needed.
# To use: cp anetbbs-nginx.conf.template /etc/nginx/sites-available/anetbbs
#         sed -i 's/DOMAIN_NAME/yourdomain.com/g' /etc/nginx/sites-available/anetbbs
#         ln -s /etc/nginx/sites-available/anetbbs /etc/nginx/sites-enabled/
#         nginx -t && systemctl reload nginx

server {
    listen 80;
    server_name DOMAIN_NAME;

    # Redirect all HTTP to HTTPS
    return 301 https://$host$request_uri;
}

server {
    listen 443 ssl http2;
    server_name DOMAIN_NAME;

    # SSL — managed by certbot (run: certbot --nginx -d DOMAIN_NAME)
    ssl_certificate     /etc/letsencrypt/live/DOMAIN_NAME/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/DOMAIN_NAME/privkey.pem;
    include             /etc/letsencrypt/options-ssl-nginx.conf;
    ssl_dhparam         /etc/letsencrypt/ssl-dhparams.pem;

    # 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;

    # Accept uploads up to the BBS's UPLOAD_MAX_SIZE (100MB) plus headroom.
    # Default nginx limit (1m) 413s avatar uploads and file-base posts.
    client_max_body_size 110m;

    # Main Flask app
    location / {
        proxy_pass http://127.0.0.1:5000;
        proxy_read_timeout 120s;
    }

    # Socket.IO WebSocket upgrade
    location /socket.io/ {
        proxy_pass         http://127.0.0.1:5000/socket.io/;
        proxy_http_version 1.1;
        proxy_set_header   Upgrade    $http_upgrade;
        proxy_set_header   Connection "upgrade";
        proxy_read_timeout 86400s;
    }

    # MRC Bridge WebSocket — gated by Flask auth_request so unauthenticated
    # browsers can't open the upstream WS even if they know the URL.
    location = /mrc-auth-check {
        internal;
        proxy_pass http://127.0.0.1:5000/mrc/auth-check;
        proxy_pass_request_body off;
        proxy_set_header Content-Length "";
        proxy_set_header X-Original-URI $request_uri;
        proxy_set_header Cookie $http_cookie;
    }

    location /mrcws {
        auth_request /mrc-auth-check;
        proxy_pass         http://127.0.0.1:8080/mrcws;
        proxy_http_version 1.1;
        proxy_set_header   Upgrade    $http_upgrade;
        proxy_set_header   Connection "upgrade";
        proxy_read_timeout 86400s;
    }

    # Static files served directly by nginx for performance
    location /static/ {
        alias /opt/anetbbs/anetbbs/static/;
        expires 7d;
        add_header Cache-Control "public, immutable";
    }
}
