欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

Ngnix upstream prematurely closed connection while reading response header from upstream, for large requests 有大用

39                    

我正在使用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;
                   

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

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;
}
                   

}                    

我正在使用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!');
});
}
                   

谢谢。                    

                      
改善这个问题                            
于2016年4月7日23:22编辑                                
16年4月7日23:02 问                                
                                   
                               
添加评论                
       

5答案                

活跃的最老                    
       
                       

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

改善这个答案                            
于16年4月7日23:08 回答                                
                                   
                               
  • 2                                    
    哦! 我发现我的节点服务器为大数据请求发送空响应。 -  Divya Konda 2016年 4月8日0:07                                    
  • 9                                    
    嗨@DivyaKonda,你能详细说明服务空响应如何导致网关超时错误吗? -  Melwyn Furtado 2016年 7月5日14:19                                    
  • 20                                    
    你是怎么解决的? -  Adams.H 2017年 4月21日2:41                                    
  • 7                                    
    你是怎么解决的,我有同样的问题。 -  gr68  2017年5月22日11:08                                    
  •                                    
    请显示问题的解决方案,以便其他人解决问题! - tryptofame 18年  2月5日23:32                                    
再显示3条评论                    
           
       
13                        

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

location / {
    proxy_read_timeout 300s;
    proxy_connect_timeout 75s;
    proxy_pass http://localhost:3000;
}
                       

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

改善这个答案                            
1月31日18:34编辑                                
18年2月19日0:48 回答                                
                                   
                               
  • 根据NGINX文档,连接超时不能超过75秒“定义与代理服务器建立连接的超时。应该注意,此超时通常不会超过75秒。” -  Richard Herries 1月30日8:11                                     
  • 谢谢@RichardHerries,我根据您的发现更新了答案。 -  低投入 1月31日18:35                                    
添加评论                    
       
2                        

您可以像这样增加节点中的超时。                        

app.post('/slow/request', function(req, res){   req.connection.setTimeout(100000); //100 seconds   ... }                        

改善这个答案                            
回答于18年9月11日20:26                                
                                   
                               
添加评论                    
       
1                        

我有相同的错误很长一段时间,这里有什么修复它给我。                        

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

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
                       

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

祝大家好运!                        

改善这个答案                            
18年10月30日21:53 回答                                
                                   
                               
添加评论                    
       
0                        

在我的情况下,我试图增加配置文件中的超时,但没有工作。后来证明它在过滤时显示在一个页面上的较少数据。在views.py中,我刚刚添加了“&Q(year = 2019)”以仅显示2019年的数据。顺便说一下,永久修复将使用分页。                        

def list_offers(request, list_type):
context = {}
context['list_type'] = list_type
if list_type == 'ready':
    context['menu_page'] = 'ready'
    offer_groups = OfferGroup.objects.filter(~Q(run_status=OfferGroup.DRAFT) & Q(year=2019)).order_by('-year', '-week')

context['grouped_offers'] = offer_groups

return render(request, 'app_offers/list_offers.html', context)
                   
改善这个答案                            
4月30日19:06 回答                                
                                   
                               
添加评论                    
       

来自 https://stackoverflow.com/questions/36488688/ngnix-upstream-prematurely-closed-connection-while-reading-response-header-from            


           


           

                       
39

I am using nginx and node server to serve update requests. I get a gateway timeout when I request an update on large data. I saw this error from the nginx error logs :

2016/04/07 00:46:04 [error] 28599#0: *1 upstream prematurely closed connection while reading response header from upstream, client: 10.0.2.77, server: gis.oneconcern.com, request: "GET /update_mbtiles/atlas19891018000415 HTTP/1.1", upstream: "http://127.0.0.1:7777/update_mbtiles/atlas19891018000415", host: "gis.oneconcern.com"

I googled for the error and tried everything I could, but I still get the error.

My nginx conf has these proxy settings:

    ##
    # Proxy settings
    ##

    proxy_connect_timeout 1000;
    proxy_send_timeout 1000;
    proxy_read_timeout 1000;
    send_timeout 1000;
                               

This is how my server is configured

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;
}
                               

}

