主要参考文章:
nginx 日志收集 arg_xx 参数:
Nginx变量使用方法详解 -> 2.2
https://www.jianshu.com/p/44680c081ea0
nginx 按天切分/删除 :
nginx日志按天生成&定期删除日志
https://blog.csdn.net/zzhongcy/article/details/86303204
linux 递归删除某个文件夹下面的文件
https://blog.csdn.net/shan165310175/article/details/18553099
我们目前主要采用的是 Nginx 收集日志。从学习提高的角度考虑,我们自己实现了一个Nginx收集日志的企业级流程。
基本环境
操作系统: CentOS7
Nginx : nginx/1.14.2
主要涵盖以下几个部分:
1.调整日期时间。使用UTC时间
2.Nginx 基本日志收集配置
3.Nginx 多项目配置
4.Nginx 日志按天切分
5.Nginx 删除过旧日志脚本(30天之前)+ crontab设置
6.其他问题
6.1 权限问题: open() "/var/log/nginx/access.log" failed (13: Permission denied) #746
6.2 Nginx 完整配置
1.调整日期时间。使用UTC时间
该步骤设置 CentOS7 当前的时区,完成输出的时间为 China/Shanghai 时区。在生成 access.log 时,
主要有2个步奏
1.通过指令修改
2.修改配置文件
1.通过指令修改
主要使用 tzselect , 解释 time zone select
选择亚洲时区
选择中国:
选择北京时区:
2.修改配置文件
修改配置文件,这里我们通过 /etc/profile 来完成预设值。
在 /etc/profile 增加以下代码
export TZ='Asia/Shanghai'
并执行
source /etc/profile
然后 我们再次执行 date, 可以看到现在我们看到的就是UTC 时间了。
[root@nginx1 my_bin]# date
Mon Jan 21 17:05:19 CST 2019
2.Nginx 基本日志收集配置
这里主要借鉴的思路是,arg_xx
2.2 $arg_XXX
另一个特别常用的内建变量其实并不是单独一个变量,而是有无限多变种的一群变量,即名字以 arg_ 开头的所有变量,我们估且称之为 $arg_XXX 变量群。
一个例子是 $arg_name,这个变量的值是当前请求中名为 name 的参数的值,而且还是未解码的原始形式的值。
location /test-arg {
echo "name: $arg_name";
echo "class: $arg_class";
}
输出:
[root@localhost html]# nginx -s reload
[root@localhost html]# curl localhost/test-arg
name:
class:
[root@localhost html]# curl "localhost/test-arg?name=Tom&class=3"
name: Tom
class: 3
[root@localhost html]# curl "localhost/test-arg?name=hello%20world&class=9"
name: hello%20world
class: 9
2.3 $arg_XXX 不区分大小写
其实 $arg_name 不仅可以匹配 name 参数,也可以匹配 NAME 参数,抑或是 Name,Nginx 会在匹配参数名之前,自动把原始请求中的参数名调整为全部小写的形式。
[root@localhost html]# curl "localhost/test-arg?NAME=Marry"
name: Marry
class:
[root@localhost html]# curl "localhost/test-arg?Name=Jimmy"
name: Jimmy
class:
2.4 对 uri 解码
如果你想对 URI 参数值中的 %XX 这样的编码序列进行解码,可以使用第三方 ngx_set_misc 模块提供的
location /test-unescape-uri {
set_unescape_uri $name $arg_name;
set_unescape_uri $class $arg_class;
echo "name: $name";
echo "class: $class";
}
现在我们再看一下效果:
[root@localhost html]# curl "localhost/test-arg?name=hello%20world&class=9"
name: hello world
class: 9
最后,我们看一个较为完整的配置:
log_format test2 '{"time":"$time_iso8601",'
'"header":{'
'"remote_ip":"$remote_addr", "real_ip":"$http_x_forwarded_for", "http_code":"$status", "body_bytes_sent":"$body_bytes_sent",'
'"req_time":"$request_time", "res_time":"$upstream_response_time", "user_ua":"$http_user_agent", "http_referer":"$http_referer", "user_cookie":"$cookie_uid"'
'},'
'"arguments":{'
'"dot":"$arg_dot", "gender":"$arg_gender", "weChatId":"$arg_weChatId", "ua":"$arg_ua", "test":"$arg_test"'
'}';
location /monitor/mobile/u100001/p100002/00 {
access_log logs/test2/access_$year$month$day.log test2;
add_header Content-Type 'text/html';
add_header X-REQUEST-PATH '/tmp/logs/test';
return 200;
}
上面我们定义了一个 test2 的转换格式。会根据请求中参数 ,生成一条 test2 格式的日志。
3.Nginx 多项目配置
多个收集项目,主要通过配置不同的路径来实现收集。
location /monitor/mobile/u100001/p100001/00 {
access_log logs/test1/access_$year$month$day.log test1;
add_header Content-Type 'text/html';
add_header X-REQUEST-PATH '/tmp/logs/test';
return 200;
}
location /monitor/mobile/u100001/p100002/00 {
access_log logs/test2/access_$year$month$day.log test2;
add_header Content-Type 'text/html';
add_header X-REQUEST-PATH '/tmp/logs/test';
return 200;
}
可以看出主要通过不同的路径,来实现不同的采集项目。
第一个测试请求:
http://192.168.75.135/monitor/mobile/u100001/p100001/00?gender=22&dot=22&test=33
第二个测试请求
http://192.168.75.135/monitor/mobile/u100001/p100002/00?gender=22&dot=22&test=33
4.Nginx 日志按天切分
按天切分,主要是通过内置的变量实现的。
需要使用到 timeiso8601内嵌变量来获取时间。 time_iso8601 内嵌变量来获取时间。time iso8601内嵌变量来获取时间。time_iso8601格式如下:2018-09-21T16:01:02+02:00。然后使用正则表达式来获取所需时间的数据。
按天分割日志
配置在server段:
if ($time_iso8601 ~ "^(\d{4})-(\d{2})-(\d{2})") {
set $year $1;
set $month $2;
set $day $3;
}
access_log logs/host.access_${year}${month}${day}.log main;
5.Nginx 删除过旧日志脚本(30天之前)
这里我们写了一个脚本完成这样的功能:
#!/bin/bash
ROOT_PATH=$(dirname $(readlink -f $0))
LOG_BASE_PATH=${NGINX_HOME}/logs
#cur_day='20181225'
cur_day=$(date +"%Y%m%d")
del_day=$(date -d "${cur_day} -30day" +"%Y%m%d")
#----------log----------
echo "del date : "$del_day" start" >> del_logs.log
#----------log----------
find ${LOG_BASE_PATH} -name "*$del_day*.log" -type f -print -exec rm -rf {} \;
crontab -l
[root@nginx1 logs]# crontab -l
# 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
#定期删除Nginx access.log
0 1 * * * sh /opt/nginx/my_bin/del_old_accesslog.sh
6.其他问题
6.1 权限问题: open() "/var/log/nginx/access.log" failed (13: Permission denied) #746
这个问题主要是我的nginx 的 logs 目录 是 root:root , 我们修改一下目录权限。
drwxr-xr-x. 4 nobody root 139 Jan 21 02:16 logs
现在,就可以创建 access.log 了。
-rw-r--r--. 1 root root 416 Jan 15 01:21 access.log
-rw-r--r--. 1 root root 5965 Jan 20 23:21 error.log
-rw-r--r--. 1 nobody nobody 107 Jan 20 23:02 host.access_20190120.log
-rw-r--r--. 1 root root 1672 Jan 15 06:40 host.access.log
-rw-r--r--. 1 root root 6 Jan 20 23:22 nginx.pid
drwxr-xr-x. 2 nobody root 60 Jan 21 02:41 test1
drwxr-xr-x. 2 nobody root 33 Jan 20 23:12 test2
6.2 Nginx 完整配置
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
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"';
log_format test1 '{"time":"$time_iso8601",'
'"header":{'
'"remote_ip":"$remote_addr", "real_ip":"$http_x_forwarded_for", "http_code":"$status", "body_bytes_sent":"$body_bytes_sent",'
'"req_time":"$request_time", "res_time":"$upstream_response_time", "user_ua":"$http_user_agent", "http_referer":"$http_referer", "user_cookie":"$cookie_uid"'
'},'
'"arguments":{'
'"dot":"$arg_dot", "gender":"$arg_gender", "weChatId":"$arg_weChatId", "ua":"$arg_ua", "test":"$arg_test2"'
'}'
'}';
log_format test2 '{"time":"$time_iso8601",'
'"header":{'
'"remote_ip":"$remote_addr", "real_ip":"$http_x_forwarded_for", "http_code":"$status", "body_bytes_sent":"$body_bytes_sent",'
'"req_time":"$request_time", "res_time":"$upstream_response_time", "user_ua":"$http_user_agent", "http_referer":"$http_referer", "user_cookie":"$cookie_uid"'
'},'
'"arguments":{'
'"dot":"$arg_dot", "gender":"$arg_gender", "weChatId":"$arg_weChatId", "ua":"$arg_ua", "test":"$arg_test"'
'}';
access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
listen 80;
server_name localhost;
#charset koi8-r;
if ($time_iso8601 ~ "^(\d{4})-(\d{2})-(\d{2})") {
set $year $1;
set $month $2;
set $day $3;
}
access_log logs/host.access_${year}${month}${day}.log main;
location / {
root html;
index index.html index.htm;
}
location /hello {
echo "<br/>hello, use echo!";
}
location /monitor/mobile/u100001/p100001/00 {
access_log logs/test1/access_$year$month$day.log test1;
add_header Content-Type 'text/html';
add_header X-REQUEST-PATH '/tmp/logs/test';
return 200;
}
location /monitor/mobile/u100001/p100002/00 {
access_log logs/test2/access_$year$month$day.log test2;
add_header Content-Type 'text/html';
add_header X-REQUEST-PATH '/tmp/logs/test';
return 200;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}