将nginx日志按照日期切割如下图所示:
需要启动nginx自己自带的切割方式,只需要在nginx.conf中配置:
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include 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" $upstream_addr $upstream_response_time $request_time $uri http_origin:$http_origin';
#access_log logs/access.log main;
sendfile on;
keepalive_timeout 65;
#这里是启动日志以日期的方式存储
map $time_iso8601 $logdate {
'~^(?<ymd>\d{4}-\d{2}-\d{2})' $ymd;
default 'date-not-found';
}
#日志存储路径
access_log /usr/local/nginx/logs/access-$logdate.log;
server {
listen 80;
server_name localhost;
location / {
root html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
问题:配置完成后没有access.log没有以日期格式存储
解决:
vim nginx.conf
user root; #配置user root;
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
然后重新加载nginx.conf
/usr/local/nginx/sbin/nginx -s reload