I have simple problem - I am using PHP7 and Nginx and I want to decide on PHP side if current request should be logged in Nginx's access log. Is it possible or is it too late and log line has been already written before PHP starts processing request ?
Thanks for any help.
It's not too late. You can use the if= condition on the access_log directive to control which responses are logged.
All you need to decide is how to convey the loggable status from PHP back to Nginx. For example, you could use a custom response header (e.g. X-Log).
As a proof of concept, the following seems to work:
PHP script that will not be logged:
<?php
header('X-Log: No');
phpinfo();
Nginx configuration fragment:
map $sent_http_x_log $loggable {
default 1;
No 0;
}
access_log ... if=$loggable;
See this document for details.