如何修复“与'wss:// <url>'的WebSocket连接失败:WebSocket打开握手超时”?

时间:2019-03-23 08:10:40

标签: php apache nginx websocket ratchet

我已经在Hostgator提供的VPS服务器上安装了PHP应用程序。该应用程序还包括在端口cboden/ratchet上使用8080的websocket实现。这是启动websocket服务器的代码。

require_once __DIR__ . '/vendor/autoload.php';

$server = Ratchet\Server\IoServer::factory(
    new Ratchet\Http\HttpServer(
        new Ratchet\WebSocket\WsServer(
            new ChatSocket()
        )
    ),
    8080
);

$server->run();

使用ws协议时,websocket连接可以正常工作,但是wss协议在客户端抛出错误。

WebSocket connection to 'wss://<domain>:8080' failed: WebSocket opening handshake timed out

VPS服务器具有Apache和Nginx。 Nginx正在将请求代理到Apache。 HTTPHTTPSWS可以正常工作,但是要在客户端使用HTTPS,需要设置WSS。主域使用Lets加密SSL证书进行保护。

这是nginx配置:

server {
    listen <server-ip>:443 ssl http2;

    server_name <domain-name>;
    server_name <domain-name>;
    server_name <domain-name>;

    ssl_certificate             /opt/psa/var/certificates/<file-name>;
    ssl_certificate_key         /opt/psa/var/certificates/<file-name>;
    ssl_client_certificate      /opt/psa/var/certificates/<file-name>;

    client_max_body_size 128m;

    root "/var/www/vhosts/<subdomain-dir>/public";
    access_log "/var/www/vhosts/system/<subdomain-dir>/logs/proxy_access_ssl_log";
    error_log "/var/www/vhosts/system/<subdomain-dir>/logs/proxy_error_log";

    #extension letsencrypt begin
    location /.well-known/acme-challenge/ {
        root /var/www/vhosts/default/htdocs;

        types { }
        default_type text/plain;

        satisfy any;
        auth_basic off;
        allow all;

        location ~ ^/\.well-known/acme-challenge.*/\. {
            deny all;
        }
    }
    #extension letsencrypt end

    location / {
        proxy_pass https://<server-ip>:7081;
        proxy_set_header Host             $host;
        proxy_set_header X-Real-IP        $remote_addr;
        proxy_set_header X-Forwarded-For  $proxy_add_x_forwarded_for;
        proxy_set_header X-Accel-Internal /internal-nginx-static-location;
        access_log off;

    }

    location /internal-nginx-static-location/ {
        alias /var/www/vhosts/<subdomain-dir>/public/;
        internal;
    }


    add_header X-Powered-By PleskLin;

}

server {
    listen <server-ip>:80;

    server_name <domain-name>;
    server_name <domain-name>;
    server_name <domain-name>;

    client_max_body_size 128m;

    return 301 https://$host$request_uri;
}

您能告诉我如何设置WSS吗?

0 个答案:

没有答案