跳到主內容

將 Nginx log 記錄傳到 Graylog

  如果要看每個台服務的 log 檔都要到該台設備去查看記錄,這樣子太麻煩了因為有架設 graylog 服務,可以將這些服務上的記錄都傳到這一台 log 服務器上記錄並且察看

2024.02.27 修改為 Graylog 新版本可以正常收集資料

  Nginx 的服務版本最起要在 1.11.8 以上版本才會有支援 escape=json。

  • Nginx 全域設定檔做設定 vim /etc/nginx/nginx.conf 並且填入以下的內容
log_format graylog_json escape=json '{ "nginx_timestamp": "$time_iso8601", '
        '"remote_addr": "$remote_addr", '
        '"connection": "$connection", '
        '"connection_requests": $connection_requests, '
        '"pipe": "$pipe", '
        '"body_bytes_sent": $body_bytes_sent, '
        '"request_length": $request_length, '
        '"request_time": $request_time, '
        '"response_status": $status, '
        '"request": "$request", '
        '"request_method": "$request_method", '
        '"host": "$host", '
        '"upstream_cache_status": "$upstream_cache_status", '
        '"upstream_addr": "$upstream_addr", '
        '"http_x_forwarded_for": "$http_x_forwarded_for", '
        '"http_referrer": "$http_referer", '
        '"http_user_agent": "$http_user_agent", '
        '"http_version": "$server_protocol", '
        '"remote_user": "$remote_user", '
        '"http_x_forwarded_proto": "$http_x_forwarded_proto", '
        '"upstream_response_time": "$upstream_response_time", '
        '"nginx_access": true }';

# 將 syslog:server=這裡填入 graylog ip or hostname 
# replace the hostnames with the IP or hostname of your Graylog server
access_log syslog:server=graylog.server.org:12304 graylog_json;
error_log syslog:server=graylog.server.org:12305;

  • 建立新的 .json 檔在將內容貼到您剛剛建立的記事本,在另存成 .json 檔後再匯入,System -> Content Packs

graylog.png

  • 上傳檔案,按下 Upload

graylog-1.png

選擇檔案,剛剛下載好的 .json 檔,然後按下 Upload

graylog-2.png

上傳成功會在下面看到剛剛上傳的 .json 檔,檔名是 nginx-syslog 然後按下安裝 Install

graylog-3.png

顯示幫你安裝有關於 Nginx 服務的規則,按下 Install

graylog-4.png

  • 如果您有自己設定 Inputs 的話需要再新增加設定,如果沒有正常來說會自己幫您新增 Inputs

  • 以下是您自己新增後還有需要在設定的參數

  • 新增加 static field

nginxlog-01.png

  • 在填入以下的參數,這樣子在 streams 裡面有個項目是 NGINX Access Logs 裡面可以看到目前所收集到 Nginx 資訊

nginxlog-02.png




參考相關網頁: