替换为新日志文件后,访问日志不会记录


Access log won't log after replacing with a new log file

我正在尝试在我的Nginx服务器上表达访问日志文件。

为了措辞文件,我只需重命名原始访问日志文件并立即创建一个新的访问日志文件,这样我就不会错过任何东西。

但是在替换文件后,Nginx 不会将任何内容记录到该文件上,而是在我替换文件之前工作。

Nginx在我重新启动Nginx后再次开始记录到替换的文件。

看不出我做错了什么,有什么帮助吗?

PHP 代码的第一部分

if(rename("access.log", $tempname)){ // I'm renaming the access log file
    $fp = fopen("access.log","wb"); 
    if( $fp == false ){
    }else{
        fwrite($fp,$content); // I'm creating a new access log file
        fclose($fp);
    }
    // I'm phrasing the renamed file here
}

正如我在评论中所说,由于nginx的性质,可能无法删除该文件,我的建议是使用相同的方法,但不实际删除日志文件。相反,只需清除它。

伪代码

file = open "nginx.log", READ
new_file = open tmpname, WRITE
new_file.write file.contents
file.close
new_file.close
sys_command "cat /dev/null > nginx.log"

或使用脚本

#!/bin/bash
cp nginx.log nginx.backup.log
cat /dev/null > nginx.log

这样,您就不会破坏文件,nginx拥有的文件句柄仍然有效。