Tag Archives: Nginx Websocket 400 Error

[Nginx] Configure nginx to support websocket to solve the problem of returning 400 error

When nginx is not configured to support webocket, but the domain name has been configured, such as: ws://gofly.sopans.com/ws_visitorĀ 

Direct js connection will return a 400 error

 

You need to add these three headers under the nginx location block to upgrade the http connection to a websocket connection

proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection “Upgrade”;

 

For example, I open source customerĀ gofly in

server{
       listen 80 ;
        server_name gofly.sopans.com;
        access_log /var/log/nginx/ gofly.sopans.com.access.log main;
        client_max_body_size 10M;
        location /static {
                root /var/www/html/go- fly;
        } 
        location / {
                proxy_pass http://127.0.0.1:8081;
                    proxy_http_version 1.1 ;
                    proxy_set_header X-Real- IP $remote_addr;
                    proxy_set_header Upgrade $http_upgrade;
                    proxy_set_header Connection "upgrade" ;
        }
}