翼度科技»论坛 云主机 服务器技术 查看内容

Nginx访问日志access_log配置及信息详解(推荐)

4

主题

4

帖子

12

积分

新手上路

Rank: 1

积分
12
Nginx访问日志access_log配置及信息

Nginx访问日志主要有两个参数控制:
log_format #用来定义记录日志的格式(可以定义多种日志格式,取不同名字即可)
access_log #用来指定日至文件的路径及使用的何种日志格式记录日志
  1. #    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
  2. #                      '$status $body_bytes_sent "$http_referer" '
  3. #                      '"$http_user_agent" "$http_x_forwarded_for"';
复制代码
access_log的默认值:
  1. #access_log  logs/access.log  main;
复制代码
log_format log_format语法格式及参数语法说明如下:
  1. log_format    <NAME>    <Strin­­­g>;
  2.     关键字     格式标签   日志格式
  3.     关键字:其中关键字error_log不能改变
  4.     格式标签:格式标签是给一套日志格式设置一个独特的名字
  5.     日志格式:给日志设置格式
  6. 作用域    :    http
复制代码
eg:
  1. log_format compression '$remote_addr - $remote_user [$time_local] '
  2.                        '"$request" $status $bytes_sent '
  3.                        '"$http_referer" "$http_user_agent" "$gzip_ratio"';
  4. access_log /spool/logs/nginx-access.log compression buffer=32k;
复制代码
log_format格式变量:
  1. 参数                      说明                                         示例
  2. $remote_addr             客户端地址                                    211.28.65.253
  3. $remote_user             客户端用户名称                                --
  4. $time_local              访问时间和时区                                18/Jul/2012:17:00:01 +0800
  5. $request                 请求的URI和HTTP协议                           "GET /article-10000.html HTTP/1.1"
  6. $http_host               请求地址,即浏览器中你输入的地址(IP或域名)     www.wang.com 192.168.100.100
  7. $status                  HTTP请求状态                                  200
  8. $upstream_status         upstream状态                                  200
  9. $body_bytes_sent         发送给客户端文件内容大小                        1547
  10. $http_referer            url跳转来源                                   https://www.baidu.com/
  11. $http_user_agent         用户终端浏览器等信息                           "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; SV1; GTB7.0; .NET4.0C;
  12. $ssl_protocol            SSL协议版本                                   TLSv1
  13. $ssl_cipher              交换数据中的算法                               RC4-SHA
  14. $upstream_addr           后台upstream的地址,即真正提供服务的主机地址     10.10.10.100:80
  15. $request_time            整个请求的总时间                               0.205
  16. $upstream_response_time  请求过程中,upstream响应时间                    0.002
复制代码
access_log


语法格式及参数语法说明如下:
  1.     access_log    <FILE>    <NAME>;
  2.     关键字         日志文件   格式标签
  3.     关键字:其中关键字error_log不能改变
  4.     日志文件:可以指定任意存放日志的目录
  5.     格式标签:给日志文件套用指定的日志格式
  6. 其他语法:
  7.     access_log    off;  #关闭access_log,即不记录访问日志
  8.     access_log path [format [buffer=size [flush=time]] [if=condition]];
  9.     access_log path format gzip[=level] [buffer=size] [flush=time] [if=condition];
  10.     access_log syslog:server=address[,parameter=value] [format [if=condition]];
  11.     说明:
  12.     buffer=size  #为存放访问日志的缓冲区大小
  13.     flush=time  #为缓冲区的日志刷到磁盘的时间
  14.     gzip[=level]  #表示压缩级别
  15.     [if = condition]  #表示其他条件
  16. 作用域(参数的标签段位置)   : http, server, location, if in location, limit_except
复制代码
eg:
  1. 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:禁用缓存
  1. 语法格式:   open_log_file_cache max=N [inactive=time] [min_uses=N] [valid=time];
  2.                      open_log_file_cache off;
  3. 默认值:     open_log_file_cache off;
  4. 作用域:     http, server, location
复制代码
eg:
  1. open_log_file_cache max=1000 inactive=20s valid=1m min_uses=2;
复制代码
  1. 参考资料:<a href="http://nginx.org/en/docs/http/ngx_http_log_module.html" rel="external nofollow"  target="_blank">http://nginx.org/en/docs/http/ngx_http_log_module.html</a>
复制代码
nginx日志调试技巧


设置 Nginx 仅记录来自于你的 IP 的错误

当你设置日志级别成 debug,如果你在调试一个在线的高流量网站的话,你的错误日志可能会记录每个请求的很多消息,这样会变得毫无意义。
在events{…}中配置如下内容,可以使 Nginx 记录仅仅来自于你的 IP 的错误日志。
  1. events {
  2.         debug_connection 1.2.3.4;
  3. }
复制代码
调试 nginx rewrite 规则

