WebSocket配置与测试
2019-08-21 本文已影响0人
xyz098
WebSocket
与HTTP
协议的区别
-
HTTP
协议只能由客户端发出请求,服务端返回消息 -
WebSocket
协议则可以服务器主动向客户端推送消息,双向平等对话,全双工通信
WebSocket
特点
- 建议在
TCP
协议之上 - 默认80和443端口,握手阶段采用
HTTP
协议,能够通过HTTP
代理服务器 - 没有同源限制,客户端可与任意服务器通信
- 协议标识符是
ws
,(加密则为wss
)
nginx
配置与验证
-
配置
server { listen 80; server_name abc.abc.com; location ^~ /ws/interaction { rewrite /ws/interaction/(.*) /$1 break; proxy_connect_timeout 10s; proxy_read_timeout 3600s; proxy_send_timeout 12s; # 关键配置项,设置header的Upgrade、Connection proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade"; proxy_pass http://192.168.1.119:8999; } }
-
验证
# install npm install -g wscat # use wscat -c "ws://abc.abc.com/ws/interaction"
ingerss
配置与测试
-
配置
apiVersion: extensions/v1beta1 kind: Ingress metadata: name: interactive-ingress-test namespace: test-crm annotations: kubernetes.io/ingress.class: nginx nginx.ingress.kubernetes.io/proxy-body-size: 200m # 支持websocket操作 nginx.org/websocket-services: "ws-web-test" nginx.ingress.kubernetes.io/configuration-snippet: | proxy_set_header zone test; rewrite /ws/interaction/(.*) /$1 break; spec: rules: - host: abc.abc.com http: paths: - path: /ws/interaction backend: serviceName: ws-web-test servicePort: tcp8999
-
验证
# kubectl get svc -n ingress-nginx NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE ingress-nginx NodePort 172.16.255.225 <none> 80:30080/TCP,443:30106/TCP 158d 使用容器ip # wscat -H 'Host:abc.abc.com' -c 'ws://172.16.255.225/ws/interaction' 使用外网ip # wscat -H 'Host:abc.abc.com' -c 'ws://11.11.111.111/ws/interaction'
-
注意
若访问
ingress
返回404,则可能访问配置rewrite导致