<blockquote class="danger">准备一个域名</blockquote>
<blockquote class="danger">准备一个ssl证书</blockquote>
1. 进入nginx配置文件目录新建一个配置文件,内容如下,将域名,端口,证书设置好
~~~
server{
listen 443 ssl;
server_name 域名;
#开启SSL
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
#证书文件
ssl_certificate 证书.crt;
ssl_certificate_key 证书.key;
#代理配置
location / {
proxy_set_header X-Original-Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host 域名;
#端口地址改成swoole服务端口
proxy_pass http://127.0.0.1:9501/;
}
#websocket代理路由地址
location /ws {
#端口地址改成swoole服务端口
proxy_pass http://127.0.0.1:9501;
proxy_read_timeout 60s;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'Upgrade';
}
}
~~~
2. 已在gitee上的static目录下增加了一个web文件夹,内部有个websocket调试demo
3. 打开地址[https://域名/static/web/ws.html](https://v3.haswallow.cn/static/web/ws.html)![](https://img.kancloud.cn/42/4f/424f3e6e31c609e9fc8f38196819cb00_1920x937.png)
4. 输入websocket地址wss://域名:443/ws 点击连接即可.![](https://img.kancloud.cn/f1/37/f137e1542c8f8d317d72973e16e3faa8_1920x937.png)
<blockquote class="danger">着重说明下,ws的连接地址不再是swoole的监听地址!!! wss连接默认走的就是443端口,所以:443可以省略即 wss://域名:443/ws 与 wss://域名/ws 等价. /ws为websocket代理路由地址,两者同步即可</blockquote>