启动
格式:./nginx -s reload
- reload:重载配置文件,不会丢失请求
- stop:立刻停止
- Quit:优雅的停止
- Reopen:重新记录日志文件
- -t:检查配置文件是否正确
重新编译
在解压后的目录下,我们可以查看将要被安装的模块
- cat auto/options | grep YES
或者在sbin下,查看自定义安装的模块
- ./nginx -V
比如我想添加http_v2_module模块:
1.在源码路径下重新编译
- ./configure —prefix=/home/jw/nginx —with-http_v2_module [—原本就有的模块]
2.make ,(不要make install)
热部署
- 备份nginx:cp nginx nginx.old
2. ./configure,将objs目录下的nginx执行文件cp到sbin下,cp后面加-f,否则提示busy
3. 发信号,通知要进行热部署,kill -USR2 25512
4. 发信号,让老的work优雅的退出,kill -WINCH 25512,当老版本没有worker,说明所有的request都切换到新版本中了
日志切割
- 备份 mv access.log access_20180503.log
2. ./nginx -s reopen
实际使用:写个定时脚本,执行上述两条命令
python2.7
#!/usr/bin/python# -*- coding: utf-8 -*-import os, sys, datetime,re# nginx日志存放的路径nginxLogPath="/home/jw/nginx/logs/"# 获取昨天的日期yesterday = (datetime.date.today() + datetime.timedelta(days = -1)).strftime("%Y-%m-%d")# nginx启动的pid文件PID = "/home/jw/nginx/logs/nginx.pid"def cutNginxLog(path):logList = os.listdir(path) # 判断传入的path是否是目录for logName in logList: # 循环处理目录里面的文件suffix = os.path.splitext(logName)[1]if suffix == ".log":# 分割日志re_Num = re.compile(r'^[a-zA-Z]')# 判断日志文件是否是字母开头(要切割的error.log或access.log),如果是日期开头就不切割if re_Num.search(logName):logNewName = yesterday + "_" + logName # 新日志文件名称列如:2018-11-8_access.logoldLogPath = os.path.join(path, logName) # 旧日志文件绝对路径newLogPath = os.path.join(path, logNewName) # 新日志文件绝对路径os.rename(oldLogPath, newLogPath)cmd = " kill -USR1 `cat %s` "% PIDres = os.system(cmd)if res != 0:return "重新加载nginx失败"cutNginxLog(nginxLogPath)
shell
#!/bin/bash#设置日志文件存放目录#LOG_HOME="/home/wwwlogs/"LOG_HOME="/usr/local/nginx/logs"#备分文件名称LOG_PATH_BAK="$(date -d yesterday +%Y%m%d%H%M)".access.log#重命名日志文件mv ${LOG_HOME}/access.log ${LOG_HOME}/${LOG_PATH_BAK}#向nginx主进程发信号重新打开日志kill -USR1 `cat /usr/local/nginx/logs/nginx.pid`# crontab -e*/1 * * * * /opt/nginx/nginx_log.sh
计划任务
每天凌晨00:00定时执行这个脚本:* * * * *:分 时 日 月 周crontab -e00 00 * * * /home/jw/nginx/logs/logCut.py

日志删除
shell
# touch auto-del-7-day-ago-log.sh# chmod +x auto-del-7-day-ago-log.sh# vim auto-del-7-day-ago-log.shfind /www/cookie.com/log.cookie.com/ -mtime +7 -name "push*.log" -exec rm -rf {} \;# crontab -e10 0 * * * /opt/sh/auto-del-7-day-ago-log.sh > /dev/null 2>&1
