Hello

I tried to configure a Websocket proxy on my Nginx server, but unfortunately, I don’t get it working. I have read various forms but cannot get out of it. I think it has something to do between the client connection to the server. Local om my pc is everything working fine

client code:

 var port = new osc.WebSocketPort({
          url: "wss://circusfamilyprojects.nl/"
        });

server code:

var wss = new WebSocket.Server({
    port: 8083
});

This is my configuration in Nginx

# custom code for hop by hop headers
    map $http_upgrade $connection_upgrade {
        default upgrade;
        ''      close;
    }


    #custom code for connection with websocket this is the port on the server
     upstream websocket  {
        server 178.62.209.37:8083;
    }

server {

      listen [::]:443 ssl ipv6only=on; # managed by Certbot
      listen 443 ssl; # managed by Certbot
      ssl_certificate /etc/letsencrypt/live/circusfamilyprojects.nl/fullchain.pem; # managed by Cert$
      ssl_certificate_key /etc/letsencrypt/live/circusfamilyprojects.nl/privkey.pem; # managed by Ce$
      include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
      ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot

       root /var/www/html/vue/cfdomotica/server/public;

        # Add index.php to the list if you are using PHP
        index index.html index.htm index.nginx-debian.html;

       server_name  circusfamilyprojects.nl www.circusfmailyprojects.nl; # managed by Certbot

      location / {
            proxy_pass http://websocket;
            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-Forwarded-Proto $scheme;
            proxy_pass_request_headers on;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection $connection_upgrade;

        }
}

server {
    if ($host = www.circusfamilyprojects.nl) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


    if ($host = circusfamilyprojects.nl) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


        listen 8080 ;
        listen [::]:8080 ;
    server_name  circusfamilyprojects.nl;
    return 404; # managed by Certbot
}

In my browser I get the message “Status Code: 426 Upgrade Required” I hope someone can help me, thanks in advance!

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.

×
1 answer

Hi @dedominique94,

I believe you’ll need to change your server block. Please change your location block to be the following

      location / {
            proxy_pass http://websocket;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "upgrade";
            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-Forwarded-Proto $scheme;
            proxy_pass_request_headers on;
        }

One other thing, please allow access on port 80, I believe this might be causing issues as well.

When I check your website on port 80, I get

 curl -IL circusfamilyprojects.nl
curl: (7) Failed to connect to circusfamilyprojects.nl port 80: Connection refused

It’s possible you are experiencing the issues due to the above.

Regards,
KDSys

  • Hi @KDSys thanks for your help! I solved my problem by separating a server block for port 443 and a server block for the WebSocket client. I did the server block for the client on port 8086.

    So now WebSocket is working over HTTPS :)

    Now I have to make a redirect from HTTP to HTTPS, but that can’t be that difficult

    • Hi @dedominique94,

      I’m glad to hear that!

      Yes, a redirection is pretty easy to do in Nginx. You’ll just need to add the following in your http server block:

      server {
          listen 80 default_server;
      
          server_name example.com;
      
          return 301 https://$host$request_uri;
      }
      

      You’ll need to change example.com with your actual domain.

      Regards,
      KDSys

Submit an Answer