Nginx的日志剖析
1、访问日志(access.log)
Nginx的访问日志就是一个文件,它存储着每个用户对网站的访问请求,这个功能是有ngx_http_log_module模块来负责的,这个文件存在的主要目的就是为了分析用户的浏览行为
Nginx的访问日志主要是由:log_format和access_log来控制的(log_format:用来定义记录日志的格式,access_log:用来指导日志的路径以及使用什么格式来存储日志)
Nginx的日志格式默认参数如下:
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $boby_bytes_sent "http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
上面的这个默认格式:其中,log_format为日志格式的关键参数,不能变,main 是为日志格式指定的标签,记录日志时通过这个main标签选择指定的格式,其后所接的所有内容都是可以记录的日志信息,所有的日志段以空格分割,一行可记录多个
下面的表格对上面的日志格式参数做详细介绍:
| Nginx日志变量 | 说明 |
| $remote_addr | 记录访问网站的客户端IP地址 |
| $http_x_forwarded_for | 当前端有代理服务时,设置web节点记录客户端地址的配置,此参数生效的前提是在代理服务器上也进行了相关的x_forwarded_for设置 |
| $remote_user | 远程客户端的名称 |
| $time_local | 记录访问时间和时区 |
| $request | 用户http请求起始行信息 |
| $status | http状态码,记录返回的状态 |
| $boby_bytes_sents | 服务器发给客户端响应的boby字节数 |
| $http_referer | 记录此次的请求是从哪个链接访问过来的,可以根据referer进行防盗链设置 |
| $http_user_agent | 记录客户端访问信息,比如浏览器等 |
还有一些可以对日志做到极致优化的一些参数,这些参数一般都是使用默认的就好了,除非有很强的需求:
配置语法如下:
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,表示不记录日志
下面我们来模拟一下自定义的日志格式:
编辑主配置文件nginx.conf,配置日志的格式如下:
[root@Nginx ~]# sed -n '21,23 s/#//gp' /opt/nginx/conf/nginx.conf.default # 在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 nginx]# cat conf/nginx.conf
worker_processes ;
events {
worker_connections ;
}
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 ;
include www_date/brian.conf;
include www_date/brianzjz.conf;
include www_date/status.conf;
}
然后在每个虚拟主机中配置,使其使用上面的日志格式:(以www.brian.com为例,红色部分为添加部分)
[root@Nginx nginx]# cat conf/www_date/brian.conf
server {
listen ;
server_name www.brian.com brian.com;
location / {
root html/brian;
index index.html index.htm;
}
access_log logs/brian.log main;
error_page /50x.html;
location = /50x.html {
root html;
}
}
检查语法:
[root@Nginx nginx]# sbin/nginx -t # 出现ok说明没有问题
nginx: the configuration file /opt/nginx//conf/nginx.conf syntax is ok
nginx: configuration file /opt/nginx//conf/nginx.conf test is successful
平滑重启:
[root@Nginx nginx]# sbin/nginx -s reload
模拟用户访问,使用Linux和windows浏览器访问,生成日志:
使用Linux测试访问:
[root@Nginx nginx]# curl www.brian.com
www.brian.com
使用windows测试:

查看生成的日志:
[root@Nginx logs]# pwd # 查看日志路径
/opt/nginx/logs
[root@Nginx logs]# ll
总用量 24
-rw-r--r--. 1 root root 7420 3月 21 17:02 access.log
-rw-r--r--. 1 root root 527 3月 22 14:37 brian.log # 生成的brian.log日志
-rw-r--r--. 1 root root 6151 3月 22 14:37 error.log
-rw-r--r--. 1 root root 6 3月 21 17:04 nginx.pid
日志内容:(日志内容没有的用 "-" 填充)
[root@Nginx logs]# cat brian.log
192.168.1.102 - - [22/Mar/2018:14:35:12 +0800] "GET / HTTP/1.1" 200 14 "-" "curl/7.29.0" "-" # Linux访问日志
192.168.1.106 - - [22/Mar/2018:14:37:28 +0800] "GET / HTTP/1.1" 200 14 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36 Edge/16.16299" "-" # windows访问日志
192.168.1.106 - - [22/Mar/2018:14:37:28 +0800] "GET /favicon.ico HTTP/1.1" 404 570 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36 Edge/16.16299" "-" # windows访问日志
我们还可以在记录日志参数中加上buffer和flush,这样可在高并发场景下提升网站的访问性能:
[root@Nginx nginx]# cat conf/www_date/brian.conf
server {
listen ;
server_name www.brian.com brian.com;
location / {
root html/brian;
index index.html index.htm;
}
access_log logs/brian.log main gzip buffer=128k flush=5s; # 对日志进行了一下优化
error_page /50x.html;
location = /50x.html {
root html;
}
}
Nginx访问日志轮询切割:
默认情况下nginx的访问日志都会存到access.log的日志文件中,但是时间长了,这个文件会变的非常的大,不利于我们后期的分析,因此有必要对nginx的日志,按照天或者小时进行分割,保存为不同的文件,一般情况下按照天进行分割就已经够用了:
我们可以写个脚本实现切割nginx的日志文件,将正在写入的日志改名为带日期格式的文件,然后在平滑的重启主机,生成新的nginx日志文件,然后在添加到计划任务,自动去执行:
操作如下:
# 分割脚本如下:
[root@Nginx nginx]# mkdir script
[root@Nginx nginx]# cd script/
[root@Nginx script]# vim segmentation.sh
[root@Nginx script]# cat segmentation.sh
#!/bin/bash
Dateformat=`date +%Y%m%d`
Basedir="/opt/nginx"
Nginxlogdir="$Basedir/logs"
Logname="access"
[ -d $Nginxlogdir ] && cd $Nginxlogdir||exit 1
[ -f ${Logname}.log ] || exit 1
/bin/mv ${Logname}.log ${Dateformat}_${Logname}.log
$Basedir/sbin/nginx -s reload ====================================================================================================
# 添加到计划任务列表
[root@Nginx script]# cat >>/var/spool/cron/root <<EOF
> 00 00 * * * /bin/sh /opt/nginx/script/segmentation.sh > /dev/null 2>&1
> EOF
[root@Nginx script]# crontab -l
00 00 * * * /bin/sh /opt/nginx/script/segmentation.sh > /dev/null 2>&1
我们执行下脚本,最后的结果就是:
[root@Nginx script]# sh segmentation.sh
[root@Nginx script]# ll /opt/nginx/logs/
总用量 24
-rw-r--r--. 1 root root 7420 3月 21 17:02 20180322_access.log # 已经按照我们的脚本进行了分割了
-rw-r--r--. 1 root root 0 3月 22 15:11 access.log
-rw-r--r--. 1 root root 527 3月 22 14:37 brian.log
-rw-r--r--. 1 root root 6212 3月 22 15:11 error.log
-rw-r--r--. 1 root root 6 3月 21 17:04 nginx.pid
2、错误日志(error.log)
Nginx服务软会把自身运行故障信息及用户访问的日志信息记录到指定的日志文件中,这个日志文件就是error.log
配置记录nginx的错误信息是做调试的重要手段,所以必须要有,他属于模块ngx_core_module的参数,它可以放在Main区块中,也可以写到不同的虚拟主机中、
其语法格式为:
error_log file level
关键字 日志文件 错误级别
其中error_log关键字不能变,日志文件可以放到任意的路径下,错误级别包括:debug、info、notice、warn、error、crit、alert、emerg,级别越高记录的信息越少,一般情况下会用warn、error、crit这三个级别,不要使用低级,会给磁盘带来巨大的磁盘I/O消耗
错误日志文件配置:
[root@Nginx script]# cat /opt/nginx/conf/nginx.conf
worker_processes ;
error_log logs/error.log; # 添加这一行就行了
events {
worker_connections ;
}
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 ;
include www_date/brian.conf;
include www_date/brianzjz.conf;
include www_date/status.conf;
}
Nginx的日志剖析的更多相关文章
- 菜鸟nginx源码剖析 框架篇(一) 从main函数看nginx启动流程(转)
俗话说的好,牵牛要牵牛鼻子 驾车顶牛,处理复杂的东西,只要抓住重点,才能理清脉络,不至于深陷其中,不能自拔.对复杂的nginx而言,main函数就是“牛之鼻”,只要能理清main函数,就一定能理解其中 ...
- 菜鸟nginx源码剖析数据结构篇(九) 内存池ngx_pool_t[转]
菜鸟nginx源码剖析数据结构篇(九) 内存池ngx_pool_t Author:Echo Chen(陈斌) Email:chenb19870707@gmail.com Blog:Blog.csdn. ...
- Nginx 访问日志轮询切割
Nginx 访问日志轮询切割脚本 #!/bin/sh Dateformat=`date +%Y%m%d` Basedir="/application/nginx" Nginxlog ...
- 按日期切割nginx访问日志--及性能优化
先谈下我们需求,一个比较大的nginx访问日志,根据访问日期切割日志,保存在/tmp目录下. 测试机器为腾讯云机子,单核1G内存.测试日志大小80M. 不使用多线程版: #!/usr/bin/env ...
- 一、基于hadoop的nginx访问日志分析---解析日志篇
前一阵子,搭建了ELK日志分析平台,用着挺爽的,再也不用给开发拉各种日志,节省了很多时间. 这篇博文是介绍用python代码实现日志分析的,用MRJob实现hadoop上的mapreduce,可以直接 ...
- log_format为Nginx设置日志格式
nginx服务器日志相关指令主要有两条,一条是log_format,用来设置日志格式, 另外一条是access_log,用来指定日志文件的存放路径.格式和缓存大小,一般在nginx的配置文件中日记配置 ...
- Nginx常用日志分割方法
方式一: nginx cronolog日志分割配置文档,根据下面方法,每分钟分割一次NGINX访问日志. 1.nginx日志配置 access_log /var/log/nginx/access.lo ...
- elk收集分析nginx access日志
elk收集分析nginx access日志 首先elk的搭建按照这篇文章使用elk+redis搭建nginx日志分析平台说的,使用redis的push和pop做队列,然后有个logstash_inde ...
- nginx实现日志按天切割
背景 nginx的日志文件没有rotate功能.一段时间过后,日志将越发臃肿,一个accesslog很快就突破1G,因此有必要通过脚本实现按天切割日志. 解决思路 1 重命名日志文件,如更改为acc ...
随机推荐
- Python -----issubclass和isinstance
issubclass用于判断一个类是否为另一个类的子类,isinstance用于判断一个对象是否某类的一个实例 import math class Point: def __init__(self, ...
- Mac安装PhoneGap3
Mac安装PhoneGap3第一步需要安装NodeJS,在Mac下有一个.pkg安装包(Mac OS X Installer (.pkg),下载下来一路点击就可以安装成功了.在Terminal控制台输 ...
- 基于 WiFi ( Wireless Fidelity) 的室内定位技术
系统的拓扑结构 基于信号强度的定位技术(RSSI, Received Signal Strength Indication) 无线信号的信号强度随着传播距离的增加而衰减,接收方与发送方离得越近, 则接 ...
- GO入门——3. 控制语句
1 if 条件表达式没有括号 支持一个初始化表达式(可以是并行方式) 左大括号必须和条件语句或else在同一行 支持单行模式 初始化语句中的变量为block级别,同时隐藏外部同名变量 a := 1 i ...
- nginx反向代理如何获取真实IP?
由于客户端和web服务器之间增加了中间层,因此web服务器无法直接拿到客户端的ip,通过$remote_addr变量拿到的将是反向代理服务器的ip地址. 1.安装--with-http_realip_ ...
- psql工具使用(二)
所有psql命令都以 \ 开头 一.使用psql -l查看有哪些数据库: -bash-4.2$ psql -l List of databases Name | Owner | Encodin ...
- php还原16进制特殊字符
特殊字符的16进制表:https://websitebuilders.com/tools/html-codes/ascii/ 可以通过 hexdec() 和 chr()方法来进行转换, 例子: < ...
- 【SpringBoot系列1】SpringBoot整合MyBatis
前言: 一直看网上说SpringBoot是解锁你的配置烦恼,一种超级快速开发的框架.一直挺想学的,正好最近也有时间,就学了下 这个是SpringBoot整合MyBatis的一个教程,用了阿里的drui ...
- 你得学会并且学得会的Socket编程基础知识
这一篇文章,我将图文并茂地介绍Socket编程的基础知识,我相信,如果你按照步骤做完实验,一定可以对Socket编程有更好地理解. 本文源代码,可以通过这里下载 http://files.cnblog ...
- 全局唯一订单号生成方法(参考snowflake)
backgroud Snowflake is a network service for generating unique ID numbers at high scale with some si ...