Recently we noticed some intermittent slow downs with our Gondola sites and wanted to track down the source of the issue. Our sites are all Django projects served by Apache/mod_wsgi behind an Nginx frontend proxy. Nginx’s upstream module makes the process of logging response times trivial with its upstream_response_time variable.
I added this block to
/etc/nginx/nginx.conf to define a new log format
timed_combined which captures some additional data in our logs:
log_format timed_combined '$remote_addr - $remote_user [$time_local] ' '"$request" $status $body_bytes_sent ' '"$http_referer" "$http_user_agent" ' '$request_time $upstream_response_time $pipe';
combinedformat<sup class="footnote" id="fnrevf77907a8d8ce4c0ca66db9a68a60d5c8-1"><a href="#fnf77907a8d8ce4c0ca66db9a68a60d5c8-1">1</a></sup> with the three variables appended to it. <ul> <li>
request_timeThis shows how long Nginx dealt with the request</li> <li>
upstream_response_timeGives us the time it took our upstream server (in this case Apache/mod_wsgi) to respond</li> <li>
pipeShows ‘p’ in case the request was pipelined.</li> </ul> Next we modify our
access_logdirectives to use the new format:
access_log /var/log/nginx/yourdomain.com.access.log timed_combined;
184.108.40.206 - - [08/Nov/2010:14:16:18 -0600] "GET /blog/2010/apr/30/installing-geodjango-dependencies-homebrew/ HTTP/1.1" 200 6569 "-" "Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)" 0.640 0.640 .In other words, the Googlebot spidered our blog post at
220.127.116.11at 2:16pm. It took 0.640 seconds to serve (all of those came waiting for the upstream proxy) and the request was not pipelined. Some clever log filtering can now show you exactly where and when your Django stack is slowing down, giving you clues about where to start optimizing. In addition, Nginx’s logging can be used to capture all sorts of interesting information such as cookies, http headers etc. You’ll find documentation of the core variables here and other modules spread throughout the rest of the docs.