简介

Nginx 软件会把自身的故障信息及用户的日志信息记录到指定的日志文件里。配置记录 Nginx 的错误日志是调试 Nginx 服务的重要手段,属于核心功能模块(ngx_core_module)的参数,该参数的名字是 error_log

日志配置

日志配置参数

错误日志 –error.log

nginx的默认配置(缺省配置) 就是error_log logs/error.log error;//在nginx.conf里

语法

error_log file level
关键字(错误日志名称)) 日志文件(即保存日志文件路径) 错误级别

关键字(名称不能改),file–日志文件存放路径,level–保存什么级别的错误

错误日志级别常见的有(从低到高排列)

| debug | info | notice | warn | error | crit | alert | emerg |

注意:
一般不记录info等低级别 带来大量磁盘io
场景一般是wam,error,crit这三个级别之一

具体例子:

位置

可以放置的标签段位: main,http,server,location等

main (全局设置)
events(nginx工作模式)
http(http设置) : upstream(负载均衡服务器设置),
server(主机设置):location(url匹配)

访问日志

nginx软件会把每个用户访问网站的日志信息记录到指定的日志文件里,供网站提供分析用户浏览行为等,此功能由ngx_http_log_module模块来负责。log_format 用来定义记录日志的格式(可以定义多种日志格式,取不同名字即可)access_log 用来指定日志文件的路径及使用的何种日志格式记录日志。

日志参数

nginx日志格式默认的参数配置如下:

1
2
3
4
5
log_format  main  '$remote_addr - $remote_user [$time_local] "$request"'                     

'$status $body_bytes_sent "$http_referer" '                     

'"$http_user_agent" "$http_x_forwarded_for"';
变量 说明
status http状态码,记录请求返回的状态,如301 200 404 等
request http请求起始行的信息
time_local 记录访问时间与时区
http_referer 记录此次请求是从哪个链接访问过来的(发出),可以根据referer进行防盗链设置
remote_addr 记录访问网站的客户端ip地址
remote_user 客户端用户名称
http_user_agent 记录客户端的访问信息,如:浏览器、手机客户端
body_bytes_send 服务器发送给客户端的响应body 字节数
http_x_forwarded_for 当前端有代理服务器时,设置web节点记录客户端地址配置,此参数生效的前提是代理服务器上也要进行相关的x_forwarded_for设置

谷歌浏览器有插件可以修改x_forwarded_for、addr等请求ip ,许多接口可能只争对ip做了限制 ,还是容易被攻击

记录日志access_log参数说明:

语法:

1
2
3
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 增加错误日志配置:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

worker_processes 1;
error_log logs/error.log error;
events {
worker_connections 1024;
}
http {
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"';
#nginx vhosts config
include extra/www.conf;
include extra/bbs.conf;
include extra/status.conf;
access_log logs/access_www.log main;
}

在高并发场景下加上buffer和flush,可以提高网站的性能:access_log logs/access_www.log main gzip buffer=32k flush=5s;

nginx访问日志轮询切割

默认情况nginx会把所有的访问日志生成一个指定的访问日志文件access.log里,时间长了会导致日志个头很大,不利于分析日志和处理,因此,有必要对nginx按天或按小时进行切割成不同的文件保留,这里使用按天切割方法:

shell脚本如下

1
2
3
4
5
6
7
8
9
10

#!/bin/sh
Dateformat=`date +%Y%m%d -d -1day` //定义时间
Basedir="/application/nginx" //nginx安装目录
Nginxlogdir="$Basedir/logs" //存放日志的目录
Logname="access_www" //日志的名称
[ -d $Nginxlogdir ] && cd $Nginxlogdir||exit 1 //判断如果有存放目录就cd进去,否则退出
[ -f ${Logname}.log ]||exit 1 //判断如果不存在日志名称就执行下面命令,否则退出
/bin/mv ${Logname}.log ${Dateformat}_${Logname}.log //把当前日志名重命名为时间日志名
$Basedir/sbin/nginx -s reload //从新生成一个不带时间的日志文件

然后去设置一下定时任务