Incompatibile SockJS! Main site uses: "1.1.5", the iframe: "1.0.0"

目录

一、问题描述:

二、问题解决


一、问题描述:

公司有个项目用到了websocket,在本地环境测试没有问题,因为公司后台websocket是微服务搭建,我们需要nginx进行一层代理,结果出现如下错误

错误1:WebSocket failed: Error during WebSocket handshake: Unexpected response code: 400

错误2:Incompatibile SockJS! Main site uses: "1.1.5", the iframe: "1.0.0"

二、问题解决

1、首先找到自己nginx的配置文件—>配置下面代码 ——> nginx重启

location /consultation-websocket/ {
	  proxy_pass http://127.0.0.1:8098/consultation-websocket/;
      proxy_set_header X-Real-IP $remote_addr;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header Host $http_host;

      # For WebSocket upgrade header
      proxy_http_version 1.1;
      proxy_set_header Upgrade $http_upgrade;
      proxy_set_header Connection "upgrade";
}

2、其中最重要的是下面这三行

proxy_http_version 1.1;(告诉nginx使用HTTP/1.1通信协议,这是websoket必须要使用的协议)

proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade";(告诉nginx,当它想要使用WebSocket时,响应http升级请求)

3、80端口的配置

server {
            listen       80;
            server_name  school.godotdotdot.com;
            charset utf-8;


            proxy_http_version 1.1; 
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "upgrade";
    

            location /consultation-websocket/ {
                proxy_pass http://127.0.0.1:3000;
                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_connect_timeout 60;
                proxy_read_timeout 600;
                proxy_send_timeout 600;
            }

            error_page   500 502 503 504  /50x.html;
            location = /50x.html {
                root   html;
            }
     }

4、443端口的配置

server {
    listen       443 ssl;
    server_name  test-pacs.test.cn;

    client_max_body_size 50M;
    #   ssl on;
    ssl_certificate     /etc/nginx/cert/123.pem;
    ssl_certificate_key /etc/nginx/cert/123.key;
    ssl_session_timeout 5m;
    #ssl_protocols SSLv2 SSLv3 TLSv1;
    ssl_protocols TLSv1.2 TLSv1.1 TLSv1;
    ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP;

    ssl_prefer_server_ciphers on;
    location /consultation-websocket/ {
        proxy_pass http://127.0.0.1:8098/consultation-websocket/;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;

        # For WebSocket upgrade header
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
    }
  
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

}


5、参考文档:

Incompatibile SockJS! Main site uses:

你可能感兴趣的:(nginx,nginx)