eggjs / egg

🥚 Born to build better enterprise frameworks and apps with Node.js & Koa
https://eggjs.org
MIT License
18.9k stars 1.82k forks source link

在服务器时socket.io报错 #2730

Closed xiaobenSB closed 6 years ago

xiaobenSB commented 6 years ago

failed: Error during WebSocket handshake: 'Connection' header value must contain 'Upgrade'

使用了nginx做代理

egg-bot commented 6 years ago

Translation of this issue:


socket.io error on server

Failed: Error during WebSocket handshake: 'Connection' header value must contain 'Upgrade'

Use nginx as a proxy

BaffinLee commented 6 years ago

nginx 配置发一下

xiaobenSB commented 6 years ago

其实不配nginx,在本地也会报握手协议400,不知是不是用server.js启动项目原因,使用npm run dev不会

user nobody;

worker_processes 1;

error_log logs/error.log;

error_log logs/error.log notice;

error_log logs/error.log info;

pid logs/nginx.pid;

events { worker_connections 1024; }

http { include mime.types; default_type application/octet-stream;

#log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
#                  '$status $body_bytes_sent "$http_referer" '
#                  '"$http_user_agent" "$http_x_forwarded_for"';

#access_log  logs/access.log  main;

sendfile        on;
#tcp_nopush     on;

#keepalive_timeout  0;
keepalive_timeout  65;

          gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.0;
gzip_comp_level 3;
gzip_types text/plain application/x-javascript application/javascript font/ttf text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png;
gzip_vary on;
gzip_disable "MSIE [1-6]\.";

server {
      listen 8090;
    server_name wzb.hjy.ooo;
     location / {
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "upgrade";
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header Host $host;
            proxy_pass http://111.67.198.222:7001;

            # http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_bind
            # proxy_bind       $remote_addr transparent;
     }
}

    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    #error_page   500 502 503 504  /50x.html;
    #location = /50x.html {
      #  root   html;
    #}

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    #location ~ \.php$ {
    #    root           html;
    #    fastcgi_pass   127.0.0.1:9000;
    #    fastcgi_index  index.php;
    #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
    #    include        fastcgi_params;
    #}

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #    deny  all;
    #}
#}

# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
#    listen       8000;
#    listen       somename:8080;
#    server_name  somename  alias  another.alias;

#    location / {
#        root   html;
#        index  index.html index.htm;
#    }
#}

# HTTPS server
#
#server {
#    listen       443;
#    server_name  localhost;

#    ssl                  on;
#    ssl_certificate      cert.pem;
#    ssl_certificate_key  cert.key;

#    ssl_session_timeout  5m;

#    ssl_protocols  SSLv2 SSLv3 TLSv1;
#    ssl_ciphers  HIGH:!aNULL:!MD5;
#    ssl_prefer_server_ciphers   on;

#    location / {
#        root   html;
#        index  index.html index.htm;
#    }
#}

}

BaffinLee commented 6 years ago

不是 egg-scripts 启动的吗,server.js 是什么内容?

xiaobenSB commented 6 years ago

const egg = require('egg');

const workers = Number(process.argv[2] || require('os').cpus().length); egg.startCluster({ workers, baseDir: __dirname, });

Ailein commented 6 years ago

server.js

sticky: true , 配置下

xiaobenSB commented 6 years ago

不显示握手协议,谢谢了

xiaobenSB commented 6 years ago

不行了,过一会控制台自动又报出握手协议

xiaobenSB commented 6 years ago

在前端页面 transports数组设为'polling ‘’ const socket = io('/ioo', {

// 实际使用中可以在这里传递参数
query: {
  room: 'demo',
  userId: `client_${Math.random()}`,
},
transports: [
'polling'

], });

这样还没报握手协议

thonatos commented 6 years ago

如果依然不能解决问题,建议提供最小可复现仓库,这样提问无法判断那边是什么状况。

参考链接: