是否可以在 nginx 日志中记录自定义标头的默认值

way*_*lfe 5 nginx

我正在提取自定义标头值并使用自定义日志格式将它们记录在访问日志文件中:

log_format custom  '$remote_addr - $remote_user [$time_local] "$request" '
    '$status $body_bytes_sent "$http_referer" "$http_user_agent" '
    '$http_my_custom_header'
Run Code Online (Sandbox Code Playgroud)

当记录不包含标头的请求时,ngx_http_log_module将 a 插入-到日志中。是否可以为缺失的标头定义默认值?我需要标头始终具有一个数值,以便以后在 elasticsearch 中建立索引。

我有一个使用地图的解决方法,它将标头值设置为0不存在时:

map $http_my_custom_header $custom_header {
    default $http_my_custom_header;
    '' 0;
}
log_format custom  '$remote_addr - $remote_user [$time_local] "$request" '
    '$status $body_bytes_sent "$http_referer" "$http_user_agent" '
    '$custom_header'
Run Code Online (Sandbox Code Playgroud)

我希望有一个更优雅的解决方案,因为我将来会添加更多标头。