我正在尝试在一个非常简单的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

版本号

Node 0.10.8
express 4.1.1

最佳答案

这对我来说可以。我测试了本地主机的相同版本。

$ 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

10-08 05:04