Nginx访问日志access_log配置及信息详解(推荐)
目录
- Nginx访问日志access_log配置及信息
 - log_format log_format语法格式及参数语法说明如下:
 - log_format格式变量:
 - access_log
 - 语法格式及参数语法说明如下:
 - open_log_file_cache
 - nginx日志调试技巧
 - 设置 Nginx 仅记录来自于你的 IP 的错误
 - 调试 nginx rewrite 规则
 - 使用location记录指定URL的日志
 - Nginx配置访问日志过程:
 - 常用例子
 - main格式
 - json格式
 - 压缩格式
 - upstream格式
 - 统计信息
 
Nginx访问日志access_log配置及信息
Nginx访问日志主要有两个参数控制:
log_format #用来定义记录日志的格式(可以定义多种日志格式,取不同名字即可)
access_log #用来指定日至文件的路径及使用的何种日志格式记录日志
# 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的默认值:
#access_log logs/access.log main;
log_format log_format语法格式及参数语法说明如下:
 log_format    <NAME>    <Strin---g>;
    关键字     格式标签   日志格式
    关键字:其中关键字error_log不能改变
    格式标签:格式标签是给一套日志格式设置一个独特的名字
    日志格式:给日志设置格式
作用域    :    httpeg:
log_format compression '$remote_addr - $remote_user [$time_local] '
                       '"$request" $status $bytes_sent '
                       '"$http_referer" "$http_user_agent" "$gzip_ratio"';
access_log /spool/logs/nginx-access.log compression buffer=32k;log_format格式变量:
参数 说明 示例 $remote_addr 客户端地址 211.28.65.253 $remote_user 客户端用户名称 -- $time_local 访问时间和时区 18/Jul/2012:17:00:01 +0800 $request 请求的URI和HTTP协议 "GET /article-10000.html HTTP/1.1" $http_host 请求地址,即浏览器中你输入的地址(IP或域名) www.wang.com 192.168.100.100 $status HTTP请求状态 200 $upstream_status upstream状态 200 $body_bytes_sent 发送给客户端文件内容大小 1547 $http_referer url跳转来源 https://www.baidu.com/ $http_user_agent 用户终端浏览器等信息 "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; SV1; GTB7.0; .NET4.0C; $ssl_protocol SSL协议版本 TLSv1 $ssl_cipher 交换数据中的算法 RC4-SHA $upstream_addr 后台upstream的地址,即真正提供服务的主机地址 10.10.10.100:80 $request_time 整个请求的总时间 0.205 $upstream_response_time 请求过程中,upstream响应时间 0.002
access_log
语法格式及参数语法说明如下:
    access_log    <FILE>    <NAME>;
    关键字         日志文件   格式标签
    关键字:其中关键字error_log不能改变
    日志文件:可以指定任意存放日志的目录
    格式标签:给日志文件套用指定的日志格式
其他语法:
    access_log    off;  #关闭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]  #表示其他条件
作用域(参数的标签段位置)   : http, server, location, if in location, limit_excepteg:
access_log /spool/logs/nginx-access.log compression buffer=32k;
open_log_file_cache
使用open_log_file_cache来设置日志文件缓存(默认是off)。
max:设置缓存中的最大文件描述符数量,如果缓存被占满,采用LRU算法将描述符关闭。
inactive:设置存活时间,默认是10s
min_uses:设置在inactive时间段内,日志文件最少使用多少次后,该日志文件描述符记入缓存中,默认是1次
valid:设置检查频率,默认60s
off:禁用缓存
语法格式:   open_log_file_cache max=N [inactive=time] [min_uses=N] [valid=time];
                     open_log_file_cache off;
