配置文件,使支持websocket协议

websocket-nginx

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
server {
listen 80;
server_name localhost;


location / {
root /usr/share/nginx/html;
index index.html index.htm;
}

location = /50x.html {
root /usr/share/nginx/html;
}

location /ejy-push-service {
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_read_timeout 600;

proxy_pass http://localhost:19877/;
}
}

需要注意proxy_read_timeout

websocket也是一种长链接,默认的proxy_read_timeout 为60s

应用市场使用WebSocket Test Client进行测试

ws://www.test.com/ws
wss://www.test.com/wss

URL : ws://www.test.com/ws
REQUEST: {“type”: “ping” }
Message Log:
{“type”: “ping” }
{“type”:”ping”,”message”:”pong…”}

k8s ingress中设置websocket

apiVersion: extensions/v1beta1     
kind: Ingress    
metadata:           
  name: ingress名称
  namespace: ingress所属命名空间
  annotations:           
    #ingress使用那种软件 
    kubernetes.io/ingress.class: nginx
    #配置websocket 需要的配置   
    nginx.ingress.kubernetes.io/configuration-snippet: |
       proxy_set_header Upgrade "websocket";
       proxy_set_header Connection "Upgrade";
spec:      
  rules: 
  - host: 识别的域名
    http:
      paths: 
        #代理websocket服务
      - path: /websocket地址
        backend:
          serviceName: websocket服务名称
          servicePort: websocket服务端口