想要记录请求的body
等信息可以通过自定义日志格式的方式,下面例子中在http
块内定义custom_log
自定义日志格式,在server
块内的access_log指令中使用了这个自定义日志格式。此配置会让接收到的HTTP请求将会按照custom_log
格式进行记录,并将日志写到/var/log/nginx/access.log文件中。
http {
# 其他配置项...
# 定义自定义日志格式
log_format custom_log '$remote_addr - $remote_user [$time_local] '
'"$request" $status $body_bytes_sent '
'"$http_referer" "$http_user_agent" '
'$request_body';
server {
listen 80;
server_name example.com;
access_log /var/log/nginx/access.log custom_log; # 使用自定义日志格式
location / {
# 其他配置项...
}
}
}
配置中各个字段的具体含义:
$remote_addr
:客户端的IP地址。$remote_user
:客户端用户标识(如果启用了HTTP身份验证)。$time_local
:请求的本地时间。$request
:完整的HTTP请求行,包括请求方法、URI和协议版本。$status
:HTTP响应状态码。$body_bytes_sent
:发送给客户端的字节数(不包括响应头)。$http_referer
:客户端的引用页面URL。$http_user_agent
:客户端的用户代理(浏览器等)信息。$request_body
:HTTP请求正文。