除了用Workerman自身的SSL,也能够利用nginx/apache做为wss代理转发给workerman(注意此方法workerman部分千万不要设置ssl,不然将没法链接)。nginx
通信原理及流程是:web
一、客户端发起wss链接连到nginx/apacheapache
二、nginx/apache将wss协议的数据转换成ws协议数据并转发到Workerman的websocket协议端口安全
三、Workerman收到数据后作业务逻辑处理服务器
四、Workerman给客户端发送消息时,则是相反的过程,数据通过nginx/apache转换成wss协议而后发给客户端websocket
前提条件及准备工做:session
一、已经安装nginx,版本不低于1.3socket
二、假设Workerman监听的是8282端口(websocket协议)测试
三、已经申请了证书(pem/crt文件及key文件)放在了/etc/nginx/conf.d/ssl下网站
四、打算利用nginx开启443端口对外提供wss代理服务(端口能够根据须要修改)
五、nginx通常做为网站服务器运行着其它服务,为了避免影响原来的站点使用,这里使用地址 域名/wss
做为wss的代理入口。也就是客户端链接地址为 wss://域名/wss
nginx配置相似以下:
server { listen 443; ssl on; ssl_certificate /etc/ssl/server.pem; ssl_certificate_key /etc/ssl/server.key; ssl_session_timeout 5m; ssl_session_cache shared:SSL:50m; ssl_protocols SSLv3 SSLv2 TLSv1 TLSv1.1 TLSv1.2; ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP; location /wss { proxy_pass http://127.0.0.1:8282; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "Upgrade"; proxy_set_header X-Real-IP $remote_addr; } # location / {} 站点的其它配置... }
测试
// 证书是会检查域名的,请使用域名链接 ws = new WebSocket("wss://域名/wss"); ws.onopen = function() { alert("链接成功"); ws.send('tom'); alert("给服务端发送一个字符串:tom"); }; ws.onmessage = function(e) { alert("收到服务端的消息:" + e.data); };
也能够利用apache做为wss代理转发给workerman(注意如使用apache代理SSL,则workerman部分千万不要设置ssl,不然将没法链接)。
准备工做:
一、GatewayWorker 监听 8282 端口(websocket协议)
二、已经申请了ssl证书, 放在了/server/httpd/cert/ 下
三、利用apache转发443端口至指定端口8282
四、httpd-ssl.conf 已加载
五、openssl 已安装
启用 proxy_wstunnel_module 模块
LoadModule proxy_module modules/mod_proxy.so LoadModule proxy_wstunnel_module modules/mod_proxy_wstunnel.so
配置SSL及代理
#extra/httpd-ssl.conf DocumentRoot "/网站/目录" ServerName 域名 # Proxy Config SSLProxyEngine on ProxyRequests Off ProxyPass /wss ws://127.0.0.1:8282/wss ProxyPassReverse /wss ws://127.0.0.1:8282/wss # 添加 SSL 协议支持协议,去掉不安全的协议 SSLProtocol all -SSLv2 -SSLv3 # 修改加密套件以下 SSLCipherSuite HIGH:!RC4:!MD5:!aNULL:!eNULL:!NULL:!DH:!EDH:!EXP:+MEDIUM SSLHonorCipherOrder on # 证书公钥配置 SSLCertificateFile /server/httpd/cert/your.pem # 证书私钥配置 SSLCertificateKeyFile /server/httpd/cert/your.key # 证书链配置, SSLCertificateChainFile /server/httpd/cert/chain.pem
测试
// 证书是会检查域名的,请使用域名链接 ws = new WebSocket("wss://域名/wss"); ws.onopen = function() { alert("链接成功"); ws.send('tom'); alert("给服务端发送一个字符串:tom"); }; ws.onmessage = function(e) { alert("收到服务端的消息:" + e.data); };