@yanglt7
2018-10-21T15:54:04.000000Z
字数 3013
阅读 690
Web集群实战
log_format | 用来定义记录日志的格式 |
---|---|
access_log | 用来指定日志文件的路径及使用何种日志格式记录日志 |
- Nginx 日志变量
$remote_addr | 客户端地址 |
---|---|
$remote_user | 用于HTTP基础认证服务的用户名 |
$request | 代表客户端的请求地址 |
$status | HTTP响应代码 |
$time_local | 服务器时间(LOG Format 格式) |
$body_bytes_sent | 传输给客户端的字节数,响应头不计算在内;这个变量和 Apache 的 mod_log_config 模块中的"%B"参数保持兼容 |
$http_referer | url 跳转来源,用来记录从那个页面链接访问过来的 |
$http_user_agent | 用户终端浏览器等信息 |
参考链接:https://www.cnblogs.com/wajika/p/6426270.html
[root@ylt001 conf]# sed -n '21,23 s/#//gp' 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"';
worker_processes 1;
error_log logs/error.log;
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"';
sendfile on;
keepalived_timeout 65;
include extra/www.conf;
include extra/bbs.conf;
include extra/blog.conf;
include extra/statu.conf;
}
[root@ylt001 conf]# cat extra/www.conf
#www virtualhost by ylt
server {
listen 80;
server_name www.yangyangyang.org yangyangyang.org;
location / {
root html/www;
index index.html index.htm;
}
}
[root@ylt001 conf]# ../sbin/nginx
[root@ylt001 conf]# ../sbin/nginx -s reload
[root@ylt001 conf]# curl www.yangyangyang.org
www.yangyangyang.org
[root@ylt001 conf]# ll ../logs/access_www.log
-rw-r--r-- 1 root root 3595 Sep 19 14:47 ../logs/access_www.log
[root@ylt001 conf]# tail -1 ../logs/access_www.log
[root@ylt001 conf]# tail -1 ../logs/access_www.log
[root@ylt001 conf]# cat /server/script/cut_nginx_log.sh
#!/bin/sh
Dateformat=`date +%Y%m%d`
Basedir="/application/nginx"
Nginxlogdir="$Basedir/logs"
Logname="access_www"
[ -d $Nginxlogdir ] && cd $Nginxlogdir || exit 1
[ -f ${Logname}.log ] || exit 1
/bin/mv ${Logname}.log ${Dateformat}_${Logname}.log
$Basedir/sbin/nginx -s reload
注:脚本实现切割 Nginx 日志的思想为将正在写入的 Nginx 日志(access_www.log)改名为带日期的格式文件(20180919_access_www.log),然后平滑重新加载 Nginx,生成新的 Nginx 日志(access_www.log)。
[root@ylt001 logs]# cat >>/var/spool/cron/root <<EOF
>#cut nginx access log by ylt001
>00 00 * * * /bin/sh /server/script/cut_nginx_log.sh >/dev/null 2>&1
>EOF
[root@ylt001 ~]# cd /application/nginx/logs
[root@ylt001 logs]# ll
[root@ylt001 ~]# /bin/sh /server/script/cut_nginx_log.sh
[root@ylt001 ~]# cd /application/nginx/logs
[root@ylt001 logs]# ll
total 56
-rw-r--r-- 1 root root 4417 Sep 19 15:58 20180919_access_www.log
-rw-r--r-- 1 root root 105 Sep 19 15:30 access_bbs.log
-rw-r--r-- 1 root root 189 Sep 19 14:46 access_blog.log
-rw-r--r-- 1 root root 15520 Sep 18 21:38 access.log
-rw-r--r-- 1 root root 0 Sep 19 16:26 access_www.log
-rw-r--r-- 1 root root 17912 Sep 19 16:26 error.log