nginx系列4:日志管理

日志切割

若是使用默认日志配置,通过一段时间运行后,access.log和error.log文件会变得很是大,使维护和排查问题变得不便,因此很是有必要作日志切割。html

一般的思路是:使用nginx的-s reopen命令,结合linux系统的crontab定时任务命令,弄一个定时任务按时切割日志文件。linux

天天定时执行脚本切割日志文件。nginx

image

附:bash脚本shell

#!/bin/bash
#Rotate the nginx logs to prevent a single logfile from consuming too much disk space.
LOGS_PATH=/usr/local/nginx/logs/history
CUR_LOGS_PATH=/usr/local/nginx/logs
YESTERDAY=$(date -d "yesterday" +%Y-%m-%d)
mv ${CUR_LOGS_PATH}/access.log ${LOGS_PATH}/access_${YESTERDAY}.log
mv ${CUR_LOGS_PATH}/error.log ${LOGS_PATH}/error_${YESTERDAY}.log
#向nginx master进程发送USR1信号,USR1信号是从新打开日志文件,至关于-s reopen命令
kill -USR1 $(cat /usr/local/nginx/logs/nginx.pid)

access日志的详细用法

1,log_format指令用来定义日志的格式,做用在上下文http中。bash

image

默认的main日志格式以下:spa

image

2,定义日志文件,经过access_log指令来完成。.net

image

默认的access_log是放在http上下文中,咱们也能够为每一个server块定义access_log,以下图。日志

image

参考资料

  1. crontab:至关于Windows下的任务计划,参考:http://www.javashuo.com/article/p-yjmnxdsd-cy.html
  2. bash脚本:是Linux下Shell的其中一种,相似于Windows下Shell的cmd,参考:https://blog.csdn.net/cy1070779077/article/details/85034083http://www.runoob.com/linux/linux-shell.html
相关文章
相关标签/搜索