要修改 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
)。