nginx服务器日志介绍与按天分割
2017-09-12 20:09
537 查看
nginx的log日志分为access log 和 error log。 其中access log 记录了哪些用户,哪些页面以及用户浏览器、ip和其他的访问信息。 error log 则是记录服务器错误日志。
nginx的log日志比较强大,可以记录所有的访问记录,从而进行针对性分析。但是如果把所有访问日志都放到一个文件的话,随着时间的进行,文件会越来越大,而积累的内容也不利于我们进行有效性的分析,所以对日志文件进行合理切割是有必要的。
nginx日志的常规格式的参数明细表:
nginx日志的分割(按天)
[root@cqs_test tmp]# vim /usr/local/bin/cut_nginx_logs.sh
#!/bin/bash
#function:cut nginx log files
#set the path to nginx log files(设置nginx日志存储路径)
log_files_path="/usr/local/nginx/logs/"
log_files_dir=${log_files_path}
#set nginx log files you want to cut(切割的日志)
log_files_name=(access)
#set the path to nginx.(nginx命令执行路径)
nginx_sbin="/usr/local/nginx/sbin/nginx"
#Set how long you want to save(日志保存时间)
save_days=10
#############################################Please do not modify the following script ##############################################
mkdir -p $log_files_dirlog_files_num=${#log_files_name[@]}
#cut nginx log files(当前最新日志看 access.log)
for((i=0;i<$log_files_num;i++));domv ${log_files_path}${log_files_name[i]}.log ${log_files_dir}${log_files_name[i]}.log_$(date -d "yesterday" +"%Y-%m-%d")done
#delete 10 days ago nginx log files(删除10天的日志)
find $log_files_path -mtime +$save_days -exec rm -rf {} \;
#restart nginx
$nginx_sbin -s rel
4000
oad
以上的sh脚本只要针对性地修改对应的路径,便可使用了。
把对应的cut_nginx_logs.sh加入系统计划中
[root@cqs_test tmp]# cat /etc/crontab
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
# For details see man 4 crontabs
# Example of job definition:
# .---------------- minute (0 - 59)
# | .------------- hour (0 - 23)
# | | .---------- day of month (1 - 31)
# | | | .------- month (1 - 12) OR jan,feb,mar,apr ...
# | | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# | | | | |
# * * * * * user-name command to be executed
#每天0点执行日志切割脚本
00 00 * * * root /usr/local/bin/cut_nginx_logs.sh
[root@cqs_test tmp]# cd /usr/local/nginx/logs/
[root@cqs_test logs]# ll
total 3064
-rw-r--r--. 1 root root 70414 Sep 12 16:34 access.log
-rw-r--r--. 1 root root 246 Sep 1 12:00 access.log_2017-09-03
-rw-r--r--. 1 root root 246 Sep 4 12:00 access.log_2017-09-04
-rw-r--r--. 1 root root 66150 Sep 6 21:08 access.log_2017-09-06
-rw-r--r--. 1 root root 74082 Sep 8 13:58 access.log_2017-09-07
-rw-r--r--. 1 root root 48882 Sep 11 12:00 access.log_2017-09-10
-rw-r--r--. 1 root root 12116 Sep 12 09:57 access.log_2017-09-11
-rw-r--r--. 1 nobody root 2835879 Sep 12 11:46 error.log
-rw-r--r--. 1 root root 5 Sep 12 09:30 nginx.pid
接下来就可以比较方便地分析对应的日志文档了。
nginx的log日志比较强大,可以记录所有的访问记录,从而进行针对性分析。但是如果把所有访问日志都放到一个文件的话,随着时间的进行,文件会越来越大,而积累的内容也不利于我们进行有效性的分析,所以对日志文件进行合理切割是有必要的。
nginx日志的常规格式的参数明细表:
$remote_addr | 客户端的ip地址(代理服务器,显示代理服务ip) |
$remote_user | 用于记录远程客户端的用户名称(一般为“-”) |
$time_local | 用于记录访问时间和时区 |
$request | 用于记录请求的url以及请求方法 |
$status | 响应状态码,例如:200成功、404页面找不到等。 |
$body_bytes_sent | 给客户端发送的文件主体内容字节数 |
$http_user_agent | 用户所使用的代理(一般为浏览器) |
$http_x_forwarded_for | 可以记录客户端IP,通过代理服务器来记录客户端的ip地址 |
$http_referer | 可以记录用户是从哪个链接访问过来的 |
[root@cqs_test tmp]# vim /usr/local/bin/cut_nginx_logs.sh
#!/bin/bash
#function:cut nginx log files
#set the path to nginx log files(设置nginx日志存储路径)
log_files_path="/usr/local/nginx/logs/"
log_files_dir=${log_files_path}
#set nginx log files you want to cut(切割的日志)
log_files_name=(access)
#set the path to nginx.(nginx命令执行路径)
nginx_sbin="/usr/local/nginx/sbin/nginx"
#Set how long you want to save(日志保存时间)
save_days=10
#############################################Please do not modify the following script ##############################################
mkdir -p $log_files_dirlog_files_num=${#log_files_name[@]}
#cut nginx log files(当前最新日志看 access.log)
for((i=0;i<$log_files_num;i++));domv ${log_files_path}${log_files_name[i]}.log ${log_files_dir}${log_files_name[i]}.log_$(date -d "yesterday" +"%Y-%m-%d")done
#delete 10 days ago nginx log files(删除10天的日志)
find $log_files_path -mtime +$save_days -exec rm -rf {} \;
#restart nginx
$nginx_sbin -s rel
4000
oad
以上的sh脚本只要针对性地修改对应的路径,便可使用了。
把对应的cut_nginx_logs.sh加入系统计划中
[root@cqs_test tmp]# cat /etc/crontab
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
# For details see man 4 crontabs
# Example of job definition:
# .---------------- minute (0 - 59)
# | .------------- hour (0 - 23)
# | | .---------- day of month (1 - 31)
# | | | .------- month (1 - 12) OR jan,feb,mar,apr ...
# | | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# | | | | |
# * * * * * user-name command to be executed
#每天0点执行日志切割脚本
00 00 * * * root /usr/local/bin/cut_nginx_logs.sh
[root@cqs_test tmp]# cd /usr/local/nginx/logs/
[root@cqs_test logs]# ll
total 3064
-rw-r--r--. 1 root root 70414 Sep 12 16:34 access.log
-rw-r--r--. 1 root root 246 Sep 1 12:00 access.log_2017-09-03
-rw-r--r--. 1 root root 246 Sep 4 12:00 access.log_2017-09-04
-rw-r--r--. 1 root root 66150 Sep 6 21:08 access.log_2017-09-06
-rw-r--r--. 1 root root 74082 Sep 8 13:58 access.log_2017-09-07
-rw-r--r--. 1 root root 48882 Sep 11 12:00 access.log_2017-09-10
-rw-r--r--. 1 root root 12116 Sep 12 09:57 access.log_2017-09-11
-rw-r--r--. 1 nobody root 2835879 Sep 12 11:46 error.log
-rw-r--r--. 1 root root 5 Sep 12 09:30 nginx.pid
接下来就可以比较方便地分析对应的日志文档了。
相关文章推荐
- shell 脚本nginx日志分割
- nginx服务器的日志拆分
- nginx 分割日志文件
- nginx日志分割 for linux
- nginxm每分钟分割日志
- 用cronolog分割Nginx日志
- centos中nginx按日期自动分割访问日志的方法
- Nginx 服务安全优化---隐藏版本号、网页缓存、日志分割
- nginx 通过rsyslog发日志 rsyslog服务器挂掉 日志丢失问题
- Nginx按天分割轮询日志文件
- polysh同时查看多台服务器日志 安装与使用详细介绍
- nginx日志分割 for linux
- nginx,php日志分割
- nginx日志分割
- 【总结】Nginx日志分割
- Nginx 日志自动分割
- shell脚本控制nginx日志文件分割
- Nginx日志分割
- nginx 日志分割
- nginx 通过rsyslog发日志 rsyslog服务器挂掉 日志丢失问题