简体   繁体   中英

websocket + php + nginx - super simple

I'm trying to set up super simple websocket on PHP + nginx

nginx

server {
    listen 8443 ssl;
    server_name  websocket.example.com;

    root  /var/www/public/websocket;

    ssl_certificate  /var/ini/ssl/public.crt;
    ssl_certificate_key  /var/ini/ssl/private.key;

    error_log  /var/log/nginx/error_websocket.log warn;

    location / {
        proxy_pass  http://127.0.0.1:9000;
        proxy_http_version  1.1;
        proxy_set_header  Upgrade $http_upgrade;
        proxy_set_header  Connection "upgrade";
        proxy_read_timeout  86400;
    }
}

client

var host = 'wss://websocket.dyntest.dk:8443';
var socket = new WebSocket(host);
socket.onmessage = function(e){
    document.getElementById('ws_test').innerHTML = e.data;
    console.log(e.data);
};

server

$address = '127.0.0.1';
$port = 9000;
// Create WebSocket.
$server = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
socket_set_option($server, SOL_SOCKET, SO_REUSEADDR, 1);
socket_bind($server, $address, $port);
socket_listen($server);
$client = socket_accept($server);
// Send WebSocket handshake headers.
$request = socket_read($client, 5000);
preg_match('#Sec-WebSocket-Key: (.*)\r\n#', $request, $matches);
$key = base64_encode(pack(
    'H*',
    sha1($matches[1] . '258EAFA5-E914-47DA-95CA-C5AB0DC85B11')
));
$headers = "HTTP/1.1 101 Switching Protocols\r\n";
$headers .= "Upgrade: websocket\r\n";
$headers .= "Connection: Upgrade\r\n";
$headers .= "Sec-WebSocket-Version: 13\r\n";
$headers .= "Sec-WebSocket-Accept: $key\r\n\r\n";
socket_write($client, $headers, strlen($headers));
// Send messages into WebSocket in a loop.
while (true){
    sleep(1);
    $content = 'Now: ' . time();
    $response = chr(129) . chr(strlen($content)) . $content;
    socket_write($client, $response);
}

start websocket server

php -q public/websocket/index.php

error

In the browser: wss://websocket.example.com:8443/

ERR_DISALLOWED_URL_SCHEME

It looks like your certificate isn't trusted by the browser, which is not allowing you to access the WebSocket. You need to either add the CA to you computer's/browser's trust store or get a trusted cert. If you do want a trusted cert you could use Let's Encrypt or you could pay for a SSL cert from any number of companies that sell such things.

To add to the trust store of your machine try these links (the basic premise of which is to find where the browser stores the CA/Cert information and manually add yours):

With the new information you provided you may need to fix your nginx conf, add the web socket location as a passthrough in the nginx conf.

location /websocket/ {

proxy_pass ​http://backend_host;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_read_timeout 86400;

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM