繁体   English   中英

HAProxy + Nodejs + SockJS + Express + SSL

[英]HAProxy + Nodejs + SockJS + Express + SSL

我在NodeJS中有一个服务器设置,如下图所示:

在此输入图像描述

现在我想做两件似乎可以用HAProxy做的事情:

  1. 无论客户端想要访问什么服务器,只能使用一个端口。 我想将外部端口8080用于所有非SSL流量。 (所有SSL流量都应使用端口443)

  2. SockJS ServerExpress Server上启用SSL。

请注意,我所有的服务器都在amazon ec2上的同一个实例上运行。 所以我想在内部路由流量。

到目前为止,这是我的haproxy.cfg

    mode http
    # Set timeouts to your needs
    timeout client  10s
    timeout connect 10s
    timeout server  10s

frontend all 0.0.0.0:8080
    mode http
    timeout client 120s

    option forwardfor
    # Fake connection:close, required in this setup.
    option http-server-close
    option http-pretend-keepalive

    acl is_sockjs path_beg /echo /broadcast /close
    acl is_stats  path_beg /stats

    use_backend sockjs if is_sockjs
    use_backend stats if is_stats
    default_backend express


backend sockjs
    # Load-balance according to hash created from first two
    # directories in url path. For example requests going to /1/
    # should be handled by single server (assuming resource prefix is
    # one-level deep, like "/echo").
    balance uri depth 2
    timeout server  120s
    server srv_sockjs1 127.0.0.1:8081

backend express
    balance roundrobin
    server srv_static 127.0.0.1:8008

backend stats
    stats uri /stats
    stats enable

无法弄清楚如何将SSL和流量路由到TCP Server (8080内部端口)

有任何想法吗?

你的设置有点难以理解(对我而言)。 如果我正确理解您的目标,您希望通过SSL服务您的Web服务,因此端口443.从443,连接到端口8080(内部)。 如果是这种情况,那么以下配置可能就是您要查找的内容。 它并不真正使用端口8080,而是直接连接到您的express后端。 您实际上不需要暴露端口8080(除非您有特殊原因),因为您可以直接在前端部分内使用后端服务器。

请注意,这仅适用于HAProxy 1.5+,如果您使用的是旧版本的HAProxy,您应该在SSL连接到达HAProxy之前添加一些内容(但我强烈建议使用1.5,因为它会使您的设置不那么复杂)

frontend ssl
    bind *:443 ssl crt /path/to/cert.pem ca-file  /path/to/cert.pem
    timeout client 120s

    option forwardfor
    # Fake connection:close, required in this setup.
    option http-server-close
    option http-pretend-keepalive

    acl is_sockjs path_beg /echo /broadcast /close
    acl is_stats  path_beg /stats

    use_backend sockjs if is_sockjs
    use_backend stats if is_stats
    default_backend express

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM