Nginx logs for statistics, system services, debugging very useful, but the original configuration program, log difficult to locate the problem. So imagine the nginx log categories, including access and error logs. And are classified according to different domain names and logs.
Configuring nginx log directory
Easy to manage, recommend a different server in a different .conf separate, easy to post-maintenance, and then introduced in nginx.conf.
vim /usr/local/nginx/conf/nginx.conf
# 引入其他server块的配置文件
include vhost/www.conf;
include vhost/database.conf;
Configuration directory server log in block www.conf, database.conf of
access_log /data/logs/nginx/www/nginx_access.log main;
error_log /data/logs/nginx/www/nginx_error.log error;
access_log /data/logs/nginx/database/nginx_access.log main;
error_log /data/logs/nginx/database/nginx_error.log error;
Write scripts automatically execute cutting logs as follows cut_nginx_log.sh
M=$(date +%Y%m)
D=$(date +%m%d)
WWW_NGINX_LOGS_DIR=/data/logs/nginx/www/
WWW_NGINX_ACCESS_LOG=/data/logs/nginx/www/nginx_access.log
WWW_NGINX_ERROR_LOG=/data/logs/nginx/www/nginx_error.log
DATABASE_NGINX_LOGS_DIR=/data/logs/nginx/database/
DATABASE_NGINX_ACCESS_LOG=/data/logs/nginx/database/nginx_access.log
DATABASE_NGINX_ERROR_LOG=/data/logs/nginx/database/nginx_error.log
PHP_LOGS_DIR=/data/logs/php/
# 裁剪www域名下的nginx日志
if [ -f "$WWW_NGINX_ACCESS_LOG" ]; then
echo 'www域名下nginx_access.log文件存在,开始裁剪日志'
if [ ! -d "$WWW_NGINX_LOGS_DIR$M" ]; then
mkdir -p "$WWW_NGINX_LOGS_DIR$M"
echo '文件夹创建成功'
mv ${WWW_NGINX_ACCESS_LOG} ${WWW_NGINX_LOGS_DIR}${M}/nginx_access_${D}.log
fi
else
echo 'www域名下nginx_access.log不文件存在'
fi
if [ -f "$WWW_NGINX_ERROR_LOG" ]; then
echo 'www目录下nginx_error.log文件存在,开始裁剪日志'
if [ ! -d "$WWW_NGINX_LOGS_DIR$M" ]; then
mkdir -p "$WWW_NGINX_LOGS_DIR$M"
echo '文件夹创建成功'
mv ${WWW_NGINX_ERROR_LOG} ${WWW_NGINX_LOGS_DIR}${M}/nginx_error_${D}.log
fi
else
echo 'www域名下nginx_error.log不文件存在'
fi
# 裁剪database域名下的nginx日志
if [ -f "$DATABASE_NGINX_ACCESS_LOG" ]; then
echo 'database域名下nginx_access.log存在,开始裁剪日志'
if [ ! -d "$DATABASE_NGINX_LOGS_DIR$M" ]; then
mkdir -p "$DATABASE_NGINX_LOGS_DIR$M"
echo '文件夹创建成功'
mv ${DATABASE_NGINX_ACCESS_LOG} ${DATABASE_NGINX_LOGS_DIR}${M}/nginx_access_${D}.log
fi
else
echo 'database域名下nginx_access.log不存在'
fi
if [ -f "$DATABASE_NGINX_ERROR_LOG" ]; then
echo 'database域名下nginx_error.log存在,开始裁剪日志'
if [ ! -d "$DATABASE_NGINX_LOGS_DIR$M" ]; then
mkdir -p "$DATABASE_NGINX_LOGS_DIR$M"
echo '文件夹创建成功'
mv ${DATABASE_NGINX_ERROR_LOG} ${DATABASE_NGINX_LOGS_DIR}${M}/nginx_error_${D}.log
fi
else
echo 'database域名下nginx_error.log不存在'
fi
kill -USR1 $(cat /usr/local/nginx/
logs/nginx.pid)
Run crontab -e
the script to crontab task
00 00 * * * /bin/bash /data/logs/cut_nginx_log.sh