51工具盒子

依楼听风雨
笑看云卷云舒,淡观潮起潮落

修改Nginx配置实现日志按天保存

第一种方案 {#第一种方案}

修改配置 {#修改配置}

修改前

|---------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | 1 2 3 | log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; |

修改后
$time_local改成$time_iso8601

|---------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | 1 2 3 | log_format main '$remote_addr - $remote_user [$time_iso8601] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; |

然后在server 中添加以下代码

|-----------------|---------------------------------------------------------------------------------------------------------------| | 1 2 3 4 | if ($time_iso8601 ~ '(\d{4}-\d{2}-\d{2})') { set $time $1; } access_log logs/$time.access.log main; |

完整配置(参考) {#完整配置(参考)}

|---------------------------------------------------------------------------------------------------------------------------------------------------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 | user root; worker_processes 1; #error_log logs/error.log; #error_log logs/error.log notice; error_log logs/error.log debug; #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_iso8601] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; #access_log logs/$tttt.access.log main; access_log logs/access.log main; sendfile on; #tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 65; #gzip on; server { listen 8888; server_name localhost; #charset koi8-r; if ($time_iso8601 ~ '(\d{4}-\d{2}-\d{2})') { set $time $1; } access_log logs/$time.access.log main; #access_log logs/host.access.log main; location / { #root html; #index index.html index.htm;#root html; index index.html index.htm; proxy_pass http://testTomcat/ssm/; } } } |

重启Nginx {#重启Nginx}

修改完保存退出,重启nginx 命令:./nginx -s reload

出错处理 {#出错处理}

如果error.log出现报错

|-----------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | 1 | 2020/04/28 22:47:55 [crit] 7727#0: *32 open() "/usr/local/nginx/logs/2020-04-28.access.log" failed (13: Permission denied) while logging request, client: 192.168.175.1, server: localhost, request: "GET / HTTP/1.1", upstream: "http://192.168.175.171:8880/ssm/", host: "192.168.175.170:8888" |

这是因为当前用户没有权限open() "/usr/local/nginx/logs/2020-04-28.access.log" 可以使用chown命令重新设置拥有者。

我这里文件的拥有者为root 所以nginx.conf配置文件中设置成 user root;也可以。

第二种方案 {#第二种方案}

  1. 在nginx中加入以下配置。

    |---------------------|-----------------------------------------------------------------------------------------------------------------------------------| | 1 2 3 4 5 6 | map $time_iso8601 $logdate { '~^(?<ymd>\d{4}-\d{2}-\d{2})' $ymd; default 'nodate'; } access_log logs/access-$logdate.log; |

  2. 加入位置如下所示

    |------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; # 日志样式 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 'date-not-found'; } access_log /var/log/nginx/access.log main; # 增加一个日志 access_log /var/log/nginx/access${logdate}.log main; server { ... } } |

    添加一个日志来做按天保存日志的原因是,nignx镜像做了重定向设置,将默认的日志输出到了stdout,所以无法直接保存日志文件,我的做法就是增加一个新的日志来保存。

  3. 加入之后保存
    Esc :wq(保存命令)

  4. 执行测试指令
    保存之后,可以先去sbin目录下执行 ./nginx -t,查看文件是否可用。

  5. 修改logs文件夹的权限(改成自己的位置,我的是放在/usr/local/nginx下的):

    |-----------|--------------------------------------------| | 1 | chmod -R 777 /usr/local/nginx/logs |

  6. 完成之后重启nginx

    |-----------|-------------------------| | 1 | nginx -s reload |

  7. 然后访问应用即可查看按天生成的日志

参考链接:https://blog.csdn.net/weixin_43565319/article/details/105827924
参考链接:https://blog.csdn.net/weixin_45544011/article/details/125408227


赞(5)
未经允许不得转载:工具盒子 » 修改Nginx配置实现日志按天保存