1

I'm trying to setup a websocket connection (wss). My domain uses ssl (certbot) and is powered by Nginx. I am unsure how to configure my /etc/nginx/sites-available/domain.com file.

server {
    listen 443 ssl http2;
    listen [::]:443 ssl http2;
...
}

I added the following into my config block:

location /websocket {
    proxy_pass         https://domain.com;
    proxy_http_version 1.1;
    proxy_set_header   Upgrade $http_upgrade;
    proxy_set_header   Connection "upgrade";
    proxy_set_header   Host $host;
}

When connecting via: wss://domain.com/, I am getting an error of

WebSocket connection to 'wss://domain.com/' failed: 
Error during WebSocket handshake: Unexpected response code: 200

Most of the examples out there are using a nodejs framework to serve their sites, but I am using php.

1 Answer 1

0

Your websocket endpoint is configured in the provided nginx configuration as hosted at the location /websocket within the server, but you are attempting to connect to the root URL wss://domain.com.

The websocket connection is an ordinary HTTP (with a TLS wrapper in this case) session until the upgrade takes place, so you must ensure the URL used to access the service includes all relevant path designators, e.g. wss://domain.com/websocket.

5
  • I understand that, sorta... However, I am getting WebSocket connection to 'wss://domain.com/websocket' failed: Error during WebSocket handshake: Unexpected response code: 500 with that setup, so unsure if it is my config or a port that needs to be configured?
    – PaulELI
    Commented Sep 28, 2018 at 21:44
  • What is hosted on https://domain.com? 500 indicates a server error, most probably from the back-end you're proxying to, while trying to respond to the web socket request. Check the PHP application is correctly receiving the web socket request and is speaking web socket in response. Its logs may provide further assistance in this respect. Commented Sep 28, 2018 at 21:46
  • A php cms (OctoberCMS). pvpaissa.com/broadcast/panel/match/score/testslug I'm using a websocket plugin. It works on my local macbook... The issue came after trying to set it up on my live server (ubuntu 16.04).
    – PaulELI
    Commented Sep 28, 2018 at 21:51
  • I found this article, will try and see if it bears fruit tomorrow modalduality.org/posts/…
    – PaulELI
    Commented Sep 29, 2018 at 5:32
  • Thank you, this worked. Now I need to see how to keep my connections active.
    – PaulELI
    Commented Sep 29, 2018 at 21:01

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .