2017-08-17 164 views
0

上工作我將我的網站移至https://。 在http到套接字上有一個通過ws://sitename.com:3003的連接,現在有必要在wss://sitename.com上使用它:3003. 我該如何做到這一點? 謝謝。PHP websockets無法在wss://

PHP:

$loop = React\EventLoop\Factory::create(); 
$context = new React\ZMQ\Context($loop); 
$pull = $context->getSocket(ZMQ::SOCKET_PULL); 
$pull->bind('tcp://127.0.0.1:3004'); 
$pull->on('message', array($pusher, 'onMessage')); 

$webSock = new React\Socket\Server($loop); 
$webSock->listen(3003, '0.0.0.0'); 
$webServer = new Ratchet\Server\IoServer(
    new Ratchet\Http\HttpServer(
    new Ratchet\WebSocket\WsServer(
     new Ratchet\Wamp\WampServer(
     $pusher 
    ) 
    ) 
), 
    $webSock 
); 

$loop->run(); 

JS:

window.phpSocket = new ab.Session('wss://sitename.com:3003', 

nginx的:

server { 
    listen 443 ssl; 
    keepalive_timeout 70; 

    client_max_body_size 500M; 
    root /var/www/path/to/site/root; 
    index index.php; 
    server_name sitename.com; 
    gzip_static on; 
    gzip   on; 
    gzip_min_length 1000; 
    gzip_proxied expired no-cache no-store private auth; 
    gzip_types  text/plain application/xml text/css text/javascript application/javascript; 


    ssl      on; 
    ssl_certificate   /etc/letsencrypt/live/sitename.com/fullchain.pem; 
    ssl_certificate_key  /etc/letsencrypt/live/sitename.com/privkey.pem; 
    ssl_session_cache  shared:SSL:10m; 
    ssl_session_timeout  10m; 

回答

0

因爲你的nginx的conf沒有規定如何將客戶端WSS轉發到你的服務器端口聽了在3003.

你應該在nginx.conf添加到您的服務器塊

location /wss/ { 
    proxy_pass http://localhost:3003; 
    proxy_http_version 1.1; 
    proxy_set_header Upgrade $http_upgrade; 
    proxy_set_header Connection "upgrade"; 
} 

,改變你的JS代碼到這一點:

window.phpSocket = new ab.Session('wss://sitename.com/wss/', 

有關如何使用nginx作爲websocket_proxy更多信息,請參閱https://www.nginx.com/blog/websocket-nginx/