From 6902daaea153712f54fed0da2d94ba4be00c9b2b Mon Sep 17 00:00:00 2001 From: Josh Knapp Date: Fri, 26 Dec 2025 13:02:04 -0800 Subject: [PATCH] Add automatic SSE detection and support to backend template Changes: - Detect SSE via Accept header (text/event-stream) or ?action=stream parameter - Disable http-server-close to allow long-lived SSE connections - Enable http-no-delay for immediate event delivery - Set 1-hour timeouts for SSE support (also fine for normal requests) - Force Connection: keep-alive for detected SSE requests Benefits: - SSE now works automatically without special backend configuration - Fixes transcription server display disconnection issues - Normal HTTP requests still work perfectly - No need for separate SSE-specific backends Fixes: Server-Sent Events timing out through HAProxy --- templates/hap_backend.tpl | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/templates/hap_backend.tpl b/templates/hap_backend.tpl index 3b38212..04d0019 100644 --- a/templates/hap_backend.tpl +++ b/templates/hap_backend.tpl @@ -1,5 +1,25 @@ backend {{ name }}-backend + # Detect Server-Sent Events (SSE) connections + # SSE uses Accept: text/event-stream or ?action=stream query parameter + acl is_sse hdr(accept) -i -m sub text/event-stream + acl is_sse_url urlp(action) -i -m str stream + + # Disable http-server-close from defaults to allow SSE long-lived connections + # Normal HTTP requests still work fine without this option + no option http-server-close + + # Enable http-no-delay for immediate data transmission (good for SSE and general performance) + option http-no-delay + + # Extended timeouts to support SSE long-lived connections + # These values also work fine for normal HTTP requests + timeout server 1h + timeout http-keep-alive 1h + + # Ensure keep-alive connection for SSE requests + http-response set-header Connection keep-alive if is_sse or is_sse_url + option forwardfor # Pass the real client IP to backend (from proxy headers or direct connection) # This is crucial for container-level logging and security tools @@ -7,9 +27,8 @@ backend {{ name }}-backend http-request set-header X-Real-IP %[var(txn.real_ip)] http-request set-header X-Forwarded-For %[var(txn.real_ip)] {% if ssl_enabled %}http-request set-header X-Forwarded-Proto https if { ssl_fc }{% endif %} - - + + {% for server in servers %} server {{ server.server_name }} {{ server.server_address }}:{{ server.server_port }} {{ server.server_options }} {% endfor %} - \ No newline at end of file