nginx日志切割
手动切割
随着时间的推移,日志文件越来越大,体积越来越多,分析日志,查看日志相当不方便,往往一天为单位。
- 创建一个shell可执行文件:cut_my_log.sh,内容为:
#!/bin/bash
LOG_PATH="/var/log/nginx/"
RECORD_TIME=$(date -d "yesterday" +%Y-%m-%d)
PID=/var/run/nginx/nginx.pid
mv ${LOG_PATH}/access.log ${LOG_PATH}/access.${RECORD_TIME}.log
mv ${LOG_PATH}/error.log ${LOG_PATH}/error.${RECORD_TIME}.log
#向Nginx主进程发送信号,用于重新打开日志文件
kill -USR1 `cat $PID`
- 为cut_my_log.sh添加可执行的权限:
chmod +x cut_my_log.sh - 测试日志切割后的结果:
./cut_my_log.sh
自动切割
用定时任务
- 安装定时任务:
yum install crontabs - crontab -e
59 23 * * * /usr/local/nginx/sbin/cut_my_log.sh - service crond restart
Nginx的跨域
#允许跨域请求的域,*代表所有
add_header ‘Access-Control-Allow-Origin’ *;
#允许带上cookie请求
add_header ‘Access-Control-Allow-Credentials’ ‘true’;
#允许请求的方法,比GET/POST/PUT/DELETE
add_header ‘Access-Control-Allow-Methods’ *;
#允许请求的header
add_header ‘Access-Control-Allow-Headers’ *;
Nginx防盗链配置支持
#对源站点验证
valid_referers *.zk103.com;
#非法引入会进入下方判断
if ($invalid_referer) {
return 404;
}