-1

i have a php script which only have some function like get some content or post a file to the server.

now i am trying to open a websocket with JS. But i got everytime that the handshake wouldnt work. new WebSocket('myDomain.com')

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

I am not sure, what is going wrong.

server {
        listen 80;
        root /var/www/html;
        index index.php index.html index.htm index.nginx-debian.html;
#    listen 80 default_server;
#    listen [::]:80 default_server;
#    root /var/www/html;
    server_name  mydomain.com;

#  index index.php index.html index.htm index.nginx-debian.html;

    listen 443 ssl; # managed by Certbot

    # RSA certificate
    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

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/var/run/php/php7.3-fpm.sock;
    }
    location / {
                try_files $uri $uri/ =404;
#      proxy_set_header Upgrade $http_upgrade;
#      proxy_set_header Connection $connection_upgrade;

    }

}
Anutrof
  • 27
  • 5

1 Answers1

0

Seems like a websocket server is missing in the setup.

It should be binding to a port somewhere independent of nginx.

The relevant nginx setting should look similar to this:

    location /ws {
        proxy_pass http://localhost:8123/websocket;

        proxy_http_version 1.1;
        proxy_set_header Host $host;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
    }

Your php script likely needs to be expanded into a php websocket server.

You can read up on it here:

How to create websockets server in PHP

vhoang
  • 1,349
  • 1
  • 8
  • 9
  • okay, so the problem is also that the php script needs to open a socket? So i need to give it back do the Developer. Thanks a lot! – Anutrof Nov 21 '19 at 19:58
  • If websockets are what you want to use, then there's more to do. If you want to get/post some content, you can take a look at ajax to communicate via GET/POST instead of websockets. – vhoang Nov 21 '19 at 20:16
  • i got a code to deploy on a nginx server and with technology i didnt use before. But i cant change the code at the moment. So i am glad to know that this isnt only nginx configuration which failed. – Anutrof Nov 21 '19 at 20:21