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

作者: 大猪大猪 | 来源:发表于2018-03-16 22:38 被阅读229次

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

wechat

使用教程

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)
})

相关文章

网友评论

  • badboyqing:大佬端口怎么配置的,我看不懂你的端口,我现在访问的方式是 ws://域名:端口访问是可以的,但是wss://域名:端口就提示握手超时
    大猪大猪:@badboyqing 是的,证书自己去申请
    badboyqing:@dounine 你的那些ssl'的协议和https 的协议是一致的吗?
    大猪大猪:@badboyqing 配置说了挺明白的啦,不行就copy进去

本文标题:nginx简单配置同时支持微信小程序https/wss协议

本文链接:https://www.haomeiwen.com/subject/pxfdqftx.html