【发布时间】:2018-03-08 23:30:34
【问题描述】:
我正在 AWS EC2 实例上为我的 node.js 应用程序运行 nginx。我想使用 websockets (socket.io) 和正常的 http 请求/响应。我的问题是,每当我从移动设备到服务器的活动套接字连接并尝试发出正常的 http 请求时,都会调用移动设备的 socket.io 错误函数并显示消息“502 Bad Gateway”。
只有套接字有效。只有普通的 http 请求也可以。
我发现这个问题是在我将 nginx 设置为仅使用 https 之后发生的。
这是我在 /sites-enabled /sites-available 中的 nginx 配置:
server {
listen 80;
listen 443 ssl;
server_name example.com www.example.com;
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
include /etc/letsencrypt/options-ssl-nginx.conf;
if ($scheme != "https") {
rewrite ^ https://$host$request_uri? permanent;
}
location / {
proxy_pass http://127.0.0.1:3000;
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 $host;
}
}
Nginx 错误日志:
[error] 14117#14117: *50 upstream prematurely closed connection while reading response header from upstream, client: 78.94.9.226, server: example.com, request: "GET /socket.io/?transport=polling&b64=1&sid=rgXMQhL6mbSET8ktAAAA HTTP/1.1", upstream: "http://127.0.0.1:3000/socket.io/?transport=polling&b64=1&sid=rgXMQhL6mbSET8ktAAAA", host: "example.com"
iOS 错误日志:
LOG SocketIOClient: Handling event: error with data: ["Got unknown error from server <html>\r\n<head><title>502 Bad Gateway</title></head>\r\n<body bgcolor=\"white\">\r\n<center><h1>502 Bad Gateway</h1></center>\r\n<hr><center>nginx/1.10.3 (Ubuntu)</center>\r\n</body>\r\n</html>\r\n"]
如果您需要更多信息,请告诉我!
【问题讨论】:
标签: node.js nginx amazon-ec2 socket.io