调试rewrite规则时,如果规则写错只会看见一个404页面,可以在配置文件中开启nginx rewrite日志,进行调试。
  1. server {
  2.         error_log    /var/logs/nginx/example.com.error.log;
  3.         rewrite_log on;
  4. }
复制代码
rewrite_log on; 开启后,它将发送所有的 rewrite 相关的日志信息到 error_log 文件中,使用 [notice] 级别。随后就可以在error_log 查看rewrite信息了。

使用location记录指定URL的日志
  1. server {
  2.         error_log    /var/logs/nginx/example.com.error.log;
  3.         location /static/ {
  4.         error_log /var/logs/nginx/static-error.log debug;
  5.     }         
  6. }
复制代码
Nginx配置访问日志过程:

(1)创建log_format语句
  1. worker_processes  1;
  2. error_log logs/error.log error;
  3. events {
  4.     worker_connections  1024;
  5. }
  6. http {
  7.     include status.conf;
  8.     include       mime.types;
  9.     default_type  application/octet-stream;
  10.     sendfile        on;
  11.     keepalive_timeout  65;
  12.     log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
  13.                                '$status $body_bytes_sent "$http_referer" '
  14.                                '"$http_user_agent" "$http_x_forwarded_for"';
  15.     access_log  logs/access.log  main;
  16.     server {
  17.         listen       80;
  18.         server_name  localhost;
  19.                 rewrite ^/.* http://www.wl.com permanent;
  20.     }
  21.     include vhost/*.conf;
  22. }
复制代码
(2)插入access_log语句
  1. server {
  2.         access_log /data/log/www;
  3.         listen 80;
  4.         server_name abc.com www.wl.com;
  5.         location / {
  6.                 root /data/www/www;
  7.                 index index.html index.htm;
  8.         }
  9.         error_log    logs/error_www.wl.com.log    error;
  10.         access_log    logs/access_www.wl.com.log    main;
  11.         #新增内容↑
  12. }
复制代码
(3)重启服务
  1. nginx -t
  2. nginx -s reload
复制代码
常用例子


main格式
  1. log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
  2.                       '$status $body_bytes_sent "$http_referer" '
  3.                       '"$http_user_agent" "$http_x_forwarded_for"'
  4.                        '$upstream_addr $upstream_response_time $request_time ';
  5. access_log  logs/access.log  main;
复制代码
json格式
  1. log_format logstash_json '{"@timestamp":"$time_iso8601",'
  2.        '"host": "$server_addr",'
  3.        '"client": "$remote_addr",'
  4.        '"size": $body_bytes_sent,'
  5.        '"responsetime": $request_time,'
  6.        '"domain": "$host",'
  7.        '"url":"$request_uri",'
  8.        '"referer": "$http_referer",'
  9.        '"agent": "$http_user_agent",'
  10.        '"status":"$status",'
  11.        '"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代替,如无特殊业务需求,完全可以替换。

压缩格式

日志中增加了压缩的信息。
  1. http {
  2.     log_format compression '$remote_addr - $remote_user [$time_local] '
  3.                            '"$request" $status $body_bytes_sent '
  4.                            '"$http_referer" "$http_user_agent" "$gzip_ratio"';
  5.     server {
  6.         gzip on;
  7.         access_log /spool/logs/nginx-access.log compression;
  8.         ...
  9.     }
  10. }
复制代码
upstream格式

增加upstream消耗的时间。
  1. http {
  2.     log_format upstream_time '$remote_addr - $remote_user [$time_local] '
  3.                              '"$request" $status $body_bytes_sent '
  4.                              '"$http_referer" "$http_user_agent"'
  5.                              'rt=$request_time uct="$upstream_connect_time" uht="$upstream_header_time" urt="$upstream_response_time"';
  6.     server {
  7.         access_log /spool/logs/nginx-access.log upstream_time;
  8.         ...
  9.     }
  10. }
复制代码
统计信息

统计status 出现的次数
  1. awk '{print $9}' access.log | sort | uniq -c | sort -rn
  2. 36461 200
  3. 483 500
  4. 87 404
  5. 9 400
  6. 3 302
  7. 1 499
  8. 1 403
  9. 1 301
复制代码
显示返回302状态码的URL。
  1. awk '($9 ~ /302/)' access.log | awk '{print $7}' | sort | uniq -c | sort -rn
  2. 1 /wp-login.php
  3. 1 /wp-admin/plugins.php?action=activate&plugin=ewww-image-optimizer%2Fewww-image-optimizer.php&_wpnonce=cc4a379131
  4. 1 /wp-admin/
复制代码
到此这篇关于Nginx访问日志access_log配置及信息详解的文章就介绍到这了,更多相关Nginx访问日志access_log内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

来源:https://www.jb51.net/server/319017b04.htm
免责声明:由于采集信息均来自互联网,如果侵犯了您的权益,请联系我们【E-Mail:cb@itdo.tech】 我们会及时删除侵权内容,谢谢合作!

举报 回复 使用道具