Nginx关闭网站某个文件的404记录或者关闭访问日志

来自:互联网
时间:2018-08-21
阅读:

很多时候,我们不想记录某个网站的URL或者文件的访问,这些东西太多会产生大量日志记录。包括某一些文件不存在记录的大量404错误日志,这里以测试文件1.php为例。

默认访问www.21yunwei.com/1.php错误日志会记录一些404报错,比如:

2017/08/10 10:51:05 [error] 9490#0: *406 open() "/data/wwwroot/21yunwei/errpage/404.html" fAIled (2: No such file or directory), client: 222.212.171.125, server: 21yunwei.com, request: "GET /1.php HTTP/1.1", host: "www.21yunwei.com"
2017/08/10 10:51:08 [error] 9490#0: *406 open() "/data/wwwroot/21yunwei/errpage/404.html" failed (2: No such file or directory), client: 222.212.171.125, server: 21yunwei.com, request: "GET /1.php HTTP/1.1", host: "www.21yunwei.com"
2017/08/10 10:51:42 [error] 9709#0: *450 open() "/data/wwwroot/21yunwei/errpage/404.html" failed (2: No such file or directory), client: 222.212.171.125, server: 21yunwei.com, request: "GET /1.php HTTP/1.1", host: "www.21yunwei.com"
2017/08/10 10:51:44 [error] 9709#0: *450 open() "/data/wwwroot/21yunwei/errpage/404.html" failed (2: No such file or directory), client: 222.212.171.125, server: 21yunwei.com, request: "GET /1.php HTTP/1.1", host: "www.21yunwei.com"
2017/08/10 10:51:47 [error] 9709#0: *450 open() "/data/wwwroot/21yunwei/errpage/404.html" failed (2: No such file or directory), client: 222.212.171.125, server: 21yunwei.com, request: "GET /1.php HTTP/1.1", host: "www.21yunwei.com"
2017/08/10 10:51:48 [error] 9709#0: *450 open() "/data/wwwroot/21yunwei/errpage/404.html" failed (2: No such file or directory), client: 222.212.171.125, server: 21yunwei.com, request: "GET /1.php HTTP/1.1", host: "www.21yunwei.com

server {}标签配置如下代码:

location  = /1.php  {
access_log off;
log_not_found off;
}

重新加载Nginx,查看错误日志已经没有再记录1.php的404记录了。 

说明:

1,log_not_found off;  默认为on,作用就是启用或禁用404错误日志(针对errorlog有效)

2,access_log off;    这个有效的前提是server{}标签中,其他地方没有启用access_log,否则还是会记录。

如果没有配置这个参数,默认是写入到nginx安装目录的logs/access.log,这个是记录全站的,可以修改全局配置文件nginx.conf设定。需要单独记录该站日志的话,可以单独从server{}标签中进行配置,指定输出日志,当然也可以指定输出错误日志access_log。如果不需要记录这个站的输出,直接设定一个access_log off;即可不记录这个站的日志输出。

ps:注意error_log off并不能关闭日志记录功能,它将日志文件写入一个文件名为off的文件中(off位置在/usr/local/nginx/off,即nginx的安装目录下),如果你想关闭错误日志记录功能,应使用以下配置:error_log /dev/null crit;

log_not_found off改成error_log off效果也一样,不同的是写成error_log off是将错误日志输出到off文件,而log_not_found则是关闭日志。

返回顶部
顶部