如何在https中访问websocket?

时间:2017-10-24 04:42:08

标签: apache google-chrome ssl websocket

以下配置是为了通过SSL连接ws。

使用Apache 2.4服务器

在httpd.conf中

<VirtualHost *:8060>
RewriteEngine On
RewriteCond %{HTTP:Upgrade} =websocket [NC]
RewriteRule ^/BizMessenger/(.*)    ws://xxx:8070/BizMessenger/$1 [P,L]
ProxyPass /BizMessenger  ws://xxx:8070/BizMessenger/hello/chat
</VirtualHost>

错误: 在请求操作期间,浏览器中出现以下错误。

biz.js.xhtml?ln=js:49 Mixed Content: The page at 'https://xxx/main' was loaded over HTTPS, but attempted to connect to the insecure WebSocket endpoint 'ws://xxx:8060/BizMessenger/hello/chat'. This request has been blocked; this endpoint must be available over WSS.send_message @ biz.js.xhtml?ln=js:49 biz.js.xhtml?ln=js:49 Uncaught DOMException: Failed to construct 'WebSocket': An insecure WebSocket connection may not be initiated from a page loaded over HTTPS. at send_message (https://xxx/javax.faces.resource/biz.js.xhtml?ln=js:49:17)
at init (https://xxx/javax.faces.resource/biz.js.xhtml?ln=js:44:5)
at autochatlogin (https://xxx/javax.faces.resource/svmchat.js.xhtml?ln=js:85:1)
at HTMLButtonElement.onclick (https://xxx/main:1201:283)

Chrome会在地址栏中抛出一个盾牌来加载不安全的脚本

1 个答案:

答案 0 :(得分:0)

以下配置在

下的httpd-ssl.conf中完成
 RewriteEngine On
 RewriteCond %{HTTP:UPGRADE} ^WebSocket$ [NC,OR]
 RewriteCond %{HTTP:CONNECTION} ^Upgrade$ [NC]
 # the description of below code
 #RewriteRule webcontext  ws://prpxy server ip:port(redirecting port in vhost)
 RewriteRule ^/BizMessenger/(.*)    ws://192.168.18.31:8060/BizMessenger/$1 [P,L]

V_host.conf文件

<VirtualHost *:8060>
RewriteEngine On
RewriteCond %{HTTP:UPGRADE} ^WebSocket$ [NC,OR]
RewriteCond %{HTTP:CONNECTION} ^Upgrade$ [NC]
# the description of below code
#RewriteRule webcontext  ws://appserver ip:port
RewriteRule ^/BizMessenger/(.*)    ws://192.168.18.50:8070/BizMessenger/$1 [P,L]
</VirtualHost>

websocket连接请求: 对于ssl:          WSS://192.168.18.31/BizMessenger/hello/chat