[[ Оптимизация страниц ]]

Оптимизация страниц

С помощью nginx можно определить какие страницы сайта являются более тяжёлыми при отдаче их клиенту.

Настройка формата лога:

nginx.conf

log_format my_combined	'$remote_addr - $remote_user [$time_local] '
			'"$request" $status $body_bytes_sent '
			'"$http_referer" "$http_user_agent" '
			'$upstream_response_time "$host"'
			
access_log /var/log/nginx/access_log my_combined;

url_stats_report.sh

#!/bin/sh
 
echo "=== Requests which took most of the time ===" > /tmp/report.txt
echo "overall time - number of requests - average time - url" >> /tmp/report.txt
 
cat /var/log/nginx/*access.log 	| url_stats.py  >> /tmp/report.txt
 
cat /tmp/report.txt | mail -s "url performance report" root

url_stats.py

#!/usr/bin/env python
 
import sys
 
urls = {}
 
try:
	while 1:
		line = raw_input()
		line_arr = line.split(" ")
	try:
		host = line_arr[-1]
		host = host[1:]
		host = host[:-1]
		url = line_arr[6]
		t = float(line_arr[-2])
		#print host, url, t
 
		try:
			urls[host + url] = (urls[host + url][0] + t, urls[host + url][1] + 1)
		except KeyError, e:
			urls[host + url] = (t, 1)
	except ValueError, e:
		pass
 
except EOFError, e:
	pass
 
def sort_by_value(d):
	""" Returns the keys of dictionary d sorted by their values """
	items=d.items()
	backitems=[ [v[1],v[0]] for v in items]
	backitems.sort(reverse=True)
	return [backitems[i][1] for i in range(0,len(backitems))]
	if (len(sys.argv) > 1):
		f = open(sys.argv[1], 'r')
		for k in f.readlines():
			k = k.strip()
		try:
			print urls[k][0], urls[k][1], urls[k][0] / urls[k][1], k
		except:
			print 0, 0, k
	else:
		i = 0
		for k in sort_by_value(urls):
			print urls[k][0], urls[k][1], urls[k][0] / urls[k][1], k
			i += 1
			if i > 100: break

Обсуждение

Ваш комментарий. Вики-синтаксис разрешён:
34 -2 = 
 
practice/nginx/оптимизация_страниц.txt · Последнее изменение: 2022/08/31 14:53 — 127.0.0.1
Gentoo Linux Gentoo Linux Driven by DokuWiki