关于nginx容器日志按天保存的配置

nginx 容器日志按天保存

  1. nginx 容器按天保存日志配置:
http {
  # 日志样式
  log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
  '$status $body_bytes_sent "$http_referer" '
  '"$http_user_agent" "$http_x_forwarded_for"';
  # 按天保存文件
   map $time_iso8601 $logdate {
    '~^(?<ymd>\d{4}-\d{2}-\d{2})' $ymd; default 'nodate';
   }

  access_log  /var/log/nginx/access.log  main;
  # 增加一个日志
  access_log  /var/log/nginx/access${logdate}.log  main;
  1. 执行效果:
    在这里插入图片描述
  2. 添加一个日志来做按天保存日志的原因是,nignx镜像做了重定向设置,将默认的日志输出到了stdout,所以无法直接保存日志文件,我的做法就是增加一个新的日志来保存。