I am using a nodejs backend to serve the requests on an aws server. The gateway error shows up only when the update takes a long time (about 3-4 minutes). I do not get any error for smaller updates. Any help will be highly appreciated.

Node js code :

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!');
});
}
                               

Thank you.

                                  
improve this question                                        
edited Apr 7 '16 at 23:22                                            
asked Apr 7 '16 at 23:02                                            
                                               
                                           
add a comment                            
                   

5 Answers

activeoldestvotes                                
                   
5

I think that error from Nginx is indicating that the connection was closed by your nodejs server (i.e., "upstream"). How is nodejs configured?

improve this answer                                        
answered Apr 7 '16 at 23:08                                            
                                               
                                           
  • 2                                                
    Oh! I found out that my node server sends empty response for big data requests. – Divya Konda Apr 8 '16 at 0:07                                                
  • 9                                                
    Hi @DivyaKonda, can you elaborate a bit more on how serving empty response caused gateway timeout errors please? – Melwyn Furtado Jul 5 '16 at 14:19                                                
  • 20                                                
    how did you solve this ? – Adams.H Apr 21 '17 at 2:41                                                
  • 7                                                
    how did you solve it , I have the same issue. – gr68 May 22 '17 at 11:08                                                
  • 5                                                
    Please show a solution to the problem, in order for others to fix the issue!! – tryptofame Feb 5 '18 at 23:32                                                
show 3 more comments                                
                   
13

I solved this by setting a higher timeout value for the proxy:

location / {
    proxy_read_timeout 300s;
    proxy_connect_timeout 75s;
    proxy_pass http://localhost:3000;
}
                                   

Documentation: https://nginx.org/en/docs/http/ngx_http_proxy_module.html                                    

improve this answer                                        
edited Jan 31 at 18:34                                            
answered Feb 19 '18 at 0:48                                            
                                               
                                           
  • According to NGINX docs the connect time out can't be longer than 75s "Defines a timeout for establishing a connection with a proxied server. It should be noted that this timeout cannot usually exceed 75 seconds." – Richard Herries Jan 30 at 8:11                                                 
  • Thanks @RichardHerries, i've updated the answer according to what you've found. – Lowinput Jan 31 at 18:35                                                
add a comment                                
                   
2

You can increase the timeout in node like so.

app.post('/slow/request', function(req, res){   req.connection.setTimeout(100000); //100 seconds   ... }                                    

improve this answer                                        
answered Sep 11 '18 at 20:26                                            
                                               
                                           
add a comment                                
                   
1

I had the same error for quite a while, and here what fixed it for me.

I simply declared in service that i use what follows:

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
                                   

What should catch your attention here is "After=network.target". I spent days and days looking for fixes on nginx side, while the problem was just that. To be sure, stop running the node service you have, launch the ExecStart command directly and try to reproduce the bug. If it doesn't pop, it just means that your service has a problem. At least this is how i found my answer.

For everybody else, good luck!

improve this answer                                        
answered Oct 30 '18 at 21:53                                            
                                               
                                           
add a comment                                
                   
0

In my case, I tried to increase the timeout in the configuration file, but did not work. Later turned out it was working when filtering for less data to display on one page. In the views.py, I just added " & Q(year=2019)" to only display the data for the year 2019. BTW, a permanent fix would be using Pagination.

def list_offers(request, list_type):
context = {}
context['list_type'] = list_type
if list_type == 'ready':
    context['menu_page'] = 'ready'
    offer_groups = OfferGroup.objects.filter(~Q(run_status=OfferGroup.DRAFT) & Q(year=2019)).order_by('-year', '-week')

context['grouped_offers'] = offer_groups

return render(request, 'app_offers/list_offers.html', context)
                               
improve this answer                                        
answered Apr 30 at 19:06                                            
                                               
                                           
add a comment                                
                   

来自 https://stackoverflow.com/questions/36488688/ngnix-upstream-prematurely-closed-connection-while-reading-response-header-from                        


           


普通分类: