天天看點

關于nginx keep-alive 參數的驗證和心得

用chrome連接配接nginx伺服器(nginx+spero),發現每次請求結果傳回給浏覽器後,會過一會才會運作

ngx_http_close_connection函數,可以看到nginx傳回給chrome的header和結果是:

HTTP/1.1 200 OK

Server: nginx

Date: Fri, 15 Apr 2016 08:39:50 GMT

Content-Type: text/plain

Content-Length: 28

Connection: keep-alive

Keep-Alive: timeout=5

spero return ads, status 200

而通過curl通路,也是傳回同樣的結果,但是nginx會立刻調用ngx_http_close_connection函數,看起來keep-alive沒有起作用,猜測是curl拿到結果後立馬主動關閉連接配接。

Date: Fri, 15 Apr 2016 08:44:11 GMT

那麼做一個實驗:設定nginx的配置檔案,将keep-alive關掉,看看chrome通路時是否ngx_http_close_connection函數立刻被調用?

首先,用指令:keepalive_timeout 0 禁用長連接配接,則看到header中的Connection為close

Date: Fri, 15 Apr 2016 08:50:05 GMT

Connection: close

同時,在nginx print的log中也可以看到,ngx_http_finalize_request函數之後,ngx_http_close_connection函數立刻就被調用了。

在spero項目中,長連接配接必須被關閉以支援大并發請求。

本文轉自 zhegaozhouji 51CTO部落格,原文連結:http://blog.51cto.com/1038741/1764232

繼續閱讀