抱歉,看了 http://wenda.workerman.net/?/question/1485 這篇文章,還不是特別明白(nginx比較弱)。
描述一下,web服務(wù)器中nginx 做反向代理,同時(shí)一個(gè)域名如 https://www.demo.com 系統(tǒng)https正常訪(fǎng)問(wèn)沒(méi)有問(wèn)題。
參照上面那篇文章,我加了這部分配置:
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 進(jìn)程,這里使用Text協(xié)議,可以用telnet測(cè)試
$gateway = new Gateway("websocket://0.0.0.0:8282",$context);
// gateway名稱(chēng),status方便查看
$gateway->name = 'YourAppGateway';
// 開(kāi)啟SSL,websocket+SSL 即wss
$gateway->transport = 'ssl';
然后在js中使用websocket應(yīng)該訪(fǎng)問(wèn)什么?
是下面這樣嗎?
// 證書(shū)是會(huì)檢查域名的,請(qǐng)使用域名連接
ws = new WebSocket("wss://xxxxxxxxxxxxxxxxx.com/wss");
ws.onopen = function() {
alert("連接成功");
ws.send('tom');
alert("給服務(wù)端發(fā)送一個(gè)字符串:tom");
};
ws.onmessage = function(e) {
alert("收到服務(wù)端的消息:" + e.data);
};
有這個(gè)502的錯(cuò)誤,是因?yàn)槲业膎ginx有問(wèn)題嗎?求指教,非常感謝!
Error during WebSocket handshake: Unexpected response code: 502
@walkor 首先很感謝您這么快就回復(fù)。
現(xiàn)在我是這么做的 nginx沒(méi)有做任何額外的更改,在gatewayWorker中開(kāi)啟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代碼如下:
// 證書(shū)是會(huì)檢查域名的,請(qǐng)使用域名連接
ws = new WebSocket("wss://xxxxxx.com:8282");
ws.onopen = function() {
alert("連接成功");
ws.send('tom');
alert("給服務(wù)端發(fā)送一個(gè)字符串:tom");
};
ws.onmessage = function(e) {
alert("收到服務(wù)端的消息:" + e.data);
};
報(bào)錯(cuò)是超時(shí)
(index):28 WebSocket connection to 'wss://xxxxxx.com:8282/' failed: Error in connection establishment: net::ERR_CONNECTION_TIMED_OUT
使用的騰訊云,安全組開(kāi)放了全部端口
ufw和iptables都是 inactive狀態(tài)
https://xxxxxx.com本身訪(fǎng)問(wèn)沒(méi)有問(wèn)題
當(dāng)我改成 text的時(shí)候,使用telnet測(cè)試127.0.0.1沒(méi)有問(wèn)題
$gateway = new Gateway("text://0.0.0.0:8282");
想問(wèn)一下是什么問(wèn)題?會(huì)是我的 crt和key 文件嗎?
crt和key文件地址沒(méi)有問(wèn)題的。