最近我在
log_not_found
配置中添加了 Nginx
指令。工作正常,可以防止 Nginx
在 error.log
上打印 404 日志,但仍然在 access.log
上打印 404 日志。
如何禁用
Nginx
404 登录access.log
?
172.18.xx.xx - - [02/Sep/2024:11:38:00 +0000] "GET /test HTTP/2.0" 404 131 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36" "219.100.xx.xx"
http {
log_format main
'$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
server {
listen 443 ssl;
root /example/path/html;
log_not_found off;
access_log /var/log/nginx/access.log main;
error_log /var/log/nginx/error.log warn;
location / {
add_header "Access-Control-Allow-Origin" "*" always;
try_files $uri =404;
}
}
}
其实
Nginx
并没有做错什么。每个请求都应该登录access.log
。
目前我使用
access_log
完全关闭 location
中的日志。
location /favicon.ico {
access_log off;
}
除了此解决方法之外,我还向 Nginx
GitHub 存储库提交了
功能请求。