# Example reverse-proxy config terminating HTTPS in front of the `api`
# container (docker-compose service on 127.0.0.1:8000).
#
# Install certbot (Let's Encrypt) or bring your own cert/key, then:
#   sudo cp nginx.conf.example /etc/nginx/sites-available/bulkmailer
#   sudo ln -s /etc/nginx/sites-available/bulkmailer /etc/nginx/sites-enabled/
#   sudo nginx -t && sudo systemctl reload nginx
#
# Replace `your-domain.example.com` and the cert paths below.

server {
    listen 80;
    server_name your-domain.example.com;
    # Redirect all HTTP to HTTPS
    location / {
        return 301 https://$host$request_uri;
    }
    # Let's Encrypt HTTP-01 challenge path (certbot webroot mode)
    location /.well-known/acme-challenge/ {
        root /var/www/certbot;
    }
}

server {
    listen 443 ssl http2;
    server_name your-domain.example.com;

    ssl_certificate     /etc/letsencrypt/live/your-domain.example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/your-domain.example.com/privkey.pem;
    ssl_protocols       TLSv1.2 TLSv1.3;
    ssl_ciphers         HIGH:!aNULL:!MD5;
    ssl_prefer_server_ciphers on;

    # Reasonable upload size for CSV/attachment uploads (matches MAX_UPLOAD_MB
    # in .env; raise both together if you increase the app-side limit).
    client_max_body_size 15m;

    # SSE (/campaigns/{id}/stream) needs long-lived connections with
    # buffering disabled so progress events flush immediately.
    location /campaigns/ {
        proxy_pass http://127.0.0.1:8000;
        proxy_http_version 1.1;
        proxy_set_header Connection "";
        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_buffering off;
        proxy_cache off;
        proxy_read_timeout 3600s;
        chunked_transfer_encoding on;
    }

    location / {
        proxy_pass http://127.0.0.1:8000;
        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_read_timeout 120s;
    }

    add_header Strict-Transport-Security "max-age=63072000; includeSubDomains" always;
}
