Report this

What is the reason for this report?

Can someone please help me regarding ratchet library on nginx

Posted on November 8, 2020

Hi,

I’m new to vps hosting and my website is hosted on digitalocean, I’m using ratchet library (http://socketo.me) for my php application. It’s working on localhost but not in server.

server.php

   <?php
    use Ratchet\Server\IoServer;
    use Ratchet\Http\HttpServer;
    use Ratchet\WebSocket\WsServer;
    use MyApp\Chat;
    
        require dirname(__DIR__) . '/vendor/autoload.php';
    
        $server = IoServer::factory(
            new HttpServer(
                new WsServer(
                    new Chat()
                )
            ),
            8080
        );
    
        $server->run();

In my client side code:

 if(window.WebSocket){
           var conn = new WebSocket('wss://mydomain.com');
           ......
           ......
        }

In my nginx configuration file (/etc/nginx/sites-available/mydomain.com )

 server {
        server_name mydomain.com www.mydomain.com;
        root /var/www/www.mydomain.com;
    
        index index.html index.htm index.php;
    
        location / {
            try_files $uri $uri/ =404;
        }
    
        location ~ \.php$ {
            include snippets/fastcgi-php.conf;
            fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
         }
    
        location ~ /\.ht {
            deny all;
        }
    
    
        listen 443 ssl; # managed by Certbot
        ssl_certificate /etc/letsencrypt/live/mydomain.com/fullchain.pem; # managed by Certbot
        ssl_certificate_key /etc/letsencrypt/live/mydomain.com/privkey.pem; # managed by Certbot
        include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
        ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
    
    
     }
    server {
        if ($host = www.mydomain.com) {
            return 301 https://$host$request_uri;
        } # managed by Certbot
    
    
        if ($host = mydomain.com) {
            return 301 https://$host$request_uri;
        } # managed by Certbot
    
    
        listen 80;
        server_name mydomain.com www.mydomain.com;
        return 404; # managed by Certbot
    
    
    
    }

In my console it says

“WebSocket connection to ‘wss://mydomain.com/’ failed: Error during WebSocket handshake: Unexpected response code: 200”

note: my domain has ssl. I’m new to this stuff, can somebody please guide me.

Thanks in advance!



This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Heya, An update on an old topic:

The issue you’re experiencing is because Nginx doesn’t directly support WebSocket connections. To fix this, you need to setup a reverse proxy to forward the WebSocket connections to your WebSocket server.

In your case, your WebSocket server is running on port 8080. So, you’ll need to add a new location block to your Nginx configuration that forwards connections from a specific path to your WebSocket server.

Update your Nginx configuration (/etc/nginx/sites-available/mydomain.com) to include a new location block for your WebSocket server:

server {
    server_name mydomain.com www.mydomain.com;
    root /var/www/www.mydomain.com;

    index index.html index.htm index.php;

    location / {
        try_files $uri $uri/ =404;
    }

    location /wsapp/ {  # new location block for WebSocket
        proxy_pass http://localhost:8080;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_read_timeout 86400;
    }

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
     }

    location ~ /\.ht {
        deny all;
    }

    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/mydomain.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/mydomain.com/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}

Now, your WebSocket server is accessible through wss://mydomain.com/wsapp/ rather than wss://mydomain.com/. This is due to the new location block which routes traffic to the WebSocket server only if the path starts with /wsapp/.

After making changes to the Nginx configuration, don’t forget to test the configuration and reload Nginx:

sudo nginx -t
sudo systemctl reload nginx

Update your client-side code to connect to the WebSocket server at the new path:

if(window.WebSocket){
    var conn = new WebSocket('wss://mydomain.com/wsapp/');
    ......
    ......
}

Now, the WebSocket connection should work as expected. If you’re still experiencing issues, check your firewall settings to ensure that incoming connections on port 8080 are allowed.

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.