大网数据广告活动
服务器运维问答

nginx log 404 on different log file

· 52CE 我爱测

How can i get all 404 errors from all the sites into one log? I only know how to do it on one domain and only when i change the default 404 page. Here is how i did it on one domain error_page 404 /404.html; location = /404.html {…

问题原文

原帖链接 · Bill · CC BY-SA 3.0

How can i get all 404 errors from all the sites into one log? I only know how to do it on one domain and only when i change the default 404 page.

Here is how i did it on one domain

    error_page 404 /404.html;
    location = /404.html {
            root /home/awesomename;
            access_log /home/custom_logs/404.log;
            internal;
    }

Is there a way to do it without changing anything else?

已采纳解答

原帖链接 · Nomadic · CC BY-SA 3.0

The if parameter (1.7.0+) enables conditional logging. A request will not be logged if the condition evaluates to “0” or an empty string. In the following example, the requests with response codes 4xx will be logged:

map $status $loggable {
    ~^[4]   1;
    default 0;
}

access_log /path/to/access.log combined if=$loggable;

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2017-04-20)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。