如何使用 nginx/uwsgi 关闭 json 请求的访问日志记录

How to turn off access logging for json requests, using nginx/uwsgi

我有一个 nginx.conf 文件

location / {
     # see http://uwsgi-docs.readthedocs.org/en/latest/Nginx.html
     uwsgi_pass uwsgi_devcluster;
     include uwsgi_params;
}

并且我想确保 json 对这个 uwsgi 服务器的请求没有被记录下来,因为加载是每分钟进行的。所以我尝试了

location / {
     location ~ \.json$ {
        access_log off;
     }
     # see http://uwsgi-docs.readthedocs.org/en/latest/Nginx.html
     uwsgi_pass uwsgi_devcluster;
     include uwsgi_params;
}

但这会导致这些页面出现 404 错误。我该如何设置?

location 块不是那样工作的。有关详细信息,请参阅 this document

您可以定义两个位置块,都使用必要的 uwsgi_pass 指令。最简单的解决方案是:

location / {
    uwsgi_pass uwsgi_devcluster;
    include uwsgi_params;
}
location ~ \.json$ {
    access_log off;
    uwsgi_pass uwsgi_devcluster;
    include uwsgi_params;
}

或者,您可以使用 map 指令设置一个变量来设置 access_log 指令的 if=condition。有关详细信息,请参阅 this document