【问题标题】:heroku connection closed code=H13heroku 连接关闭代码=H13
【发布时间】:2017-05-20 04:57:58
【问题描述】:

我在heroku上部署了一个简单的nodejs服务器,带有ssl有效证书

var https = require('https');
var express = require('express');
var bodyParser = require('body-parser');
var fs = require('fs');

var app = express();
var cors = require('cors');

var credentials = {
    key: fs.readFileSync('./cert/key.key'),
    cert: fs.readFileSync('./cert/cert.crt'),
    ca: fs.readFileSync('./cert/bundle.crt'),
    requestCert: true,
    rejectUnauthorized: false
};

var server = https.createServer(credentials, app);

var io = require('socket.io')(server);

app.use(cors()); 
app.use(bodyParser.json({limit: '12mb'})); 
app.use(bodyParser.urlencoded({limit: '12mb', extended: true }));

io.on('connection', function(socket){
   console.log(socket);
   socket.on('authenticate', function(data){

      console.log(data);
   });
});

var port = process.env.PORT || 8080;

server.listen(port, function () {
    console.log("server listen on", this.address());
});

server.on('clientError', function(err) {
    console.log('ERROR', err);
});

问题是当我启动应用程序时收到此错误

ERROR { Error: socket hang up
at TLSSocket.<anonymous> (_tls_wrap.js:820:25)
at emitOne (events.js:101:20)
at TLSSocket.emit (events.js:188:7)
at Socket._handle.close (net.js:492:12)
at Socket.g (events.js:286:16)
at emitOne (events.js:96:13)
at Socket.emit (events.js:188:7)
at TCP._handle.close [as _onclose] (net.js:492:12) code: 'ECONNRESET' }

当我尝试通过 chrome 连接到我的服务器时,我收到了这个错误 在服务器端

2017-01-05T15:03:43.465542+00:00 heroku[router]: at=error code=H13 desc="Connection closed without response" method=GET path="/socket.io/?EIO=3&transport=polling&t=LblA3JZ" host=www.myapp.com request_id=51982ce9-99e3-4677-acb4-a0f14eb88999 fwd="95.251.90.218" dyno=web.1 connect=1ms service=2ms status=503 bytes=0
2017-01-05T15:03:43.460178+00:00 app[web.1]: ERROR Error: 140414443095936:error:1407609C:SSL routines:SSL23_GET_CLIENT_HELLO:http request:../deps/openssl/openssl/ssl/s23_srvr.c:394:

这个错误在客户端

XMLHttpRequest cannot load https://www.myapp.com/socket.io/?EIO=3&transport=polling&t=LblDK25. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://192.168.0.8:8100' is therefore not allowed access. The response had HTTP status code 503.

一开始我以为是 socket.io,但问题仍然存在于正常的 REST 操作中。 现在我认为我是 SSL 问题。我用 godaddy 提供者创建了一个有效的证书,然后我成功地将它上传到 heroku。事实上,如果我尝试通过 https 连接连接到我的应用程序,则证书是有效的。也许问题是当我尝试将此证书上传到我的 nodejs 应用程序时。有人可以帮助我吗?提前致谢

【问题讨论】:

    标签: node.js ssl heroku


    【解决方案1】:

    您应该使用 HTTP 服务器而不是 HTTPS。 SSL 终止发生在 Heroku 的负载均衡器上;它们向您的应用发送纯(非 SSL)流量,因此您的应用应创建非 HTTPS 服务器。

    HTTPS + SSL on Heroku - Node + Express

    【讨论】:

    • 谢谢。这行得通!但我有一些疑问。我不使用 heroku ssl 端点(20 美元的插件),但从爱好计划开始包含新的 Heroku SSL 服务。现在在我的服务器代码中,我只使用一个 http 服务器,但在客户端我连接到 'https://' 。我会理解连接是否安全
    • 您的应用与 Heroku 之间的连接是安全的,但 Heroku 会将未加密的数据从其负载均衡器发送到您的应用。也许您应该尝试使用诸如 express-sslify 之类的包来对传入请求强制执行 https,但我认为这不是最好的解决方案,因为您的敏感数据在被重定向到 https 之前被发送到 http。我找不到任何关于 SSL 如何在 Heroku 中工作的特定文档。我想我们只需要信任 Heroku :S 也许你冷尝试向 Heroku 支持以获取更多信息。
    • 好的,无论如何非常感谢。但是是的,这一点非常重要
    • @DiagonalThink 你能获得更多关于 Andres 提到的信息吗?
    • 真的很难找到根本问题。非常感谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-11-05
    • 2020-11-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-31
    • 1970-01-01
    相关资源
    最近更新 更多