linux_Nginx日志

错误信息日志配置:

  日志文件默认:/application/nginx/logs/erroe.log

       error_log logs/error.log error; 

  # 不写默认就有,默认error,这个不要配置成info,数据库错误日志不要配置成info,做反向代理或者web服务,一定不要给info,带来巨大的磁盘IO消耗

 Nginx日志变量说明

       $remote_addr                           记录访问网站的客户端

       $http_x_forwarded_for             当前代理服务器时,设置web节点记录客户端地址的配置

       $remote_user                           远程客户端用户名

       $time_local                               记录访问时间与时区

       $request                                   用户的http请求起始行信息

       $status                                      http状态码,记录请求返回的状态码

       $body_bytes_sents                  服务器发送给客户端的相应body字节数

       $http_referer                            记录此次请求是从那个链接访问过来的

       $http_user_agent                     记录访问客户端信息

       # 这些参数,如果没有数据,将默认为 -

在Nginx主配置文件nginx.conf中http模块中写入,long_format字段,添加日志格,定义日志全局格式

log_format  main    '$remote_addr - $remote_user [$time_local] "$request"'

                               '$status $body_bytes_sent "$http_referer"'

                               '"$http_user_agent "$http_x_forwarded_for"';

  然后在每个server中,添加 access_log 字段,为每个单独的server添加访问日志

access_log logs/www_access.log main;

       这个access_log 参数最后还可以添加buffer 和 flush参数,格式为:

              access_log path format [buffer=size] [flush=time] [if=condition]

  # 缓存多大时候写一次日志,或者间隔多长时间写一次日志,if=condition 是固定写法,满足条件的意思,增加IO性能,在高并发的情况下提升网络访问性能

Nginx日志切割

       通过 .sh + crond 来进行日志切割,通过rsync进行日志备份,通过find删除180天以前的日志

mkdir /server/scripts -p                     # 创建一个写脚本的目录,规范
cd /server/scripts/                             # 切换到目录下
vim cut_nginx_log.sh                        # 编辑一个脚本,注意脚本名,规范,见名知意
cd /application/nginx/logs && 

/bin/mv www_access.log www_access_$(date +%F -d '-1day').log && 

/application/nginx/sbin/nginx -s reload && 

find /application/nginx/logs/ -type f -name www_access_*.log -mtime +7|xargs rm -f && 

rsync -avz /application/nginx/logs/ rsync_backup@172.16.1.41::nginxlog --password-file=/etc/rsync.password

sh -x cut_nginx_log.sh        # 测试
crontab -e                    # 写入定时任务
00 00 * * * /bin/bash /server/scripts/cut_nginx_log.sh > /dev/null 2>&1

# 备份任务都是晚上凌晨以后,如果有必要,需要进行 --bwlimit= 进行限速,单位为k

 

原文地址:https://www.cnblogs.com/2bjiujiu/p/8117395.html