在 Nginx 的 access_log 配置项中,可以通过使用变量和自定义日志格式来添加额外的日志信息。下面是一个示例:
http {
log_format mylog '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$my_custom_info"';
server {
...
access_log /var/log/nginx/access.log mylog;
location / {
...
# 在此处设置自定义的日志信息
if ($http_cookie ~* "session=([^;]+)(?:;|$)") {
set $my_custom_info "SessionID:$1";
}
...
}
}
}
在上述示例中,我们定义了名为 mylog 的自定义日志格式,其中包含 $my_custom_info 变量。然后,在对应的 location 块中,根据需求设置 $my_custom_info 变量的值。
这样配置后,在 Nginx 的访问日志中就会包含 $my_custom_info 所代表的信息。你可以根据实际需要进行相应的调整和扩展。
内容由零声教学AI助手提供,问题来源于学员提问




