nginx访问日志(access_log)
一、nginx访问日志介绍
nginx软件会把每个用户访问网站的日志信息记录到指定的日志文件里,供网站提供者分析用户的浏览行为等,此功能由ngx_http_log_module模块负责,对应的官方地址为:http://nginx.org/en/docs/http/ngx_http_log_module.html.
二、访问日志参数
nginx的访问日志主要有以下2个参数控制
log_format 用来定义记录日志的格式(可以定义多种日志格式,取不同的名字即可)
access_log 用来指定日志文件的路径及使用何种日志格式记录日志
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;
三、访问日志配置说明
日志格式的定义说明
语法如下:
定义语法 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标签选择指定的格式。其后所接的所有内容都是可以记录的日志信息,所有的日志段以空格分割,一行可以记录多个。不同列的意义如下:
$remote_addr 记录访问网站的客户端地址; $http_x_forwarded_for 当前端有代理服务器时,设置web节点记录客户端地址的配置,此参数生效的前提是代理服务器上也进行了相关的x_forwarded_for设置; $remote_user 远程客户端用户名称; $time_local 记录访问时间与时区; $request 用户的http请求起始行信息; $status http状态码,记录请求返回的状态,例如200、404、301等; $body_bytes_sent 服务器发给客户端的响应body字节数; $http_referer 记录此次请求是从哪个链接访问呢过来的,可以根据referer进行防盗链设置; $http_user_agent 记录客户端访问信息,例如:浏览器、手机客户端等;
在没有特殊要求的情况下,采用默认的配置即可,更多可以设置的记录日志信息的变量见:http://nginx.org/en/docs/http/ngx_http_log_module.html
记录日志的access_log参数说明
下面是有关access_log参数的官方说明:
语法如下:
access_log path [format [buffer=size] [gzip[=level]] [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@nginx 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"';
把上述内容放到nginx.conf 的 http 标签的首部,如下:
[root@nginx conf]# cat nginx.conf
worker_processes 1;
error_log logs/error.log error;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type 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"'; sendfile on;
keepalive_timeout 65;
include extra/dmtest1.conf;
include extra/dmtest2.conf;
include extra/dmtest3.conf;
}
然后在每个虚拟主机里进行配置,使其使用上述格式记录用户访问日志。以 www.dmtest1.com 站点为例,命令如下:
[root@nginx conf]# cat extra/dmtest1.conf
server {
listen 80;
server_name www.dmtest1.com dmtest1.com;
location / {
root html/dmtest1;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
access_log logs/access_dmtest1.log main;
}
如果不指定日志格式就会采用默认的combined格式记录日志。
检查语法重新加载配置文件:
[root@nginx conf]# ../sbin/nginx -t
nginx: the configuration file /application/nginx-1.8.1/conf/nginx.conf syntax is ok
nginx: configuration file /application/nginx-1.8.1/conf/nginx.conf test is successful [root@nginx conf]# systemctl reload nginx
用浏览器模拟用户访问生成日志,在服务器上查看日志结果,如下:
[root@nginx conf]# curl www.dmtest1.com
www.dmtest1.com [root@nginx conf]# ls -l ../logs/access_dmtest1.log
-rw-r--r-- 1 root root 95 8月 27 06:24 ../logs/access_dmtest1.log [root@nginx conf]# tail -1 ../logs/access_dmtest1.log
192.168.200.102 - - [27/Aug/2018:06:24:36 +0800] "GET / HTTP/1.1" 200 16 "-" "curl/7.29.0" "-"
使用谷歌浏览器访问的日志结果如下:
[root@nginx conf]# tail -10 ../logs/access_dmtest1.log
192.168.200.1 - - [27/Aug/2018:06:36:58 +0800] "GET / HTTP/1.1" 200 16 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36" "-"
日志格式和日志内容对应说明如下:
$remote_addr 对应的是真实日志里的192.168.200.1,即客户端的IP。 $remote_user 对应的是第二个中杠"-",没有远程用户,所以用"-"填充。 [$time_local 对应的是[27/Aug/2018:06:36:58 +0800]。 "$request" 对应的是"GET/HTTP/1.1"。 $tatus 对应的是200状态码,200表示正常访问。 $body_bytes_sent 对应的是16字节,即响应body的大小。 "$http_referer" 对应的是"-",由于是直接打开域名浏览的,因此,referer没有值。 $http_user_agent 对应的是"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36" "$http_x_forwarded_for" 对应的是"-",因为web服务没有使用代理,因此此处为"-"
下面针对日志配置进行深入说明。
可以在记录日志参数中加上buffer和flush选项,这样可以在高并发场景下提升网站访问性能。加该选项的命令如下:
access_log path [format [buffer=size] [gzip[=level]] [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]]; access_log off;
具体配置如下:
[root@nginx conf]# cat extra/dmtest1.conf
server {
listen 80;
server_name www.dmtest1.com dmtest1.com;
location / {
root html/dmtest1;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
#access_log logs/access_dmtest1.log main;
access_log logs/access_dmtest1.log main gzip buffer=32k flush=5s;
#access_log off;
}
五、nginx访问日志轮训切割
默认情况下nginx会把所有的访问日志生成到一个指定的访问日志文件access_log里,但这样一来,时间长了就会导致日志文件很大,不利于日志的分析和处理,因此,有必要对nginx日志按天或小时进行切割,使其分成不同的文件保存。这里使用按天切割的方法。
具体切割脚本如下:
[root@nginx shell]# cat cut_nginx_log.sh
#!/bin/bash
#Author:Mr.Ding
#Created Time:2018-08-27 07:19:30
#Name:cut_nginx_log.sh
#Description:
Dateformat=`date +%Y%m%d`
Basedir="/application/nginx"
Nginxlogdir="$Basedir/logs"
Logname="access_dmtest1"
[ -d $Nginxlogdir ] && cd $Nginxlogdir || exit 1
[ -f ${Logname}.log ] || exit 1
/bin/mv ${Logname}.log ${Dateformat}_${Logname}.log
#$Basedir/sbin/nginx -s reload
systemctl reload nginx
脚本实现切割nginx的思想为将正在写入的nginx日志(access_dmtest1.log)改名为带日期的格式文件(20180827_access_dmtest1.log),然后平滑重新加载nginx,生成新的nginx日志(access_dmtest1.log)。
把脚本加入计划任务:
[root@nginx shell]# cat >>/var/spool/cron/root << EOF
> #cut ngixn access log by dm 2018-8-27
> 00 00 * * * /bin/sh /server/scripts/shell/cut_nginx_log.sh >/dev/null 2&1
> EOF
[root@nginx shell]# crontab -l
#time sync by dm at 2018-8-20
*/5 * * * * /usr/sbin/ntpdate -u ntp.api.bz >/dev/null 2>$1
#cut ngixn access log by dm 2018-8-27
00 00 * * * /bin/sh /server/scripts/shell/cut_nginx_log.sh >/dev/null 2&1
最终日志切割效果如下:
[root@nginx logs]# ll
总用量 32
-rw-r--r-- 1 root root 0 8月 27 07:35 20180827_access_dmtest1.log
-rw-r--r-- 1 root root 0 8月 27 07:35 access_dmtest1.log
-rw-r--r-- 1 root root 14076 8月 27 04:41 access.log
-rw-r--r-- 1 root root 10098 8月 27 06:36 error.log
-rw-r--r-- 1 root root 5 8月 26 21:56 nginx.pid
nginx访问日志(access_log)的更多相关文章
- nginx访问日志access_log
在 nginx.conf 配置文件 http{} 方法体的括号内,增加或者打开以下代码注释: log_format main '$remote_addr - $remote_user [$time_l ...
- Nginx 访问日志轮询切割
Nginx 访问日志轮询切割脚本 #!/bin/sh Dateformat=`date +%Y%m%d` Basedir="/application/nginx" Nginxlog ...
- logstash收集nginx访问日志
logstash收集nginx访问日志 安装nginx #直接yum安装: [root@elk-node1 ~]# yum install nginx -y 官方文档:http://nginx.org ...
- Nginx 访问日志配置
一.Nginx 访问日志介绍 Nginx 软件会把每个用户访问网站的日志信息记录到指定的日志文件里,供网站提供者分析用户的浏览行为等,此功能由 ngx_http_log_module 模块负责. 二. ...
- Nginx访问日志、 Nginx日志切割、静态文件不记录日志和过期时间
1.Nginx访问日志 配制访问日志:默认定义格式: log_format combined_realip '$remote_addr $http_x_forwarded_for [$time_loc ...
- Logstash+ElasticSearch+Kibana处理nginx访问日志(转)
ELK似乎是当前最为流行的日志收集-存储-分析的全套解决方案. 去年年初, 公司里已经在用, 当时自己还山寨了一个统计系统(postgresql-echarts, 日志无结构化, json形式存储到p ...
- Nginx访问日志、日志切割、静态文件不记录日志和过期时间
6月8日任务 12.10 Nginx访问日志12.11 Nginx日志切割12.12 静态文件不记录日志和过期时间 12.10 Nginx访问日志 除了在主配置文件nginx.conf里定义日志格式外 ...
- Linux centosVMware Nginx访问日志、Nginx日志切割、静态文件不记录日志和过期时间
一.Nginx访问日志 vim /usr/local/nginx/conf/nginx.conf //搜索log_format 日至格式 改为davery格式 $remote_addr 客户端IP ...
- Nginx访问日志.Nginx日志切割
11月27日任务 12.10 Nginx访问日志12.11 Nginx日志切割12.12 静态文件不记录日志和过期时间 1.Nginx访问日志 示例一: 日志格式 vim /usr/local/ngi ...
随机推荐
- NET Core应用中如何记录和查看日志
NET Core应用中如何记录和查看日志 日志记录不仅对于我们开发的应用,还是对于ASP.NET Core框架功能都是一项非常重要的功能特性.我们知道ASP.NET Core使用的是一个极具扩展性的日 ...
- python 6 循环
循环 要计算1+2+3,我们可以直接写表达式: >>> 1 + 2 + 3 6 要计算1+2+3+...+10,勉强也能写出来. 但是,要计算1+2+3+...+10000,直接写表 ...
- JAVA代码之斗地主发牌
理解很好理解,关键是思路 按照斗地主的规则,完成洗牌发牌的动作: 具体规则: 1. 组装54张扑克牌 2. 将54张牌顺序打乱 3. 三个玩家参与游戏,三人交替摸牌,每人17张牌,最后三张留作底牌. ...
- Java基础(Java概述、环境变量、注释、关键字、标识符、常量)
第1天 Java基础语法 今日内容介绍 u Java开发环境搭建 u HelloWorld案例 u 注释.关键字.标识符 u 数据(数据类型.常量) 第1章 Java开发环境搭建 1.1 Java概述 ...
- hive中select中DISTINCT的技巧和使用
hive中select中DISTINCT的技巧和使用 单表的唯一查询用:distinct 多表的唯一查询用:group by 在使用MySQL时,有时需要查询出某个字段不重复的记录,虽然mysql提供 ...
- IOS使用固定定位遇到的问题
近日需要实现移动端页面额外功能按钮,即点击加号弹出点赞与留言功能,通常这个按钮都会固定于页面的右下角,首先就想到使用固定定位来实现. 但是在测试时我们发现,在IOS中,当系统键盘弹出时,fixed会失 ...
- Android 读取excel 文件
在面对选择国家地区,选择手机号码区号等信息的时候,常常我们是读取已存好的数据,我现在读取的就是excel里面的数据,所以在此记录下读取的方法以及注意点. 下面就是读取国际地区手机区号的数据效果图: e ...
- HoloLens | 世界的每一次变化,其实都提前打好了招呼
新年,对灯发誓——不说老话,说新鲜事. 佛经上说:世间唯一永恒不变的,就是永远在变化. 130年前(说好的不说老话呢),世界上第一辆汽车在德国发出第一声轰鸣,世界变了: 现在,汽车已遍及世界,颜值.性 ...
- jsHint-静态代码检查工具eclipse中使用
今天介绍一个关于js静态代码的检查工具,此工具可以帮助更好的规范代码的编写形式以及检查错误.由于jslint的分支jsHint有跟多的配置项相对使用也比较方便,依次本文主要介绍jsHint的使用方式. ...
- WPF中,DataGrid最左边多出一行的解决方案
这种情况下,请在DataGrid的属性里加上这个属性: RowHeaderWidth="0" 必须赋值为0,不能不赋值,也不能赋其他值. 问题解决.