Nginx,使用正则表达式排除访问日志

yhuiod9q  于 2022-11-02  发布在  Nginx
关注(0)|答案(1)|浏览(175)

在我的nginx Web服务器的访问日志中,我有这样的日志:

XX.XX.147.5 - - [20/Oct/2022:18:42:59 +0000] "GET /my-subfolder/readiness HTTP/1.1" 200 1496 "-" "EF-Strati-HealthCheck-Client/1.0" "11.16.133.0"
XX.XX.94.5 - - [20/Oct/2022:18:42:59 +0000] "POST /my-subfolder/some-path-to-exclude/socket.io/?EIO=4&transport=polling&t=OFsjAj9&sid=2Mufsq23xBh8FRLWAFtD HTTP/1.1" 200 2 "https://www.myapp-example.com/my-subfolder/home" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36" "XXX,XXX,XXX,XXX"

如何制定规则以排除包含/socket.io/的访问日志?
有没有办法把一些正则表达式?像:

location ~ /(.*)socket.io(.*)/ {
  access_log off;
}

我试过了,但我一直收到日志:

XX.XX.29.4 - - [20/Oct/2022:21:03:13 +0000] "GET /my-subfolder/some-path-to-exclude/socket.io/?EIO=4&transport=polling&t=OFtDAhA.0&sid=pxCY9pYkRXFp4ULZAAAS HTTP/1.1" 200 1 "https://www.myapp-example.com/my-subfolder/home" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/106.0.0.0 Safari/537.36" "XXX,XXX,XXX,XXX"
zsbz8rwp

zsbz8rwp1#

您可以在access_log指令行之前添加此行。

set $writelog 1;
if ( $uri ~ ^/socket.io/ ) {
    set $writelog 0;
}

然后,仅在设置了$writelog时才执行日志记录,如下所示:

access_log  /var/log/nginx/access.log main if $writelog;

相关问题