# Alternative to Caddy: nginx in front of the Workshare server on the same host (server bound to 127.0.0.1:8080).
# Certificates via certbot:  sudo certbot --nginx -d workshare.globalinfocloud.in
# Server environment for this layout: WORKSHARE_BIND=127.0.0.1 WORKSHARE_TRUST_PROXY=1 WORKSHARE_PROXY_HOPS=1

upstream workshare {
    server 127.0.0.1:8080;
    keepalive 16;
}

server {
    listen 80;
    server_name workshare.globalinfocloud.in;
    return 301 https://$host$request_uri;
}

server {
    listen 443 ssl;
    http2 on;
    server_name workshare.globalinfocloud.in;

    ssl_certificate     /etc/letsencrypt/live/workshare.globalinfocloud.in/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/workshare.globalinfocloud.in/privkey.pem;
    ssl_protocols TLSv1.2 TLSv1.3;

    add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;
    server_tokens off;

    client_max_body_size 48m;
    client_body_buffer_size 1m;
    proxy_read_timeout 300s;
    proxy_send_timeout 300s;

    # Push updates (Server-Sent Events): long-lived, must not be buffered. The server pings every 25 s.
    location ~ ^/models/[^/]+/events$ {
        proxy_pass http://workshare;
        proxy_http_version 1.1;
        proxy_set_header Connection "";
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-For $remote_addr;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Request-Id $request_id;
        proxy_buffering off;
        proxy_cache off;
        proxy_read_timeout 1h;
    }

    location / {
        proxy_pass http://workshare;
        proxy_http_version 1.1;
        proxy_set_header Connection "";
        proxy_set_header Host $host;
        # Overwrite, never append, what the client sent: one trusted hop.
        proxy_set_header X-Forwarded-For $remote_addr;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Request-Id $request_id;
        proxy_request_buffering on;   # nginx takes the slow upload; Node gets it in one burst
    }
}
