Custom Access log

Note boldface
[the root @ CentOS7 the conf] #pwd
/ Apps / Nginx / the conf
[the root @ CentOS7 the conf] #vim Server / www_yunlong_net.conf

1 server {
2 listen 80;
3 server_name www.yunlong.net;
4 access_log /apps/nginx/logs/www_yunlong_net_access.log;
5 error_log /apps/nginx/logs/www_yunlong_net_error.log;
6
7 location / {
8 root /data/nginx/html/pc;
9 index index.html;
10 }
11 location /image {
12 index index.html;
13 root /data/nginx/html/pc;
14 }
15 location /login {
16 index index.html;
17 root /data/nginx/html/pc;
18 auth_basic "login password";
19 auth_basic_user_file /apps/nginx/conf/.htpasswd;
20 }
21 }

www.yunlong.net log classification stored at blackbody path

By accessing www.yunlong.net
tail -f /apps/nginx/logs/www_yunlong_net_access.log can view the log

Into the main configuration file, added at http json custom log format
http {
the include the mime.types;
default_type file application / OCTET-Stream;

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

#access_log  logs/access.log  main;

log_format access_json '{"@timestamp":"$time_iso8601",'
 '"host":"$server_addr",'
 '"clientip":"$remote_addr",'
 '"size":$body_bytes_sent,'
 '"responsetime":$request_time,'
 '"upstreamtime":"$upstream_response_time",'
 '"upstreamhost":"$upstream_addr",'
 '"http_host":"$host",'
 '"uri":"$uri",'
 '"domain":"$host",'
 '"xff":"$http_x_forwarded_for",'
 '"referer":"$http_referer",'
 '"tcp_xff":"$proxy_protocol_addr",'
 '"http_user_agent":"$http_user_agent",'
 '"status":"$status"}';
access_log  /apps/nginx/logs/access_json.log  access_json;        

进入辅助配置文件添加access_json
server{
2 listen 80;
3 server_name www.yunlong.net;
4
5 access_log /apps/nginx/logs/www_yunlong_net_access.log access_json;
6
7 location /echo {
8 echo hello;
9 default_type text/html;
10 }
11
12 location /main {
13 index index.html;
14 default_type text/html;
15
16 set $name yunlong;
17 echo $name;
18 }
19
20 location /sub1 {
21 echo_sleep 1;
22 echo sub1;
23 }
24
25 location /sub2 {
26 echo_sleep 1;
27 echo sub2;
28 }
29
30 }

Importing a json format of the log file access_json.log
write a script py
vim log.py
# / usr / bin / env Python!
#Coding: UTF-8
#Author: xxx
status_200 = []
status_404 = []
with Open ( "access_json. log ") AS F:
for in f.readlines Line ():
Line = the eval (Line)
IF line.get (" Status ") ==" 200 is ":
status_200.append (line.get)
elif line.get (" status ") ==" 404 ":
status_404.append (line.get)
the else:
Print (" status code ERROR ")
f.close ()

print "with a status code of 200 -", len (status_200)
print "with a status code of 404 -", len (status_404)

Execute scripts
python log.py

Guess you like

Origin blog.51cto.com/14231434/2403964