如何configurationApache不让access.log或error.log一直打开虚拟主机?

我想降低我的VPS上打开文件的数量。 使用lsof我注意到,Apache不断保持access.log和error.log为服务器上托pipe的每个域打开。

有没有办法改变这种行为?

许多打开的文件通常不是问题。 有一个系统限制,你可以检查使用命令sysctl fs.file-max – 在我的CentOS 6系统与4GB内存是382299,这应该是很多,在256MB的虚拟机,我开始是23539,这可能有点太小。 但是你可以通过添加/etc/sysctl.conf来轻松地增加这个限制:

 fs.file-max = 500000 

然后运行sysctl -p或重新启动。

您可以编写一个程序,每次从stdin收到数据时都会打开,写入,closures日志文件,并使用如下所示:

 CustomLog "||open-write-close /var/log/httpd/access_log" common 

但效率不高。 而且不容易,因为它需要使用asynchronousIO。


我已经写了一个这样的程序在C:

 #include <sys/types.h> #include <sys/stat.h> #include <unistd.h> #include <fcntl.h> #include <string.h> #include <stdio.h> #include <errno.h> #include <limits.h> int outfd; int main(int argn, char* argv[]) { if (argn != 2) { fprintf(stderr, "Usage %s [output_file]\n", argv[0]); return 1; } { long stdinflags = fcntl(0,F_GETFL); if ( stdinflags == -1 ) { perror("fcntl: Can't read stdin status"); return 2; } if ( fcntl(0,F_SETFL,stdinflags|O_NONBLOCK) == -1 ) { perror("fcntl: Can't set stdin to non-blocking mode"); return 2; } } do { #define BUFSIZE PIPE_BUF char buf[BUFSIZE]; ssize_t bytesread = read(0, buf, BUFSIZE); { fd_set select_fdset; FD_ZERO(&select_fdset); FD_SET(0, &select_fdset); if ( select(1, &select_fdset, NULL, NULL, NULL) == -1 ) { if ( errno != EINTR ) { perror("select"); return 2; } }; } if ( bytesread==-1 ) { perror("Can't read from stdin"); return 2; } if ( bytesread == 0 ) break; outfd = open(argv[1],O_WRONLY|O_APPEND|O_CREAT); if ( outfd < 0 ) { fprintf(stderr, "Can't open file \"%s\": %s\n", argv[1], strerror(errno)); return 2; } do { if ( write(outfd, buf, bytesread) == -1 ) { fprintf(stderr, "Can't write to file \"%s\": %s\n", argv[1], strerror(errno)); return 2; }; bytesread = read(0, buf, BUFSIZE); if ( bytesread==-1 ) { if ( errno==EAGAIN ) break; perror("Can't read from stdin"); } } while ( bytesread>0 ); if ( close(outfd) != 0) { fprintf(stderr, "Can't close file \"%s\": %s\n", argv[1], strerror(errno)); return 2; } } while ( 1 ); return 0; } 

如果您的虚拟主机数量非常多,且具有单独的访问日志文件,则build议将所有内容都logging到一个文件中(是​​的,在每个虚拟主机中删除CustomLog),然后使用split-logfile应用程序将其分开。 您可以使用logresolve脚本将其join,以parsingDNS名称。

拆分日志文件的工作,您需要将CustomLog中的第一列设置为ServerName。