1.语法:
Syntax: log_format name [escape=default|json|none] string ...;
Default: log_format combined "...";
Context: http //只能定义在http标签里面,server,location
2.默认日志格式:
#vim /usr/local/nginx/conf/nginx.conf
http {
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;
error_log logs/error.log;
}
注意:如果定义在server里面只对当前server生效,如果定义在http内server外对所有的server生效
3.nginx日志变量:
$remote_addr //表示客户端地址
$remote_user //http客户端请求nginx认证⽤户名
$time_local //Nginx的时间
$request //Request请求⾏,GET等⽅法http协议版本
$status //respoence返回状态码
$body_bytes_sent //从服务端响应给客户端body信息⼤⼩
$http_referer //http上⼀级页⾯,防盗链、⽤户⾏为分析
$http_user_agent //http头部信息, 客户端访问设备
$http_x_forwarded_for //http请求携带的http信息
4.验证:
#nginx -t
#nginx -s reload
# tail -1 /usr/local/nginx/logs/access.log
192.168.1.252 - - [17/May/2018:16:33:05 +0800] "GET /favicon.ico HTTP/1.1" 404 539 "-" "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:59.0) Gecko/20100101 Firefox/59.0" "-"