默认情况下,Ngnix的日志会保存到一个文件中,比如access日志,长时间不清理,日志文件就会变的很大,影响性能。
下面实现将access日志文件按照天来存放的功能,代码主要来自网络。
$time_local 的格式为:local time in the ISO 8601 standard format
[28/Dec/2018:09:01:23 +0800]
$time_iso8601 的格式为:local time in the Common Log Format
[2018-12-27T18:32:45+08:00]
Nginx Rewrite 规则相关指令:
if指令
set指令:定义一个变量
if ($time_iso8601 ~ '(\d{4}-\d{2}-\d{2})') {
set $tttt $1;
}
access_log logs/access-$tttt.log main;
$time_iso8601 变量
~ 为区分大小写匹配
'(\d{4}-\d{2}-\d{2})' 正则表达式
具体参考https://www.cnblogs.com/netsa/p/6383094.html
具体配置文件如下:
http {
include mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_iso8601] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
upstream server{
#ip_hash;
server 127.0.0.1:8080;
#server 127.0.0.1:8012;
}
server {
listen 80;
server_name localhost;
charset utf-8;
if ($time_iso8601 ~ '(\d{4}-\d{2}-\d{2})') {
set $tttt $1;
}
access_log logs/access-$tttt.log main;
#代理配置参数
proxy_connect_timeout 180;
proxy_send_timeout 180;
proxy_read_timeout 180;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
#反向代理的路径(和upstream绑定),location 后面设置映射的路径
location / {
proxy_pass http://server;
}
}
}