要修改 CentOS 上 Nginx 的日志格式,请按照以下步骤操作:
/etc/nginx/nginx.conf。使用文本编辑器打开它,例如使用 vim:sudo vim /etc/nginx/nginx.conf
http 块中,找到 log_format 指令。如果没有找到,请在 http 块内添加一个新的 log_format 指令。例如,要创建一个名为 custom 的自定义日志格式,可以添加以下内容:http {
...
log_format custom '$remote_addr - $remote_user [$time_local] '
'"$request" $status $body_bytes_sent '
'"$http_referer" "$http_user_agent" "$http_x_forwarded_for"';
...
}
这里的 $remote_addr、$remote_user 等变量表示日志中的不同字段。你可以根据需要修改这些变量。完整的变量列表可以在 Nginx 官方文档中找到:http://nginx.org/en/docs/http/ngx_http_core_module.html#log_format
保存并关闭配置文件。
为了使更改生效,重新加载 Nginx 配置:
sudo nginx -s reload
现在,Nginx 将使用新的日志格式记录访问日志。如果你想查看新的日志格式,请检查 Nginx 的访问日志文件(通常位于 /var/log/nginx/access.log)。