J H
J H

Reputation: 41

How does one set up an Nginx server to use WebSockets?

I have a website that needs to work through an Nginx server. At the moment, I don't believe the configuration for nginx is correct. I currently have

server{
        server_name websitename.com;

       location /websocket/{
                proxy_pass http://websitename.com;
                proxy_http_version 1.1;
                proxy_set_header Upgrade $http_upgrade;
                proxy_set_header Connection "Upgrade";
                proxy_set_header $host;
        }

}

At the moment, I am receiving an error on the website that reads "SCRIPT12008: SCRIPT12008: WebSocket Error: Incorrect HTTP response. Status code 200, OK", which I believe is because the protocol is not switching correctly.

Upvotes: 0

Views: 559

Answers (1)

Rodrigo Albuquerque
Rodrigo Albuquerque

Reputation: 66

You forgot the double quotes and the "Host" before $host to set the Host directive:

proxy_set_header Host $host;

So your config should would like this instead:

server{
        server_name websitename.com;

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

}

Always check your syntax with 'nginx -t' command.

Upvotes: 1

Related Questions