Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to customize nginx log: adding a custom field which will be populated by web server endpoints

Tags:

logging

nginx

I am a newbie to nginx. I am using nginx as a load-balancer in front of some web servers. I want to add a custom field in the nginx log and the value of the field will be populated by web server handler (endpoint) but I have no idea how to implement this. Any pointer or brief explanation would be great to have.

like image 745
kee Avatar asked Dec 07 '25 10:12

kee


1 Answers

For work with endpoints (e.g. fastcgi backends) you need ngx_http_upstream_module, which have embedded variable $upstream_addr, put it in the log configuration, something like this:

log_format cache '$remote_addr - $remote_user [$time_local] "$host" "$request" $status $body_bytes_sent "$http_referer" "$http_user_agent" "$http_x_forwarded_for" '
    '"$upstream_cache_status" "$upstream_addr" "$upstream_response_time" "$request_time"';

Then use this log with access_log command when you need it:

server {
...
    access_log /var/log/nginx/access.log cache;
...
}

List of embedded variables in ngx_http_upstream_module

like image 60
Aleksey Deryagin Avatar answered Dec 10 '25 03:12

Aleksey Deryagin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!