默认值:     open_log_file_cache off;
作用域:     http, server, locationeg:
open_log_file_cache max=1000 inactive=20s valid=1m min_uses=2;
参考资料:http://nginx.org/en/docs/http/ngx_http_log_module.html
nginx日志调试技巧
设置 Nginx 仅记录来自于你的 IP 的错误
当你设置日志级别成 debug,如果你在调试一个在线的高流量网站的话,你的错误日志可能会记录每个请求的很多消息,这样会变得毫无意义。
在events{…}中配置如下内容,可以使 Nginx 记录仅仅来自于你的 IP 的错误日志。
events {
        debug_connection 1.2.3.4;
}调试 nginx rewrite 规则
调试rewrite规则时,如果规则写错只会看见一个404页面,可以在配置文件中开启nginx rewrite日志,进行调试。
server {
        error_log    /var/logs/nginx/example.com.error.log;
        rewrite_log on;
}rewrite_log on; 开启后,它将发送所有的 rewrite 相关的日志信息到 error_log 文件中,使用 [notice] 级别。随后就可以在error_log 查看rewrite信息了。
使用location记录指定URL的日志
server {
        error_log    /var/logs/nginx/example.com.error.log;
        location /static/ { 
        error_log /var/logs/nginx/static-error.log debug; 
    }         
}Nginx配置访问日志过程:
(1)创建log_format语句
worker_processes  1;
error_log logs/error.log error;
events {
    worker_connections  1024;
}
http {
    include status.conf;
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    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;
    server {
        listen       80;
        server_name  localhost;
                rewrite ^/.* http://www.wl.com permanent;
    }
    include vhost/*.conf;
}(2)插入access_log语句
server {
        access_log /data/log/www;
        listen 80;
        server_name abc.com www.wl.com;
        location / {
                root /data/www/www;
                index index.html index.htm;
        }
        error_log    logs/error_www.wl.com.log    error;
        access_log    logs/access_www.wl.com.log    main;
        #新增内容↑
}(3)重启服务
nginx -t nginx -s reload
常用例子
main格式
log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"'
                       '$upstream_addr $upstream_response_time $request_time ';
access_log  logs/access.log  main;json格式
log_format logstash_json '{"@timestamp":"$time_iso8601",'
       '"host": "$server_addr",'
       '"client": "$remote_addr",'
       '"size": $body_bytes_sent,'
       '"responsetime": $request_time,'
       '"domain": "$host",'
       '"url":"$request_uri",'
       '"referer": "$http_referer",'
       '"agent": "$http_user_agent",'
       '"status":"$status",'
       '"x_forwarded_for":"$http_x_forwarded_for"}';解释:
uri请求中的当前uri(不带请求参数,参数位于uri请求中的当前URI(不带请求参数,参数位于 uri请求中的当前URI(不带请求参数,参数位于args),不同于浏览器传递的$request_uri的值,它可以通过内部重定向,或者使用index指令进行修改。不包括协议和主机名,例如/foo/bar.html。
requesturi这个变量等于包含一些客户端请求参数的原始uri,它无法修改,请查看request—_uri这个变量等于包含一些客户端请求参数的原始URI,它无法修改,请查看 requestu?ri这个变量等于包含一些客户端请求参数的原始URI,它无法修改,请查看uri更改或重写URI。
也就是说:requesturi是原始请求url,request_uri是原始请求URL, requestu?ri是原始请求URL,uri则是经过nginx处理请求后剔除参数的URL,所以会将汉字表现为union。
坑点:
使用uri可以在nginx对url进行更改或重写,但是用于日志输出可以使用uri可以在nginx对URL进行更改或重写,但是用于日志输出可以使用 uri可以在nginx对URL进行更改或重写,但是用于日志输出可以使用request_uri代替,如无特殊业务需求,完全可以替换。
压缩格式
日志中增加了压缩的信息。
http {
    log_format compression '$remote_addr - $remote_user [$time_local] '
                           '"$request" $status $body_bytes_sent '
                           '"$http_referer" "$http_user_agent" "$gzip_ratio"';
    server {
        gzip on;
        access_log /spool/logs/nginx-access.log compression;
        ...
    }
}upstream格式
增加upstream消耗的时间。
http {
    log_format upstream_time '$remote_addr - $remote_user [$time_local] '
                             '"$request" $status $body_bytes_sent '
                             '"$http_referer" "$http_user_agent"'
                             'rt=$request_time uct="$upstream_connect_time" uht="$upstream_header_time" urt="$upstream_response_time"';
    server {
        access_log /spool/logs/nginx-access.log upstream_time;
        ...
    }
}统计信息
统计status 出现的次数
awk '{print $9}' access.log | sort | uniq -c | sort -rn
36461 200 
483 500
87 404
9 400
3 302
1 499
1 403
1 301显示返回302状态码的URL。
awk '($9 ~ /302/)' access.log | awk '{print $7}' | sort | uniq -c | sort -rn
1 /wp-login.php
1 /wp-admin/plugins.php?action=activate&plugin=ewww-image-optimizer%2Fewww-image-optimizer.php&_wpnonce=cc4a379131
1 /wp-admin/到此这篇关于Nginx访问日志access_log配置及信息详解的文章就介绍到这了,更多相关Nginx访问日志access_log内容请搜索电脑手机教程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持电脑手机教程网!