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