宝塔Nginx配置wss连接,使https站点可以使用websocket.docx - Word

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链接了。

服务器启动websocket服务

步骤略

前端创建websocket链接

注意连接方式需写成这样

new WebSocket('wss://域名/wss')

这样就配置完成了


本文在原文基础上进行了修改优化,若有侵权请联系删除 原文:https://blog.csdn.net/zouxiaohu2020/article/details/108197640

第1页,共1页
本文共0个字符
中文(中国)
辅助功能
文档日期2022-06-01 16:54:11