【问题标题】:Close connection instead of setting 'keep-alive'关闭连接而不是设置“keep-alive”
【发布时间】:2014-06-28 17:19:18
【问题描述】:

我正在尝试在一个非常简单的express 应用程序中禁用keep-alive 设置。我试过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

【问题讨论】:

  • 你试过res.set("KeepAlive", false)吗?这是 Node nodejs.org/api/… 的文档……我对 express 不是很熟悉。
  • res.set('KeepAlive'...) 是错误的答案。它设置了一个新的头文件,名称为“KeepAlive”,值为“false”

标签: node.js express keep-alive


【解决方案1】:

它对我来说很好用。我在 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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-17
    • 1970-01-01
    • 1970-01-01
    • 2013-05-08
    • 2015-06-29
    • 2017-02-19
    相关资源
    最近更新 更多