How can request_time be less than upstream_respons

2020-07-16 02:20发布

问题:

I understand how nginx's request_time can be larger than upstream_response_time, it simply means that the network connection was slow between nginx and the client.

What I don't understand is how request_time can be less?

I've analysed an nginx log where nginx is in front of an API. There were about 2.6 millions lines, thus I believe it's a good sample (only API requests were analysed, no static files.)

Ratios were calculated like: ratio = request_time / upstream_response_time

The mean of the ratios is 1.04, thus on average request_time is a tiny bit larger than upstream_response_time, which sounds reasonable.

I made a histogram to visualise this. What I don't understand is the left side of the histogram, where values are < 1.0.

回答1:

$upstream_response_time calculated by clock_gettime(CLOCK_MONOTONIC_COARSE), and by default it can be in past for 4 milliseconds, on opposite, $request_time calculated by gettimeofday(). So eventually upstream_response_time might be larger, than response_time.

Based on nginx forum thread