nginx配置文件
worker_processes 4;
events {
worker_connections 1024;
}
http {
include /home/nginx/conf/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" $status $body_bytes_sent "$http_referer" "$http_user_agent" "$http_x_forwarded_for"';
access_log /home/nginx/logs/access.log main;
sendfile on;
keepalive_timeout 300;
fastcgi_buffers 8 128k;
send_timeout 300;
server {
listen 8001;
server_name localhost;
location / {
include /home/nginx/conf/uwsgi_params;
uwsgi_pass 127.0.0.1:9090;
# proxy_pass http://127.0.0.1:9001;
proxy_send_timeout 300;
proxy_read_timeout 300;
fastcgi_read_timeout 600;
fastcgi_send_timeout 600;
}
}
}
uwsgi配置文件
[uwsgi]
socket = 127.0.0.1:9090
chdir = /home/cloud-python/pythonserver # django程序存放文件夹
module = pythonserver.wsgi:application
master = true
processes = 24
pidfile = /home/cloud-python/pythonserver/pythonserver-9090.pid
daemonize = /home/logs/uwsgi-9090.log
touch-logreopen = /home/logs/.touchforlogrotat # 用来监听日志文件,该文件不需要手动创建
如果uwsgi已经配置好了,就只需要添加这句话就可以了,touch-logreopen = /home/logs/.touchforlogrotat
以上注意nginx反向代理监听的端口是uwsgi的socket
定时切割uwsgi日志
脚本文件名及路径:/home/shell/touchforlogrotat.sh
脚本内容:
#!/bin/bash
LOGDIR= "/home/logs/"
sourcelogpath="/home/logs/uwsgi-9090.log"
touchfile="/home/logs/.touchforlogrotat"
DATE=`date -d "yesterday" +"%Y-%m-%d"`
destlogpath="/home/logs/uwsgi-9090-${DATE}.log"
mv $sourcelogpath $destlogpath
touch $touchfile
脚本在切分uwsgi-9090.log文件后程序不会停止写入,不用重启uwsgi,如果是用sed或echo修改uwsgi-9090.log日志文件后需要重启uwsgi,才能继续写入。
创建定时任务
利用crontab创建定时任务
执行命令 crontab -e 写入
59 23 * * * sh /home/shell/touchforlogrotat.sh
删除过期的日志
删除脚本放在/home/shell/delede-uwsgi-log.sh
#!/bin/bash
# 删除15天前所有名字中含有log的文件
find /home/logs/ -mtime +15 -name "*log*" -exec rm -f {} \;
# 删除15天之前的以.log结尾的文件
# find /home/logs/ -mtime +15 -type f -name \*.log | xargs rm -f
这两个都可以根据需求选择
创建定时任务
利用crontab创建定时任务
执行命令 crontab -e 写入
0 0 * * * sh /home/shell/delete-uwsgi-log.sh