kee
kee

Reputation: 11629

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

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.

Upvotes: 3

Views: 4292

Answers (1)

Aleksey Deryagin
Aleksey Deryagin

Reputation: 2675

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

Upvotes: 3

Related Questions