【问题标题】:PHP websockets doesn't works on wss://PHP websockets 不适用于 wss://
【发布时间】:2017-08-17 14:23:12
【问题描述】:

我将我的网站移至 https://。 在 http 到套接字上有一个通过 ws://sitename.com:3003 的连接,现在它们必须在 wss://sitename.com:3003 上可用。 我该怎么做呢? 谢谢。

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;

【问题讨论】:

    标签: php ssl nginx websocket ratchet


    【解决方案1】:

    因为你的 nginx 配置文件没有指定如何将你的客户端 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/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-04-04
      • 2020-07-21
      • 1970-01-01
      • 1970-01-01
      • 2018-10-04
      • 2020-09-19
      • 1970-01-01
      相关资源
      最近更新 更多