nginx優化之request_time 和upstream_response_time差別

筆者在根據nginx的accesslog中$request_time進行程序優化時,發現有個接口,直接返回數據,平均的$request_time也比較大。原來$request_time包含了用戶數據接收時間,而真正程序的響應時間應該用$upstream_response_time。

下面介紹下2者的差別:

1、request_time
官網描述:request processing time in seconds with a milliseconds resolution; time elapsed between the first bytes were read from the client and the log write after the last bytes were sent to the client 。

指的就是從接受用戶請求的第一個字節到發送完響應數據的時間,即包括接收請求數據時間、程序響應時間、輸出
響應數據時間。


2、upstream_response_time
官網描述:keeps times of responses obtained from upstream servers; times are kept in seconds with a milliseconds resolution. Several response times are separated by commas and colons like addresses in the $upstream_addr variable

是指從Nginx向後端(php-cgi)建立連接開始到接受完數據然後關閉連接爲止的時間。

從上面的描述可以看出,$request_time肯定比$upstream_response_time值大,特別是使用POST方式傳遞參數時,因爲Nginx會把request body緩存住,接受完畢後纔會把數據一起發給後端。所以如果用戶網絡較差,或者傳遞數據較大時,$request_time會比$upstream_response_time大很多。

所以如果使用nginx的accesslog查看php程序中哪些接口比較慢的話,記得在log_format中加入$upstream_response_time。


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章