http-log-var-set #

Sets a variable to a value just before nginx writes the access log line. Use it to log a value that is only known at the end of the request, optionally under a condition.

Source: upstream source

Directives

log_set #

syntax: log_set $variable value;  ·  default: -  ·  context: http, server, location, http when, server when, location when

Sets the request variable to the given value before access log writing. The value may contain variables from request or response, such as $upstream_http_*. For each variable, local definitions are evaluated first in configuration order. If no local condition matches, definitions inherited from the previous level

Example

log_format main '$remote_addr - $remote_user [$time_local] '
                    '"$request" $status $body_bytes_sent '
                    '"$http_referer" "$http_user_agent" '
                    '"$log_field1" "$log_field2"';
access_log /spool/logs/nginx-access.log;

server {
    listen 127.0.0.1:80;
    server_name localhost;

    location / {
        log_set $log_field1 $upstream_http_custom_header1;
        expr has_field2 !is_empty $upstream_http_custom_header2;
        when has_field2 {
            log_set $log_field2 $upstream_http_custom_header2;
        }
        proxy_pass http://example.upstream.com;
    }
}

↑ back to index