【问题标题】:WebSocket through SSL with Apache reverse proxy通过 SSL 使用 Apache 反向代理的 WebSocket
【发布时间】:2017-09-19 00:20:44
【问题描述】:

在客户端,我正在尝试建立 wss 连接:

var ws = new WebSocket("wss://wsserver.com/test")

它返回一个错误:

WebSocket connection to 'wss://wsserver.com/test' failed: Error during WebSocket handshake: Unexpected response code: 400

完整的标题是:

请求标头

GET wss://wsserver.com/test HTTP/1.1
Host: wsserver.com
Connection: Upgrade
Pragma: no-cache
Cache-Control: no-cache
Upgrade: websocket
Origin: https://website.net
Sec-WebSocket-Version: 13
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36
Accept-Encoding: gzip, deflate, sdch, br
Accept-Language: en-US,en;q=0.8
Sec-WebSocket-Key: Tj9AJ5TKglNf5LoHsQTpvQ==
Sec-WebSocket-Extensions: permessage-deflate; client_max_window_bits

响应标头

Access-Control-Allow-Credentials:true
Access-Control-Allow-Origin:https://website.net
Connection:close
Content-Length:18
Content-Type:text/plain; charset=utf-8
Date:Fri, 21 Apr 2017 21:03:45 GMT
Server:Apache/2.4.18 (Ubuntu)
Vary:Origin
X-Content-Type-Options:nosniff

服务器端在 Apache 反向代理后面的 8888 端口上运行。这是 Apache 配置:

<VirtualHost *:443>
        ServerName website.com

        ProxyPreserveHost On
        ProxyRequests Off
        ProxyPass "/" "wss://localhost:8888/"

mod_proxymod_proxy_wstunnel 已安装。

这里有什么遗漏吗?似乎请求通过但没有建立连接。

【问题讨论】:

  • lcalhost?这是笔误吗?
  • 是的,确实,这是一个错字,但它并没有解决我的问题

标签: apache ssl websocket proxy reverse-proxy


【解决方案1】:

我最终通过对虚拟主机使用此配置解决了这个问题,它使用 HTTP 标头过滤请求:

<VirtualHost *:443>
    ServerName website.com

    RewriteEngine On

    # When Upgrade:websocket header is present, redirect to ws
    # Using NC flag (case-insensitive) as some browsers will pass Websocket
    RewriteCond %{HTTP:Upgrade} =websocket [NC]
    RewriteRule ^/ws/(.*)    wss://localhost:8888/ws/$1 [P,L]

    # All other requests go to http
    ProxyPass "/" "http://localhost:8888/"

我留下这个作为参考,以防它帮助别人

【讨论】:

  • 就我而言,这有点棘手,Node-RED 使用 /nodered/comms 作为 EndPoint,所以我必须将您的规则更改为 RewriteRule ^/nodered/comms wss://localhost:1880/nodered/comms [P,L]
【解决方案2】:

这是对我有用的虚拟主机设置,我在 docker 上有 .netcore 应用程序,SignalR 作为 websocket 服务。

5000 我的 .netcore 应用正在运行,/chatHub 我的 signalR 正在监听。

对以后遇到同样问题的人会有所帮助。

<IfModule mod_ssl.c>
<VirtualHost *:443>
  RewriteEngine On
  ProxyPreserveHost On
  ProxyRequests Off

  # allow for upgrading to websockets
  RewriteEngine On
  RewriteCond %{HTTP:Upgrade} =websocket [NC]
  RewriteRule /(.*)           ws://localhost:5000/$1 [P,L]
  RewriteCond %{HTTP:Upgrade} !=websocket [NC]
  RewriteRule /(.*)           http://localhost:5000/$1 [P,L]


  ProxyPass "/" "http://localhost:5000/"
  ProxyPassReverse "/" "http://localhost:5000/"

  ProxyPass "/chatHub" "ws://localhost:5000/chatHub"
  ProxyPassReverse "/chatHub" "ws://localhost:5000/chatHub"

  ServerName site.com
  
SSLCertificateFile /etc/letsencrypt/live/site.com/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/site.com/privkey.pem
Include /etc/letsencrypt/options-ssl-apache.conf
</VirtualHost>
</IfModule>

来源:http://shyammakwana.me/server/websockets-with-apache-reverse-proxy-with-ssl.html

【讨论】:

    【解决方案3】:

    为了在不安全的 websocket 服务器前面放置一个安全的反向代理服务器,您可以这样做:

    <VirtualHost *:443>
        SSLEngine on
        SSLProxyEngine on
        SSLProtocol -all -SSLv2 -SSLv3 -TLSv1 -TLSv1.1 +TLSv1.2
        SSLCipherSuite HIGH:aNULL:eNULL:EXPORT:DES:RC4:!MD5:!PSK:!SRP:!CAMELLIA
        SSLCertificateFile /path/to/cert
        SSLCertificateKeyFile /path/to/key
        SSLCertificateChainFile /path/to/chain
        ServerName website.com
        
        RewriteEngine On
        RewriteCond %{HTTP:Upgrade} =websocket [NC]
        RewriteRule /(.*)    ws://localhost:8888/$1 [P,L]
    </VirtualHost>
    

    这将接收 wss://website.com:443 的入站请求,并将其反向代理到 ws://localhost:8888

    如果 websocket 服务器也是安全的,你可以简单地改变 ws://localhost:8888wss://website.com:8888

    【讨论】:

      【解决方案4】:

      @pimgeek 的评论:

      我认为不是 RewriteRule ^/nodered/comms wss://localhost:1880/nodered/comms [P,L]

      您可以按如下方式使用 $1: RewriteRule ^/nodered/comms$ wss://localhost:1880/$1 [P,L]

      此外,这也应该有效: RewriteRule ^/nodered/comms$ wss://localhost:1880$1 [P,L]

      注意端口后面不需要的 /,因为 $1 开头已经包含 /

      【讨论】:

      【解决方案5】:

      就我而言,我需要激活“SSLProxyEngine on”以使整个工作正常...

      我最终在 Debian / Apache 2.4 上得到了这个 2 行解决方案(使用的端口是 4321)

          SSLProxyEngine on
          ProxyPass         /wss  wss://127.0.0.1:4321/
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-11-22
        • 1970-01-01
        • 1970-01-01
        • 2016-04-17
        • 1970-01-01
        • 2014-11-14
        相关资源
        最近更新 更多