Gateway 配置ssl

ptbird

抱歉,看了 http://wenda.workerman.net/?/question/1485 这篇文章,还不是特别明白(nginx比较弱)。

描述一下,web服务器中nginx 做反向代理,同时一个域名如 https://www.demo.com 系统https正常访问没有问题。

参照上面那篇文章,我加了这部分配置:

 location /wss
{
        proxy_pass http://127.0.0.1:8282;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        rewrite /wss/(.*) /$1 break;
        proxy_redirect off;
}

我的gateway中的代码是这样:

$context = array(
    'ssl' => array(
        'local_cert'  => '/usr/local/nginx/conf/ssl/xxxxxx.com.crt', // 或者crt文件
        'local_pk'    => '/usr/local/nginx/conf/ssl/xxxxxx.com.key',
        'verify_peer' => false
    )
);
// gateway 进程,这里使用Text协议,可以用telnet测试
$gateway = new Gateway("websocket://0.0.0.0:8282",$context);
// gateway名称,status方便查看
$gateway->name = 'YourAppGateway';
// 开启SSL,websocket+SSL 即wss
$gateway->transport = 'ssl';

然后在js中使用websocket应该访问什么?
是下面这样吗?

       // 证书是会检查域名的,请使用域名连接
        ws = new WebSocket("wss://xxxxxxxxxxxxxxxxx.com/wss");
        ws.onopen = function() {
            alert("连接成功");
            ws.send('tom');
            alert("给服务端发送一个字符串:tom");
        };
        ws.onmessage = function(e) {
            alert("收到服务端的消息:" + e.data);
        };

有这个502的错误,是因为我的nginx有问题吗?求指教,非常感谢!

Error during WebSocket handshake: Unexpected response code: 502

9338 5 0
5个回答

walkor

如果gateway设置了ssl,nginx就不需要了,直接gateway就行。

如果设置了nginx,那gateway就不用设置ssl了(把$context去掉)。

两个之中选一个就行,两个都配置ssl就重复了,会报错。

  • 暂无评论
ptbird

@walkor 首先很感谢您这么快就回复。

现在我是这么做的 nginx没有做任何额外的更改,在gatewayWorker中开启ssl,

start_gateway.php如下:

$context = array(
    'ssl' => array(
        'local_cert'  => '/usr/local/nginx/conf/ssl/xxxxxx.com.crt',
        'local_pk'    => '/usr/local/nginx/conf/ssl/xxxxxx.com.key',
        'verify_peer' => false
    )
);
// 
$gateway = new Gateway("websocket://0.0.0.0:8282",$context);
// 
$gateway->name = 'YourAppGateway';
// 
$gateway->transport = 'ssl';
// 
$gateway->count = 4;
// 
$gateway->lanIp = '127.0.0.1';
//
// 
$gateway->startPort = 4001;

js代码如下:

 // 证书是会检查域名的,请使用域名连接
ws = new WebSocket("wss://xxxxxx.com:8282");
ws.onopen = function() {
    alert("连接成功");
    ws.send('tom');
    alert("给服务端发送一个字符串:tom");
};
ws.onmessage = function(e) {
    alert("收到服务端的消息:" + e.data);
};

报错是超时

(index):28 WebSocket connection to 'wss://xxxxxx.com:8282/' failed: Error in connection establishment: net::ERR_CONNECTION_TIMED_OUT

使用的腾讯云,安全组开放了全部端口
ufw和iptables都是 inactive状态
https://xxxxxx.com本身访问没有问题

当我改成 text的时候,使用telnet测试127.0.0.1没有问题

$gateway = new Gateway("text://0.0.0.0:8282");

想问一下是什么问题?会是我的 crt和key 文件吗?
crt和key文件地址没有问题的。

  • 暂无评论
walkor

先不要加nginx。客户端直接访问gateway的8282端口,不然无法判断是不是nginx配置问题。

从报错来看ERR_CONNECTION_TIMED_OUT 是防火墙问题或者是安全组问题,如果无法解决可以给腾讯云提工单让他们给你解决。

  • 暂无评论
ymb

我也是遇到这个问题 解决了吗?毫无头绪

  • 暂无评论
gaoyongguang

我也遇到了,怎么处理的

  • 暂无评论
年代过于久远,无法发表回答
🔝