title: EasySwoole wss meta:

  • name: description content: 通过nginx实现https的websocket协议
  • name: keywords content: swoole|swoole 拓展|swoole 框架|EasySwoole Socket|swoole socket|swoole websocket|swoole tcp|swoole udp|php websocket

Websocket支持Wss

这里推荐使用Nginx反向代理解决wss问题。

即客户端通过wss协议连接 Nginx 然后 Nginx 通过ws协议和server通讯。 也就是说Nginx负责通讯加解密,Nginx到server是明文的,swoole不用开启ssl,而且还能隐藏服务器端口和负载均衡(何乐不为)。

  1. server {
  2. # 下面这个部分和你https的配置没有什么区别,如果你是 宝塔 或者是 oneinstack 这里用生成的也是没有任何问题的
  3. listen 443;
  4. server_name 这里是你申请的域名;
  5. ssl on;
  6. # 这里是你申请域名对应的证书(一定要注意路径的问题,建议绝对路径)
  7. ssl_certificate 你的证书.crt;
  8. ssl_certificate_key 你的密匙.key;
  9. ssl_session_timeout 5m;
  10. ssl_session_cache shared:SSL:10m;
  11. ssl_protocols TLSv1 TLSv1.1 TLSv1.2 SSLv2 SSLv3;
  12. ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP;
  13. ssl_prefer_server_ciphers on;
  14. ssl_verify_client off;
  15. # 下面这个部分其实就是反向代理 如果你是 宝塔 或者是 oneinstack 请把你后续检查.php相关的 和重写index.php的部分删除
  16. location / {
  17. proxy_redirect off;
  18. proxy_pass http://127.0.0.1:9501; # 转发到你本地的9501端口 这里要根据你的业务情况填写 谢谢
  19. proxy_set_header Host $host;
  20. proxy_set_header X-Real_IP $remote_addr;
  21. proxy_set_header X-Forwarded-For $remote_addr:$remote_port;
  22. proxy_http_version 1.1;
  23. proxy_set_header Upgrade $http_upgrade; # 升级协议头
  24. proxy_set_header Connection upgrade;
  25. }
  26. }

重启nginx 如果没有错误 点我打开ws调试工具;

服务地址输入wss://你上面的域名不加端口号谢谢

点击开启连接 恭喜你 wss成了