在客户端,我正在尝试建立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_proxy 和 mod_proxy_wstunnel 已安装。
这里缺少什么东西吗?似乎请求通过但没有建立连接。
答案 0 :(得分:10)
我最终通过对虚拟主机使用此配置来解决此问题,虚拟主机使用HTTP标头过滤请求:
{print ext, extensions[ext]}
我将此作为参考,以防其他人
答案 1 :(得分:0)
@ 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的开头已经包含/。
答案 2 :(得分:0)
这是我为我工作的virtualhost的设置,我在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