nginx简单配置同时支持微信小程序https/wss协议

微信小程序需要使用https与wss能才进行连接,虽然开发模式下可以使用http与ws,但发布的时候还是需要安全协议,网上的各种配置是超多超复杂的,这里已经对nginx指定版本进行最简单的配置,可用。


使用教程

nginx版本

$ nginx -v
nginx version: nginx/1.12.2


系统Centos7

$ uname -r
4.14.11-1.el7.elrepo.x86_64


cat /etc/nginx/conf.d/test.conf

server {
   listen   80;
   server_name test.dounine.com;
return 301 https://$host$request_uri;
}

server {
   listen 443;
   server_name test.dounine.com;
   ssl on;
   ssl_certificate /etc/nginx/ssls/test.xxxx.pem;
   ssl_certificate_key /etc/nginx/ssls/test.xxxx.key;

   location / {
       client_max_body_size    100m;
       proxy_pass http://localhost:7777;
       proxy_set_header  Host  $host;
       proxy_set_header  X-Real-IP  $remote_addr;
       proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
       proxy_set_header Upgrade $http_upgrade;
       proxy_set_header Connection "Upgrade";
}

}


微信小程序代码

wx.connectSocket({
 url: 'wss://test.dounine.com/ws'
});
wx.onSocketOpen(function(res) {
 console.info('websocket连接成功');
});
wx.onSocketClose(function(res) {
 console.log('WebSocket 已关闭!')
});
wx.onSocketError(function(res){
 console.log('WebSocket连接打开失败,请检查!')
});
wx.onSocketMessage(function(res) {
 console.log('收到服务器内容:' + res.data)
})


作者:dounine
链接:nginx简单配置同时支持微信小程序https/wss协议-教程-小程序社区-微信小程序-微信小程序开发社区-小程序开发论坛-微信小程序联盟
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值