nginx+php implements cleaning server website logs under Linux system

little bottle
Release: 2023-04-06 11:58:01
forward
3058 people have browsed it

本篇文章主要讲述的是在Linux系统下用nginx和php实现清理服务器网站日志,具有一定参考价值,感兴趣的朋友可以了解一下,希望对你能有所帮助。 

1.清空nginx站点日志的内容(如果删除日志文件,只有重启服务器才能重新生成日志文件进行记录)

编辑脚本 

添加定时任务,每月的14号和28号的12点0分执行清理脚本

相关教程:Linux视频教程   nginx视频教程

2.删除iis站点过期的日志(我的日志是天计划,另外一点需要注意的是不能删除当天的日志,因为会有警告说已经在系统打开)


<?php
/* 
    清理IIS网站过期日志,释放C盘空间 
*/

$logs_path = "C:\inetpub\logs\LogFiles";    //日志所在路径
$folder_head = "W3SVC";    //日志文件夹名的前缀
$file_head = "u_ex";    //日志文件名的前缀
$file_foot = ".log";    //日志文件名的后缀

$logs_path = str_replace("\\", "/", $logs_path);
if (! file_exists($logs_path)) die(&#39;日志目录不存在&#39;);
$scan = scandir($logs_path);
$folders = array();    //定义变量存在日志文件夹名

for ($i=0; $i < count($scan); $i++) { 
    if((substr($scan[$i], 0, strlen($folder_head)) == $folder_head) && file_exists($logs_path.&#39;/&#39;.$scan[$i]))
        array_push($folders, $scan[$i]);
}

$today = date(&#39;Ymd&#39;);
$today = substr($today, 2);
$todayLog = $file_head.$today.$file_foot;


for ($i=0; $i < count($folders); $i++) { 

    $filesInFolder = scandir($logs_path.&#39;/&#39;.$folders[$i]);

    for ($j=0; $j < count($filesInFolder); $j++) { 
        if(($filesInFolder[$j] != $todayLog) && 
            (substr($filesInFolder[$j], 0, strlen($file_head)) == $file_head) &&
            (strrev(substr(strrev($filesInFolder[$j]), 0,strlen($file_foot))) == $file_foot))
            unlink($logs_path.&#39;/&#39;.$folders[$i].&#39;/&#39;.$filesInFolder[$j]);
    }
}

echo &#39;过期日志清理完毕&#39;;

?>
Copy after login


编辑删除脚本 clear.php

编辑bat批处理文件,存放位置任意,双击该文件即可完成删除,此处不做定时任务,手动删除

 相关教程:PHP视频教程 

The above is the detailed content of nginx+php implements cleaning server website logs under Linux system. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:cnblogs.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template