Ngnix上游过早关闭连接,同时从上游读取响应头,用于大量请求

Div*_*nda 48 webserver nginx node.js

我正在使用nginx和节点服务器来提供更新请求.当我请求更新大数据时,我得到网关超时.我从nginx错误日志中看到了这个错误:

2016/04/07 00:46:04 [错误] 28599#0:*1上游过早关闭连接,同时从上游读取响应头,客户端:10.0.2.77,服务器:gis.oneconcern.com,请求:"GET/update_mbtiles/atlas19891018000415 HTTP/1.1",上游:" http://127.0.0.1:7777/update_mbtiles/atlas19891018000415 ",主持人:"gis.oneconcern.com"

我用谷歌搜索错误并尽我所能,但我仍然得到错误.

我的nginx conf有这些代理设置:

    ##
    # Proxy settings
    ##

    proxy_connect_timeout 1000;
    proxy_send_timeout 1000;
    proxy_read_timeout 1000;
    send_timeout 1000;
Run Code Online (Sandbox Code Playgroud)

这就是我的服务器的配置方式

server {
listen 80;

server_name gis.oneconcern.com;
access_log /home/ubuntu/Tilelive-Server/logs/nginx_access.log;
error_log /home/ubuntu/Tilelive-Server/logs/nginx_error.log;

large_client_header_buffers 8 32k;
location / {
    proxy_pass http://127.0.0.1:7777;
    proxy_redirect off;

    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection 'upgrade';
    proxy_set_header Host $http_host;
    proxy_cache_bypass $http_upgrade;
}

location /faults {
    proxy_pass http://127.0.0.1:8888;
    proxy_http_version 1.1;
    proxy_buffers 8 64k;
    proxy_buffer_size 128k;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection 'upgrade';
    proxy_set_header Host $host;
    proxy_cache_bypass $http_upgrade;
}
Run Code Online (Sandbox Code Playgroud)

}

我正在使用nodejs后端来为aws服务器上的请求提供服务.仅当更新需要很长时间(大约3-4分钟)时才会显示网关错误.对于较小的更新,我不会收到任何错误.任何帮助将受到高度赞赏.

节点js代码:

app.get("/update_mbtiles/:earthquake", function(req, res){
var earthquake = req.params.earthquake
var command = spawn(__dirname + '/update_mbtiles.sh', [ earthquake, pg_details ]);
//var output  = [];

command.stdout.on('data', function(chunk) {
//    logger.info(chunk.toString());
//     output.push(chunk.toString());
});

command.stderr.on('data', function(chunk) {
  //  logger.error(chunk.toString());
 //   output.push(chunk.toString());
});

command.on('close', function(code) {
    if (code === 0) {
        logger.info("updating mbtiles successful for " + earthquake);
        tilelive_reload_and_switch_source(earthquake);
        res.send("Completed updating!");
    }
    else {
        logger.error("Error occured while updating " + earthquake);
        res.status(500);
        res.send("Error occured while updating " + earthquake);
    }
});
});

function tilelive_reload_and_switch_source(earthquake_unique_id) {
tilelive.load('mbtiles:///'+__dirname+'/mbtiles/tipp_out_'+ earthquake_unique_id + '.mbtiles', function(err, source) {
    if (err) {
        logger.error(err.message);
        throw err;
    }
    sources.set(earthquake_unique_id, source); 
    logger.info('Updated source! New tiles!');
});
}
Run Code Online (Sandbox Code Playgroud)

谢谢.

Low*_*put 13

我通过为代理设置更高的超时值来解决这个问题:

location / {
    proxy_read_timeout 300s;
    proxy_connect_timeout 75s;
    proxy_pass http://localhost:3000;
}
Run Code Online (Sandbox Code Playgroud)

文档:https://nginx.org/en/docs/http/ngx_http_proxy_module.html

  • 根据 NGINX 文档,连接超时不能超过 75 秒“定义与代理服务器建立连接的超时。应该注意的是,此超时通常不能超过 75 秒。” (6认同)

小智 8

我认为来自Nginx的错误表明连接已被nodejs服务器关闭(即"上游").nodejs是如何配置的?

  • 嗨@DivyaKonda,你能详细说明服务空响应如何导致网关超时错误吗? (10认同)
  • 可能是您的nodejs服务器超时(默认2分钟),超时时服务器将发送空响应。doc:https://nodejs.org/api/http.html#http_server_settimeout_msecs_callback (3认同)
  • 哦! 我发现我的节点服务器为大数据请求发送空响应. (2认同)

mil*_*ion 6

我有一段时间有同样的错误,这里是什么为我修复了它。

我只是在服务中声明我使用以下内容:

Description= Your node service description
After=network.target

[Service]
Type=forking
PIDFile=/tmp/node_pid_name.pid
Restart=on-failure
KillSignal=SIGQUIT
WorkingDirectory=/path/to/node/app/root/directory
ExecStart=/path/to/node /path/to/server.js

[Install]
WantedBy=multi-user.target
Run Code Online (Sandbox Code Playgroud)

这里应该引起您注意的是“After=network.target”。我花了几天时间在 nginx 方面寻找修复程序,而问题就在于此。可以肯定的是,停止运行您拥有的节点服务,直接启动 ExecStart 命令并尝试重现该错误。如果它没有弹出,则仅表示您的服务有问题。至少这是我找到答案的方式。

对于其他人,祝你好运!

  • 经过进一步调查,它看起来像是一个“systemd”配置文件,并且“After=network.target”设置试图延迟该nodejs服务的启动,直到系统网络启动并运行之后。 (2认同)

Tom*_*ski 6

*145660 upstream prematurely closed connection while reading upstream当我尝试从 Nginx 代理的服务器下载 2GB 文件时,我偶然发现了Nginx 错误日志条目。该消息表明“上游”关闭了连接,但实际上它与proxy_max_temp_file_size设置有关:

语法:proxy_max_temp_file_size大小;
默认值:proxy_max_temp_file_size 1024m;
上下文:http、服务器、位置

当启用对来自代理服务器的响应进行缓冲,并且整个响应不适合 proxy_buffer_size 和 proxy_buffers 指令设置的缓冲区时,可以将部分响应保存到临时文件中。该指令设置临时文件的最大大小。一次写入临时文件的数据大小由 proxy_temp_file_write_size 指令设置。

零值禁用对临时文件的响应的缓冲。

此限制不适用于将缓存或存储在磁盘上的响应。

症状:

  • 下载在 1GB 左右被强制停止,
  • Nginx 声称上游关闭了连接,但没有代理服务器正在返回完整内容。

解决方案:

  • 增加proxy_max_temp_file_size代理位置4096m并开始发送完整内容。


Chr*_*ger 6

当我尝试将大约 50 万行发布到我的 api 时,我在 AWS Elastic Beanstalk 实例的日志中发现了此错误。

我遵循了这里的所有建议,但没有效果。

最终起作用的是将我的 EC2 实例的大小从 1 核和 1GB RAM 增加到 4 核和 8 GB RAM。

  • 这也为我解决了这个问题;我正在尝试在具有 1GB RAM 的虚拟机上进行复杂查询。我的解决方案是把它增加到 2GB。检查您的服务器是否有足够的内存! (2认同)