我有一个Node.js服务器,它使用服务器发送事件(SSE)来允许向连接的Web客户端发送推送通知 . 当浏览器直接与Node通信时,它可以很好地工作 .

但是,当我将haproxy置于中间时,因为它必须用于 生产环境 以满足其他要求,连接将每30秒关闭并重新打开(由于SSE的自动重新连接) . 我已经改变并尝试了我所知道的一切,并且可以在haproxy配置中在线查找 .

大多数信息和他们的文档示例处理套接字,但SSE支持很少 . 它应该支持SSE的持久HTTP连接吗?如果是这样,配置它的诀窍是什么?

我的配置如下:全局守护进程#最大并发连接数maxconn 4096端口绑定后的drop权限用户nobody组nogroup#存储pid文件中的进程pid文件/var/run/haproxy.pid #stats stats stats socket / socket无功/运行/插座HAProxy的

defaults
    log global
    mode http

    # disable logging of null connections
    option dontlognull

    # I've tried all these to no avail
    #option http-server-close
    #option httpclose
    option http-keep-alive

    # Add x-forwarded-for header to forward clients IP to app
    option forwardfor

    # maximum time to wait for a server connection to succeed. Can be as low as few msec if Haproxy and server are on same LAN.
    timeout connect 1s
    # maximum inactivity time on client side. Recommended to keep it same as server timeout.
    timeout client 24d
    # maximum time given to server to respond to a request
    timeout server 24d
    # Long timeout for WebSocket connections.
    timeout tunnel 8h

    # timeout for keep alive
    timeout http-keep-alive 60s
    # maximum time to wait for client to send full request. Keep it like 5s for get DoS protection.
    timeout http-request 5s

    # enable stats web interface. very helpful to see what's happening in haproxy
    stats enable
    # default refresh time for web interface
    stats refresh 30s

# this frontend interface receives the incoming http requests and forwards to https then handles all SSL requests
frontend public
    # HTTP
    bind :80

    # by default, all incoming requests are sent to Node.js
    default_backend node_backend

    # redirect to the SSE backend if /ionmed/events (eventum #????)
    acl req_sse_path path_beg /ionmed/events
    use_backend node_sse_backend if req_sse_path

    # redirect to the tomcat backend if Time Clock, ViewerJS, Spell Checker, Tomcat Manager, or eScripts (eventum #1039, #1082)
    acl req_timeclock_path path_beg /TimeClock/
    acl req_manager_path path_beg /manager/
    acl req_spelling_path path_beg /jspellEvolution/
    acl req_escripts_path path_beg /ionmed/escripts
    use_backend tomcat_backend if req_timeclock_path or req_manager_path or req_spelling_path or req_escripts_path

    # for displaying HAProxy statistics
    acl req_stats path_beg /stats
    use_backend stats if req_stats

# node backend, transfer to port 8081
backend node_backend
    # Tell the backend that this is a secure connection,
    # even though it's getting plain HTTP.
    reqadd X-Forwarded-Proto:\ https

    server node_server localhost:8081

# node SSE backend, transfer to port 8082
backend node_sse_backend
    # Tell the backend that this is a secure connection,
    # even though it's getting plain HTTP.
    reqadd X-Forwarded-Proto:\ https

    server node_sse_server localhost:8082

# tomcat backend, transfer to port 8888
backend tomcat_backend
    # Tell the backend that this is a secure connection,
    # even though it's getting plain HTTP.
    reqadd X-Forwarded-Proto:\ https

    server tomcat_server localhost:8888