Nginx访问日志(access_log)
Nginx访问日志介绍
Nginx软件会把每个用户访问网站的日志信息记录到指定的日志文件里,供网站提供者分析用户的浏览行为等,此功能由ngx_http_log_module模块负责。对应的官方地址为:http://nginx.org/en/docs/http/ngx_http_log_module.html。
访问日志参数
Nginx的访问日志主要由两个参数控制。
Nginx日志格式中默认的参数配置如下:
log_format main '$remote_addr - $remote_user[$time_local]"$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
Nginx记录日志的默认参数配置如下:
access_log logs/access.log main;
访问日志配置说明
1.日志格式的定义说明
先来看其语法:
定义语法: log_format name string ……;
其配置位置在http标签内。
日志格式说明如下:
log_format main '$remote_addr - $remote_user[$time_local]"$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
其中,log_format为日志格式关键参数,不能变。
main是为日志格式指定的标签,记录日志时通过这个main标签选择指定的格式。其后所接的所有内容都是可以记录的日志信息,具体见表5-5。注意,所有的日志段以空格分隔,一行可以记录多个,不同列的意义:
在没有特殊要求的情况下,采用默认的配置即可,更多可以设置的记录日志信息的变量见:
http://nginx.org/en/docs/http/ngx_http_log_module.html
2.记录日志的access_log参数说明
下面是有关access_log参数的官方说明。
语法如下:
access_log path[format[buffer=size[flush=time]][if=condition]];
access_log path format gzip[=level][buffer=size][flush=time][if=condition];
access_log syslog:server=address[,parameter=value][format[if=condition]];
buffer=size为存放访问日志的缓冲区大小,flush=time为将缓冲区的日志刷到磁盘的时间,gzip[=level]表示压缩级别,[if=condition]表示其他条件。一般的场景中,这些参数都无须配置,极端优化时才可能会考虑这些参数。
access_log off中的off,表示不记录访问日志。
默认配置:access_log logs/access.log combined;
放置位置在http、server、location、if in location、limit_except中。
访问日志配置实战
编辑主配置文件 nginx.conf ,配置日志格式如下:
[root@private conf]# sed -n '21, 23 s/#//gp' nginx.conf.default
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
[root@private conf]# vim nginx.conf
[root@private conf]# cat -n nginx.conf
1
2 #user nobody;
3 worker_processes 1;
4
5 error_log logs/error.log error;
6 error_log logs/error.log warn;
7 error_log logs/error.log crit;
8
9 #pid logs/nginx.pid;
10
11
12 events {
13 worker_connections 1024;
14 }
15
16
17 http {
18 include mime.types;
19 default_type application/octet-stream;
20
21 log_format main '$remote_addr - $remote_user [$time_local] "$request" '
22 '$status $body_bytes_sent "$http_referer" '
23 '"$http_user_agent" "$http_x_forwarded_for"';
24 sendfile on;
25
26 keepalive_timeout 65;
27
28 include extra/*.conf;
29
30 }
31
[root@private conf]#
然后在每个虚拟主机里进行配置,使其商用上述格式记录用户访问日志。命令如下:
[root@private conf]# vim extra/blog.conf
[root@private conf]# cat -n extra/blog.conf
1 server {
2 listen 80;
3 server_name blog.haiyuan.org;
4
5 location / {
6 root html/blog;
7 index index.html index.htm;
8 }
9
10 error_page 404 /404.html;
11 location = /404.html {
12 root html/blog;
13 }
14
15 error_page 500 502 503 504 /50x.html;
16 location = /50x.html {
17 root html/blog;
18 }
19 access_log logs/access_blog.log main;
20 }
如果不指定日志格式就会用默认的combined格式记录日志。
接下来进行检查语法,重新加载配置,命令如下:
[root@private conf]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@private conf]# /etc/init.d/nginx reload
reload nginx [OK]
[root@private conf]#
进行测试访问,查看日志结果:
[root@private conf]# curl -I blog.haiyuan.org
HTTP/1.1 200 OK
Server: nginx/1.12.0
Date: Sat, 21 Oct 2017 06:39:45 GMT
Content-Type: text/html
Content-Length: 9480
Last-Modified: Sat, 02 Sep 2017 07:31:30 GMT
Connection: keep-alive
ETag: "59aa5e52-2508"
Accept-Ranges: bytes
[root@private conf]# ls -l ../logs/access_blog.log
-rw-r--r-- 1 root root 904 Oct 21 14:39 ../logs/access_blog.log
[root@private conf]# tailf -5 ../logs/access_blog.log
127.0.0.1 - - [21/Oct/2017:14:38:56 +0800] "GET / HTTP/1.1" 200 9480 "-" "curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.14.0.0 zlib/1.2.3 libidn/1.18 libssh2/1.4.2" "-"
127.0.0.1 - - [21/Oct/2017:14:39:07 +0800] "GET / HTTP/1.1" 200 9480 "-" "curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.14.0.0 zlib/1.2.3 libidn/1.18 libssh2/1.4.2" "-"
再分别使用谷歌和火狐浏览器进行测试访问,分别查看日志结果:
火狐浏览器访问后:
[root@private conf]# tailf -5 ../logs/access_blog.log
119.90.20.254 - - [21/Oct/2017:14:44:32 +0800] "GET /images/templatemo_180_column_bg.jpg HTTP/1.1" 200 2140 "http://114.115.155.144/templatemo_style.css" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.12; rv:56.0) Gecko/20100101 Firefox/56.0" "-"
119.90.20.254 - - [21/Oct/2017:14:44:32 +0800] "GET /images/templatemo_bottom_panel_bg.jpg HTTP/1.1" 200 27926 "http://114.115.155.144/templatemo_style.css" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.12; rv:56.0) Gecko/20100101 Firefox/56.0" "-"
119.90.20.254 - - [21/Oct/2017:14:44:32 +0800] "GET /images/templatemo_top_bg.jpg HTTP/1.1" 200 57124 "http://114.115.155.144/templatemo_style.css" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.12; rv:56.0) Gecko/20100101 Firefox/56.0" "-"
119.90.20.254 - - [21/Oct/2017:14:44:33 +0800] "GET /favicon.ico HTTP/1.1" 404 19 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.12; rv:56.0) Gecko/20100101 Firefox/56.0" "-"
119.90.20.254 - - [21/Oct/2017:14:44:33 +0800] "GET /favicon.ico HTTP/1.1" 404 19 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.12; rv:56.0) Gecko/20100101 Firefox/56.0" "-"
谷歌浏览器访问后:
[root@private conf]# tailf -5 ../logs/access_blog.log
119.90.20.254 - - [21/Oct/2017:14:44:32 +0800] "GET /images/templatemo_top_bg.jpg HTTP/1.1" 200 57124 "http://114.115.155.144/templatemo_style.css" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.12; rv:56.0) Gecko/20100101 Firefox/56.0" "-"
119.90.20.254 - - [21/Oct/2017:14:44:33 +0800] "GET /favicon.ico HTTP/1.1" 404 19 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.12; rv:56.0) Gecko/20100101 Firefox/56.0" "-"
119.90.20.254 - - [21/Oct/2017:14:44:33 +0800] "GET /favicon.ico HTTP/1.1" 404 19 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.12; rv:56.0) Gecko/20100101 Firefox/56.0" "-"
119.90.20.254 - - [21/Oct/2017:14:45:09 +0800] "GET / HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36" "-"
119.90.20.254 - - [21/Oct/2017:14:45:09 +0800] "GET /images/templatemo_main_bg.jpg HTTP/1.1" 404 19 "http://114.115.155.144/templatemo_style.css" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36" "-"
我们可以将日志格式和日志内容做一个比对:
'$remote_addr - $remote_user[$time_local]"$request" ' '$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
真实的日志内容如下:
119.90.20.254 - - [21/Oct/2017:14:44:32 +0800] "GET /images/templatemo_180_column_bg.jpg HTTP/1.1" 200 2140 "http://114.115.155.144/templatemo_style.css" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.12; rv:56.0) Gecko/20100101 Firefox/56.0" "-"
对应说明如下:
$remote_addr对应的是真实日志里的119.90.20.254,即客户端的IP。
$remote_user对应的是第二个中杠“-”,没有远程用户,所以用“-”填充。
[$time_local]对应的是[21/Oct/2017:14:44:32 +0800]。
“$request”对应的是”GET /images/templatemo_180_column_bg.jpg HTTP/1.1”。
$status对应的是200状态码,200表示正常访问。
$body_bytes_sent对应的是2140字节,即响应body的大小。
“$http_referer”对应的是”http://114.115.155.144/templatemo_style.css“,若是直接打开域名浏览的时,referer就会没有值,为”-“。
“$http_user_agent”对应的是”Mozilla/5.0 (Macintosh; Intel Mac OS X 10.12; rv:56.0) Gecko/20100101 Firefox/56.0”。
“$http_x_forwarded_for”对应的是”-“,因为Web服务没有使用代理,因此此处为”-“。