功能需求
公司使用swoole做消息通知服务
由于任务服务代码和应用代码放在不同的服务器中,任务服务器要求不能对外开放外网
所以要在应用服务器上对websocket的反响代理
大概的流程是这样的
nginx 配置内容
map $http_upgrade $connection_upgrade {
default Upgrade;
'' close;
}
location /websocket/ {
proxy_buffers 32 32k;
proxy_buffer_size 128k;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
proxy_pass http://ip(任务服务器IP):port;
}
这里 要说明几点注意事项
- failed: Error during WebSocket handshake: Unexpected response code: 301
这个错误主要是因为 nginx 配置项中没有区分大消息 Upgrade - but attempted to connect to the insecure WebSocket endpoint ‘******’. This request has been blocked; this endpoint must be available over WSS.
大概意思是使用的是https安全连接 不能使用ws这种不安全的东西 要使用wss
然后修改连接方式为wss - failed: Error in connection establishment: net::ERR_CERT_COMMON_NAME_INVALID
这种错误要查看建立服务时域名地址写的是否正确 正常情况使用域名的形式
特别提醒
建立的服务一定是 SWOOLE_SOCK_TCP 不能是 Swoole_ssl;
客户端请求要是wss 协议进行连接
推荐
https://www.thinbug.com/q/31923626