1

How can I tune nginx to write request time to logs such that awstats can show time taken for each request?

Also, how can I see requests by time taken in awstats?

This is the current format

log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                  '$status $body_bytes_sent "$http_referer" '
                  '"$http_user_agent" "$http_x_forwarded_for"';
Quintin Par
  • 4,373
  • 11
  • 49
  • 72

2 Answers2

6

As @Vladimir mentioned, to logs request time, edit your log_format directive as belows:

log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"'
                      ' $request_time';

and set the corresponding variables for LogFormat in awstats configuration file:

LogFormat="%host %other %host_r %time1 %methodurl %code %bytesd %refererquot %uaquot %other %extra1"

Here is an extra1 section example:

ExtraSectionName1="Time to serve requests (seconds)"
ExtraSectionCodeFilter1=""
ExtraSectionFirstColumnTitle1="Number of seconds to serve the request"
ExtraSectionFirstColumnValues1="extra1,(.*)"
ExtraSectionStatTypes1="H"

Building report:

awstats.pl -config=model -output -staticlinks > awstats.localhost.html

enter image description here

quanta
  • 51,413
  • 19
  • 159
  • 217
2

You can use the $request_time variable, as specified here, e.g.:

log_format main '$remote_addr - $remote_user [$time_local] "$request" '
                '$status $body_bytes_sent $request_time "$http_referer" '
                '"$http_user_agent" "$http_x_forwarded_for"';

Edit: Oops, missed your second question. You can make AWStats parse the new format by using the LogFormat directive with an extraN variable and later use an ExtraSection to display the results.

Vladimir Blaskov
  • 6,183
  • 1
  • 27
  • 22