Tuesday, September 30, 2014

ubuntu - Convert apache config to nginx equivalent for logs




I have this custom log in apache



 SetEnvIFNoCase User-agent "ELB-HealthChecker/2.0" skiplog
LogFormat "%t \"%r\" %>s %O | client:%a | Local:%A | Host:%v | %H | %m | %P(pid) | TimeTaken:%T | %q |DataReceived:%I-Sent:%O-Total:%S | %l | %u | \"%{Referer}i\" \"%{User-Agent}i\"" mylog
LogFormat "%a %l %u %t \"%r\" %>s %O \"%{Referer}i\" \"%{User-Agent}i\"" combined_mylog
CustomLog /var/log/apache2/access.log mylog env=!skiplog
CustomLog /var/log/apache2/access_org.log combined_mylog env=!skiplog



Basically i want to skip the log if user agent is "ELB-HealthChecker/2.0"



Now i have shifted to nginx and i am not sure how to use it in there


Answer



The nginx access_log directive accepts a parameter if which causes it to log only if the given condition is not false or an empty string.



So, you can set a variable and in the case that that user agent appears set the variable to 0. You can do this most easily in a map:



map $http_user_agent $loggable {
default 1;

ELB-HealthChecker/2.0 0;
}


Then you can modify access_log to check the variable:



    access_log /var/log/nginx/whatever-access.log log_format_name if=$loggable;

No comments:

Post a Comment

linux - How to SSH to ec2 instance in VPC private subnet via NAT server

I have created a VPC in aws with a public subnet and a private subnet. The private subnet does not have direct access to external network. S...