当前位置:   article > 正文

解决websocket在部署到服务器https下无法使用的问题_因为ssl nwebsocket 不可用

因为ssl nwebsocket 不可用

目录

一、问题

1.1 问题描述

1.2 问题详细描述

二、解决

2.1 https下的链接类型

2.2 修改Nginx的配置

一、问题

1.1 问题描述

一个小项目中使用到了websocket,这个websocket在本地完全是完全正常运行的,不管是前后台的信息通讯 还是 异常报错接收无任何异常,但当把后台代码部署到阿里云服务器后,websocket就再也链接不上了;

(PS: 博主的websocket是应用在uni-app中的,因此使用的是uni-app自带的websocket,这个本质上没有任何区别~)

1.2 问题详细描述

uni-app端的简易websocket代码,大致如下;

  1. // 建立websocket
  2. uni.connectSocket({
  3. url: Setting.wsUrl
  4. });
  5. // 打开
  6. uni.onSocketOpen(res => {
  7. console.log('--- 数据通信已打开 ---');
  8. });
  9. uni.onSocketError((res) => {
  10. console.log('--- 数据通信连接失败,请检查 ---');
  11. });
  12. uni.onSocketMessage((res) => {
  13. // console.log('收到服务器内容:' + res.data);
  14. this.resultData(JSON.parse(res.data))
  15. });

当项目启动打开websocket页面,与后台链接成功后前台会打印出:--- 数据通信已打开 ---,

运行后在本地正确打印了结果

但是 当后台文件打包部署到阿里云服务器 后,websocket链接异常,无法正确打印结果;

二、解决

我在这里一共遇到了 两个错误,第一个分别是 https下的链接类型 以及 nginx配置异常 这两个问题,这两个问题解决后,websocket即可正常使用了;

2.1 https下的链接类型

这个问题主要在于链接中的类型写错了,需要把 ws 改写成 wss ,如本地的时候由于使用的是http类型,因此websocket的链接写成了

ws://localhost:5002

但当部署到服务器之后,由于服务器是https的格式,因此ws显然不再合适了,需要改成 wss,即

wss://www.xxxxxx.com/xxx/

放到具体uni-app代码中就是这样

  1. // 建立websocket
  2. uni.connectSocket({
  3. url: "ws://localhost:5002"
  4. });
  5. // 需要改成 wss 开头的
  6. uni.connectSocket({
  7. url: "wss://www.xxxx.com/xxxx"
  8. });

这样,就将兼容https下的websocket了;

2.2 修改Nginx的配置

除了改wss外,nginx同样要修改,默认情况下的Nginx是不支持的,因此需要手动去开通这个功能;

首先,找到Nginx的配置文件,然后在配置文件中为需要开通websocket的后台加上这几条语句

  1. proxy_http_version 1.1;
  2. proxy_set_header Upgrade $http_upgrade;
  3. proxy_set_header Connection "upgrade";
  4. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  5. proxy_set_header X-Real-IP $remote_addr;

以我这边为例,改之前是这样的

  1. server {
  2. listen 443 ssl;
  3. server_name www.xxxx.com;
  4. # access_log /var/log/nginx/access.log main;
  5. # error_log /var/log/nginx/error.log;
  6. ssl_certificate #填写解压的pem文件
  7. ssl_certificate_key #填写解压的key文件
  8. # ssl on;
  9. ssl_session_cache shared:SSL:10m;
  10. ssl_session_timeout 50m;
  11. ssl_ciphers HIGH:!aNULL:!MD5;
  12. ssl_prefer_server_ciphers on;
  13. location / {
  14. root 前台文件;
  15. }
  16. location /api/ {
  17. proxy_pass 后台接口地址;
  18. }
  19. location /api2/ {
  20. proxy_pass 后台接口地址;
  21. }
  22. }

改完之后如下

  1. server {
  2. listen 443 ssl;
  3. server_name www.xxxx.com;
  4. # access_log /var/log/nginx/access.log main;
  5. # error_log /var/log/nginx/error.log;
  6. ssl_certificate #填写解压的pem文件
  7. ssl_certificate_key #填写解压的key文件
  8. # ssl on;
  9. ssl_session_cache shared:SSL:10m;
  10. ssl_session_timeout 50m;
  11. ssl_ciphers HIGH:!aNULL:!MD5;
  12. ssl_prefer_server_ciphers on;
  13. location / {
  14. root 前台文件;
  15. }
  16. location /api/ {
  17. proxy_pass 后台接口地址;
  18. }
  19. location /api2/ {
  20. proxy_pass 后台接口地址;
  21. proxy_http_version 1.1;
  22. proxy_set_header Upgrade $http_upgrade;
  23. proxy_set_header Connection "upgrade";
  24. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  25. proxy_set_header X-Real-IP $remote_addr;
  26. }
  27. }

加上之后 重启Nginx,websocket即可正常使用了,解决问题;

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小丑西瓜9/article/detail/449584?site
推荐阅读
相关标签
  

闽ICP备14008679号