关闭连接而不是设置“保持活力”#39;

时间:2014-05-11 08:23:48

标签: node.js express keep-alive

我试图在一个非常简单的keep-alive应用中禁用express设置。我试过the answer from someone doing the same in an older version

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

app.get('/', function(req, res) {
    res.set('Connection', 'close'); // attempting to override default (keep-alive)
    res.set('Proof', 'close');      // just to show that this should modify the header value
    res.send('hello');
});

var server = app.listen(3000, function() {
    console.log('Listening on port %d', server.address().port);
});

但是,Connection仍设置为keep-alive

$ curl -D - http://my-test-site.com
HTTP/1.1 200 OK
Server: nginx/1.4.7
Date: Sun, 11 May 2014 08:07:51 GMT
Content-Type: text/html; charset=utf-8
Content-Length: 5
Connection: keep-alive
X-Powered-By: Express
Proof: close
ETag: "907060870"

hello%

如何将keep-alive更改为close

版本

节点0.10.8
表达4.1.1

1 个答案:

答案 0 :(得分:4)

对我来说很好。我测试了localhost相同的版本。

$ curl -D - 127.0.0.1:3000
HTTP/1.1 200 OK
X-Powered-By: Express
Connection: close
Proof: close
Content-Type: text/html; charset=utf-8
Content-Length: 5
ETag: "907060870"
Date: Sun, 11 May 2014 08:52:32 GMT

hello%

我发现您可以使用nginx代理。如下所示:

client--nginx proxy--real server

我想你应该在nginx配置文件中禁用keep-alive