nginx官网文档:http://nginx.org/en/docs/http/websocket.html。
<font color='red'>前排提醒,需要先把建立好的站点部署好ssl证书再进行以下步骤</font>
打开站点设置的配置文件
map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}
# 这里的wss可以改成自己需要的名称,我自己用的是wss。
upstream wss {
# 这里填写外网ip加上端口,8181是我ws开启的端口。
server xxx.xx.xx.xxx:8181;
}
# 注意:这里的/wss后面没有/
location /wss {
proxy_pass http://wss;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
}
现在前端就可以通过“wss://你的服务器域名/wss”来创建websocket链接了。
步骤略
注意连接方式需写成这样
new WebSocket('wss://域名/wss')
本文在原文基础上进行了修改优化,若有侵权请联系删除 原文:https://blog.csdn.net/zouxiaohu2020/article/details/108197640