linux - 仅在 access_log 中记录 4xx 和 5xx 日志 - Nginx 1.5.12.1

标签 linux nginx

我的 nginx 版本是:openresty/1.5.12.1。

有没有办法只将 4xx 和 5xx 日志记录到 access_log 或附加 access_log 文件中?

我的意思是,这些日志应该存在于默认的 access_log 文件中。此外,它们也应该记录在自定义日志文件中。 仅记录 4xx 和 5xx 也很好。

谢谢!

最佳答案

不,这对于 nginx 1.5.x 来说似乎是不可能的。

是的,从 nginx 1.7.0 ( http://nginx.org/en/docs/http/ngx_http_log_module.html#access_log ) 开始,可以在 nginx.conf 文件中执行此操作:

nginx access_log directive 'if' parameter

来自https://docs.nginx.com/nginx/admin-guide/monitoring/logging/#conditional :

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

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

这将跳过记录所有 2xx 和 3xx 状态代码。您可以重写此代码以积极选择 4xx 和 5xx 状态代码(尽管这些在很大程度上是等效的,除非您返回大量 1xx 状态代码或返回非标准 > 599 状态代码,这会很奇怪):

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

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

关于linux - 仅在 access_log 中记录 4xx 和 5xx 日志 - Nginx 1.5.12.1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32555379/

相关文章:

php - Laravel Valet php-fpm已经在代客 socks 上听了

c - 如何向 Linux 系统添加/注册唯一的 errno 和相应的错误消息?

nginx - 两个子域配置

linux - JavaFXSceneBuilder1.1 未在 Ubuntu Linux 中启动

linux - 每隔一行剪切字段并将结果连接到一个没有空格的字符串

caching - 如何配置 cloudfoundry staticfile buildpack 以防止在发布后从浏览器缓存加载

docker - 如何自动停止docker容器?

ruby-on-rails - Rails 使用 send_file 发送 0 字节文件

python - (Python/Linux) 通过 python 或 os.system 命令无延迟地播放 wav 文件

linux - GNUPlot : How to plot a sphere and imported file on the same frame?