workerman——配置小程序的wss协议

摘要:
前言服务器:阿里云服务器|需要在安全组放开443端口和workerman需要的端口环境:oneinstack|lnmponeinstack添加虚拟主机的时候选择第三个即可|这个添加虚拟主机后面大家觉得要写个详细的或者有问题的话可以私信我服务器端配置workerman官方文档http://doc.workerman.net/faq/secure-websocket-server.html在你虚拟主机
前言

服务器: 阿里云服务器 | 需要在安全组放开443端口和workerman需要的端口

环境: oneinstack|lnmp

oneinstack添加虚拟主机的时候选择第三个即可 | 这个添加虚拟主机后面大家觉得要写个详细的或者有问题的话可以私信我

workerman——配置小程序的wss协议第1张

服务器端配置

workerman官方文档

http://doc.workerman.net/faq/secure-websocket-server.html

在你虚拟主机的配置文件里添加以下配置即可,利用nginx来代理wss

配置文件目录 | /usr/local/nginx/conf/vhost/

配置文件 | 域名.conf

location /wss {
    proxy_pass http://127.0.0.1:8282;

    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
}  

完整配置

其中xxxxxxxxx部分为域名或者证书加密字段

server {
  listen 443ssl http2;
  ssl_certificate /usr/local/nginx/conf/ssl/xxxxxxxxxxxx.crt;
  ssl_certificate_key /usr/local/nginx/conf/ssl/xxxxxxxxxxxxxx.key;
  ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3;
  ssl_ciphers xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx;
  ssl_prefer_server_ciphers on;
  ssl_session_timeout 10m;
  ssl_session_cache builtin:1000shared:SSL:10m;
  ssl_buffer_size 1400;
  add_header Strict-Transport-Security max-age=15768000;
  ssl_stapling on;
  ssl_stapling_verify on;
  server_name taihaoche.ecdpower.net;
  access_log /data/wwwlogs/xxxxxxxxxxx_nginx.log combined;
  index index.html index.htm index.php;
  root /data/wwwroot/xxxxxxxxx/public/;
  if ($ssl_protocol = "") { return 301 https://$host$request_uri; }

  include /usr/local/nginx/conf/rewrite/thinkphp.conf;
  #error_page 404 /404.html;
  #error_page 502 /502.html;
  location /wss {
        proxy_pass http://127.0.0.1:8282;

        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
  }
  location ~ [^/].php(/|$) {
    try_files $uri =404;
    #fastcgi_pass remote_php_ip:9000;
    fastcgi_pass unix:/dev/shm/php-cgi.sock;
    fastcgi_index index.php;
    include fastcgi.conf;
    set $real_script_name $fastcgi_script_name;
    if ($fastcgi_script_name ~ "^(.+?.php)(/.+)$") {
      set $real_script_name $1;
      set $path_info $2;
    }
    fastcgi_param SCRIPT_FILENAME $document_root$real_script_name;
    fastcgi_param SCRIPT_NAME $real_script_name;
    fastcgi_param PATH_INFO $path_info;
  }
  location ~ .*.(gif|jpg|jpeg|png|bmp|swf|flv|mp4|ico)$ {
    expires 30d;
    access_log off;
  }
  location ~ .*.(js|css)?$ {
    expires 7d;
    access_log off;
  }
  location ~ /.ht {
    deny all;
  }
}

配置完成后不要忘记重新载入nginx,两个命令任选一个就好

service nginx reload   |  systemctl reload nginx
小程序端配置

xxxx代表着域名,直接是域名没有www这些东西

export let websocket_url = 'wss://xxxxxx/wss'

连接socket的方法,可参考微信小程序API中的用法 | 我使用的是mpvue框架

webSocketTask =mpvue.connectSocket({
    url: websocket_url,
    success: function(res) {
      console.log(res);
    },
    fail: function(fail) {
      console.log(fail);
    }
})

有问题评论和私信都可以,推荐评论这样后面的博友有相同的问题可以直接解决~

免责声明:文章转载自《workerman——配置小程序的wss协议》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇Python学习之路:红绿灯例子selenium滑块操作下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

微信小程序底部导航Tabbar

1,底部导航栏这个功能是非常常见的一个功能,基本上一个完成的app,都会存在一个导航栏,那么微信小程序的导航栏该怎么实现呢?经过无数的踩坑,终于实现了,好了,先看看效果图。 2,对于底部导航栏,小程序上给出的文档要求里面的item最少2个,最多五个。 3,在项目中找到app.json这个文件 { "pages":[ "pages/index/i...

解决微信小程序textarea层级太高遮挡其他组件的问题

<view class='remark'> <view class='title'> 备注说明 </view> <textarea class='mark_textarea' placeholder='请说明人员调用具体原因'...

小程序-广告轮播/控制属性

微信小程序广告轮播元素<swiper></swiper>  图片所在元素<swiper-item>/swiper-item> 其中属性有: autoplay:true,//是否自动播放 autoplaytxt:"停止自动播放", indicatorDots: true,//指示点 // i...

「微信云托管」首篇实战|极简DEMO入门

一、项目介绍 此项目基于微信云托管能力范围编写,构建了一个简单的 WEB 后端服务,统计 WEB 网页的访问日志,并保存至 serverless 形态的 mysql 数据库中,然后在小程序中调用服务接口,获得访问次数。 项目包含微信云托管在单体运行中的基础能力使用,包含基础容器、mysql、微信小程序调用等。 项目技术栈:后端服务(nodejs + exp...

WePY 小程序授权点击取消授权失败的方案

今天看到一个微信小程序 wepy 框架的获取用户信息授权, 收录下,方便以后用的上。 在wepy里使用进行小程序页面授权,里面包含了用户点击取消的重新授权方案: //auth.js /* * @Author: Porco_Mar * @Date: 2018-04-11 15:49:55 * @Last Modified by: Porco_Mar...

uni-app中nvue (weex) 注意事项

前言 uni-app 是 DCloud 出品的新一代跨端框架,可以说是目前跨端数最多的框架之一了,目前支持发布到:App(Android/iOS)、H5、小程序(微信小程序/支付宝小程序/百度小程序/字节跳动小程序),目前市面上类似的框架还有:Taro(京东出品)、Megalo(网易出品)。 uni-app 的 nvue 说白了就是 weex 的那一套东西...