vim /usr/local/nginx/conf/nginx.conf //搜索log_format = 配置文件里面可以查找到日志格式
定义日志是需要在虚拟主机里面去定义的,nginx里面如果修改了日志格式的名字那么在虚拟主机中定义日志文件需要引用nginx配置文件里面的名字。
vim /usr/local/nginx/conf/vhost/test.com.log =定义虚拟主机日式文件
定义完以后 -t && -s reload
[root@aming-01 vhost]# /usr/local/nginx/sbin/nginx -t = 检测配置文件语法是否错误
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@aming-01 vhost]# /usr/local/nginx/sbin/nginx -s reload = 重新加载配置文件
测试配置:状态码为200说明ok
cat /tmp/test.com.log = 查看日志里面的信息
nginx日志切割需要使用到系统自带的工具,或者写一个切割的脚本
vim /usr/local/sbin/nginx_log_rotate.sh//写入如下内容 = 配置nginx需要的日志切割脚本
#! /bin/bash
## 假设nginx的日志存放路径为/data/logs/
d=`date -d "-1 day" +%Y%m%d` = 定义时间(昨天的日期)
logdir="/data/logs" =定义日志存放路径
nginx_pid="/usr/local/nginx/logs/nginx.pid"
cd $logdir = 进入到日志目录下
for log in `ls *.log` = for循环匹配
do
mv $log $log-$d = 更改所有配置文件的名字
done
/bin/kill -HUP `cat $nginx_pid`
配置好切割脚本以后可以执行测试一下脚本 sh -x 加上脚本路径 -x =可以显示出脚本执行过程
[root@aming-01 vhost]# sh -x /usr/local/sbin/nginx_log_rotate.sh
++ date -d '-1 day' +%Y%m%d
+ d=20180425
+ logdir=/tmp/
+ nginx_pid=/usr/local/nginx/logs/nginx.pid
+ cd /tmp/
++ ls test.com.log
+ for log in '`ls *.log`'
+ mv test.com.log test.com.log-20180425
++ cat /usr/local/nginx/logs/nginx.pid
+ /bin/kill -HUP 1089
写完脚本以后还需要加上一个任务计划:
crontab -e = 任务计划
0 0 * * * /bin/bash /usr/local/sbin/nginx_log_rotate.sh = 表示每天凌晨0点执行脚本
同样是在虚拟主机中配置
vim /usr/local/nginx/conf/vhost/test.com.conf = 测试的虚拟主机配置文件
配置完以后还需要 -t && -s reload 检查配置文件语法是否有错误和重新加载配置文件
[root@aming-01 vhost]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@aming-01 vhost]# /usr/local/nginx/sbin/nginx -s reload
测试:curl -x127.0.0.1:80 test.com/1.gif/2.gs
cat /tmp/test.con.log