# Nginx alternative to Caddy (bring your own TLS certificate, e.g. certbot --nginx).
# API on 127.0.0.1:4000, web app on 127.0.0.1:3000. Set TRUST_PROXY=1 on the API.
upstream devwebs_api { server 127.0.0.1:4000; keepalive 32; }
upstream devwebs_web { server 127.0.0.1:3000; keepalive 32; }

server {
    listen 80;
    server_name crm.example.com;
    return 301 https://$host$request_uri;
}

server {
    # On nginx 1.25.1+ you may write `listen 443 ssl;` plus `http2 on;` instead.
    listen 443 ssl http2;
    server_name crm.example.com;

    ssl_certificate     /etc/letsencrypt/live/crm.example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/crm.example.com/privkey.pem;
    ssl_protocols TLSv1.2 TLSv1.3;

    client_max_body_size 110m;

    proxy_http_version 1.1;
    proxy_set_header Connection "";
    proxy_set_header Host $host;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto $scheme;
    proxy_set_header X-Request-Id $request_id;

    # Inbox live updates (Server-Sent Events): no buffering, long-lived.
    location = /api/inbox/stream {
        proxy_pass http://devwebs_api;
        proxy_buffering off;
        proxy_cache off;
        proxy_read_timeout 1h;
    }

    location /api/ {
        proxy_pass http://devwebs_api;
        proxy_read_timeout 60s;
    }

    location / {
        proxy_pass http://devwebs_web;
    }
}
