2

構成しようとしているubuntuサーバーでhaproxyを使用してsocket.io websocketハンドシェイクを正しくルーティングするのに大きな問題があります。私はこことウェブの両方で多くのことを読みましたが、決定的な解決策を見つけることができないようです. 私が達成しなければならないことはかなり複雑です。同じサーバー上で複数の nodejs インスタンスが実行されており、それぞれが異なるポートで実行されていますが、ブラウザーからポート 80 でそれらすべてに到達できる必要があります (クライアントのファイアウォールの問題)。私がなんとか達成したのは、からhaproxyで書き直すことです

http://HOST/8081/

http://HOST:8081/

バックエンドで、すべてのリクエストが正しい nodejs インスタンスへのルーターになるようにします。haproxyがすべてのリクエストを書き換えるために、オプションとしてhttpcloseを使用する必要があるため、ソケットで問題が発生しましたが、これによりwebsocketが失敗します。検索すると、 /socket.io または /node で始まるリクエストを正しいポートを持つ同じバックエンドにルーティングする回避策が見つかりましたが、オプションは異なります。これはうまくいきましたが、すべての websockets リクエストが同じ nodejs インスタンスにリンクされるようになりました。これは明らかに問題です。すべてのクライアントが 1 つのサーバーに接続するようになったためです。ウェブソケットを適切なサーバーに送信するために acl を作成するためにできる限りのことを試みましたが、それらをルーティングするためのリクエストに何も見つかりません。これが私が達成できた最後の構成です。これにより、websocket 要求が適切なサーバーにルーティングされます。

ws://HOST/socket.io/1/websocket/Wra1vIqoPcTqBNkRLTif' への WebSocket 接続に失敗しました: 予期しない応答コード: 503

global
    log 127.0.0.1 local0 debug
    maxconn 4096
    user haproxy
    group haproxy
    daemon

defaults
    log global
    mode http
    # httpclose is necessary otherwise haproxy will rewrite only the first request of     the session
    option httpclose
    option httplog
    option dontlognull
    retries 3
    option redispatch
    maxconn 2000
    contimeout 5000
    clitimeout 50000
    srvtimeout 50000

frontend http
    bind :80
    option  forwardfor
    acl is_socket_io path_beg /node
    acl is_socket_io path_beg /socket.io
    #use_backend socket_io if is_socket_io
    acl myacl_rewrite0 hdr(host) -i HOST
    acl myacl_path0 path_dir -i 8081
    acl myacl_referer0 hdr_beg(referer) http://HOST/8081/
    use_backend socket_io_8081 if is_socket_io myacl_referer0
    use_backend mysrv_rewrite0 if myacl_rewrite0 myacl_path0
    acl myacl_rewrite1 hdr(host) -i HOST
    acl myacl_path1 path_dir -i 8090
    acl myacl_referer0 hdr_beg(referer) http://HOST/8090/
    use_backend socket_io_8090 if is_socket_io myacl_referer0
    use_backend mysrv_rewrite1 if myacl_rewrite1 myacl_path1

backend mysrv_rewrite0
    reqirep ^([^\ :]*)\ /8081/(.*)     \1\ /\2
    server myorigin_rewrite0 127.0.0.1:8081

backend mysrv_rewrite1
    reqirep ^([^\ :]*)\ /8090/(.*)     \1\ /\2
    server myorigin_rewrite1 127.0.0.1:8090

backend socket_io_8081
    mode http
    option httplog
    # long timeout
    timeout server 86400000
    # check frequently to allow restarting
    # the node backend
    timeout check 1s
    # add X-Forwarded-For
    option forwardfor
    # Do not use httpclose (= client and server
    # connections get closed), since it will close
    # Websockets connections
    no option httpclose
    # Use "option http-server-close" to preserve
    # client persistent connections while handling
    # every incoming request individually, dispatching
    # them one after another to servers, in HTTP close mode
    option http-server-close
    option forceclose
    server node1 127.0.0.1:8081 maxconn 2000 check

backend socket_io_8090
    mode http
    option httplog
    # long timeout
    timeout server 86400000
    # check frequently to allow restarting
    # the node backend
    timeout check 1s
    # add X-Forwarded-For
    option forwardfor
    # Do not use httpclose (= client and server
    # connections get closed), since it will close
    # Websockets connections
    no option httpclose
    # Use "option http-server-close" to preserve
    # client persistent connections while handling
    # every incoming request individually, dispatching
    # them one after another to servers, in HTTP close mode
    option http-server-close
    option forceclose
    server node2 127.0.0.1:8090 maxconn 2000 check

言うのを忘れました、私は持っています

socket.io version 0.9.11
haproxy version 1.4.18
4

0 に答える 0