【发布时间】:2018-06-24 22:50:12
【问题描述】:
我想要达到什么目的?
在我的 ec2 实例上设置 socket.io,使用 Nginx 作为 Web 服务器,使用 AWS Application Load Balancer 和解析 DNS 以指向我的 Application Load Balancer 的 Route53。
以下是我在网上找到并尝试过的解决方案列表:
在应用程序级别使用网络负载平衡器与 TCP 80、TCP 443 和 SSL。客户端套接字连接 url 将是
https://mydomain:9000,我不断收到错误websocket connection closed before connection could be established.。目标群体已启用粘性。使用 Application Load Balancer,并为路径
/socket.io的侦听器添加基于路径的规则,并将其转发到启用粘性的不同目标组:现在我尝试了443协议和9000端口使用 HTTPS 协议。我得到了同样的结果。使用我的负载均衡器的公共 DNS 作为套接字客户端连接的 url。结果还是一样。
这是我的 nginx 配置文件:
# SSL configuration
listen 443 ssl default_server;
listen [::]:443 ssl default_server;
ssl_certificate /path/to/ssl.crt;
ssl_certificate_key /path/to/private_key.pem;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
if ($ssl_protocol = "") {
rewrite ^ https://$server_name$request_uri? permanent;
}
server_name _;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_http_version 1.1;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-NginX-Proxy true;
proxy_pass http://localhost:9000;
}
现在,我应该让你知道我已经强制套接字客户端使用websocket 传输,xhr-polling 给了我以下错误,我又不知道该怎么处理它。
Failed to load https://my_domain/socket.io/?EIO=3&transport=polling&t=M3lhVWi: The value of the 'Access-Control-Allow-Origin' header in the response must not be the wildcard '*' when the request's credentials mode is 'include'. Origin 'http://localhost:4444' is therefore not allowed access. The credentials mode of requests initiated by the XMLHttpRequest is controlled by the withCredentials attribute.
如果有人可以帮助我解决建立套接字连接的任何一种情况,那么我将非常感激。另外,请询问您是否需要任何其他相关信息。
编辑这是我设置 socket.io 的方法
const app = express();
const server = http.createServer(app);
const socketio = io(server, {
serveClient: false,
pingInterval: 10000,
pingTimeout: 5000,
cookie: false,
transport: ['websocket', 'xhr-polling']
});
socketConfig(socketio);
//and here's socketConfig
export function socketConfig(socketio) {
let localSockets = [];
socketio.on('connection', connectConfig(socketio, localSockets))
}
更新:我犯了一个愚蠢的错误,虽然这并没有解决我的问题,但客户端连接 url 将是 https://mydomain,因为 socket.io 和我的 nodejs 服务器正在监听同一个端口。我现在正在使用 Application Load Balancer,但出现 502 错误。
更新:我检查了我的 Nginx 错误日志,发现:
upstream prematurely closed connection while reading response header from upstream, client: 172.31.11.251, server: _, request: "GET /socket.io/?EIO=3&transport=websocket HTTP/1.1", upstream: "http://127.0.0.1:9000/socket.io/?EIO=3&transport=websocket", host: "mydomain"
【问题讨论】:
-
我也遇到过类似的问题。从客户端连接时,我们已将 ourdomain 替换为 wss://ourdomain:
io.connect('wss://ourdomain', {path:'<your_application_route_if_you_have_any>/socket.io' }) -
但是socket.io会自动处理,反正也没用。
-
如果您可以分享您的服务器端配置,以便我可以试用。
-
@DhavalChaudhary 请检查编辑,我已经更新了所需的详细信息
标签: node.js amazon-web-services sockets nginx