当与带有nginx代理的ssl配置一起使用时,SailsJs套接字(节点)在WebSocket握手期间会给出404错误

时间:2019-07-03 09:12:19

标签: node.js ssl nginx socket.io sails.js

SailsJs套接字(节点)在与nginx作为反向代理的ssl配置一起使用时,在WebSocket握手期间给出404错误。但是,当我使用从Nginx中删除ssl配置时,它工作正常。

详细的错误消息:

WebSocket connection to 'ws://desktop.panzuracloud.com/socket.io/?__sails_io_sdk_version=1.2.1&__sails_io_sdk_platform=node&__sails_io_sdk_language=javascript&EIO=3&transport=websocket' failed: Error during WebSocket handshake: Unexpected response code: 404

这是我的Nginx conf文件:

upstream panzura_server_upstream {
  server 127.0.0.1:1337;
}

server {
# The IP that you forwarded in your router (nginx proxy)
  listen 443;
  listen [::]:443;

  ssl on;
  ssl_certificate /etc/ssl/certs/panzura/certificate.crt;
  ssl_certificate_key /etc/ssl/certs/panzura/private.key;

  # Make site accessible from http://localhost/
  server_name desktop.panzuracloud.com;


  location / {
    proxy_pass_header Authorization;
    proxy_pass http://panzura_server_upstream;
    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_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
    proxy_buffering off;
    client_max_body_size 0;
    proxy_read_timeout 36000s;
    proxy_redirect off;
  }
}

server {
  listen 80;
  listen [::]:80;

  server_name _;
  return 301 https://$host$request_uri;
}

在前端,我正在使用react并且有一个相当简单的代码:

import React from 'react';
import socketIOClient from "socket.io-client";
import sailsIOClient from "sails.io.js";

class App extends React.Component {

  componentDidMount() {
    const io = sailsIOClient(socketIOClient);
    io.sails.useCORSRouteToGetCookie = false;
    io.sails.url = 'ws://desktop.panzuracloud.com';
    io.socket.on("connect", data => {
      console.log('connect',data);
    });

    io.socket.on("connect_error", data => {
      console.log('connect_error',data);
    });

    io.socket.on("connect_timeout", data => {
      console.log('connect_timeout',data);
    });

    io.socket.on('appMetricUpdate', (body) => {
      console.log(body.data.processes);
    });

  }

  render() { 
    return (<h1>Hi</h1>);
  }

}

请注意::当我从nginx中删除ssl配置并将位置定向放在侦听端口80的服务器块中时,相同的代码也起作用。

0 个答案:

没有答案