將 Nginx log 記錄傳到 Graylog
如果要查看每一台服務的 log 檔,都得登入該台設備才看得到,這樣太麻煩了。既然已經架設了 Graylog 服務,就可以把各台服務的記錄都傳到這一台 log 伺服器上收集與查看。這個做法由 Nginx 直接用 syslog 傳送,不需要另外安裝收集程式。
2026.11.29 新增自建 Pipeline 解析做法與網址參數遮罩設定,並補充 Input 設定方式與疑難排解
2024.02.27 修改為 Graylog 新版本可以正常收集資料
前置需求
Nginx版本最低需要 1.11.8,才支援escape=json(用nginx -v查看版本)。Nginx主機可以用 UDP 連到 Graylog,防火牆要放行兩個埠:存取日誌12304/udp(直接用內容包建立的 Input 時是12401/udp)、錯誤日誌12305/udp。- 修改前先備份設定檔:
cp -a /etc/nginx/nginx.conf /etc/nginx/nginx.conf.bak
syslog 走 UDP,不會確認對方有沒有收到。Graylog 停機或網路掉包時,那段時間的記錄不會補送,所以本機的 log 檔請保留。
UDP syslog 沒有身分驗證。Graylog 的 Input 綁定 0.0.0.0 時,任何連得到的主機都能送資料進來,請用防火牆只放行 Nginx 主機,用不到的 Input 請停掉。
安裝或測試過程
設定 Nginx
- 編輯
vim /etc/nginx/nginx.conf,把以下內容填在http { }區塊裡面(放在區塊外面會設定失敗),而且要放在include /etc/nginx/conf.d/*.conf;與include /etc/nginx/sites-enabled/*;這類行的前面 - 範例中的
graylog.example.com請改成你的 Graylog 位址,建議直接填 IP(主機名解析不到時,nginx -t會失敗、服務也起不來)
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 }';
# graylog.example.com 改成你的 Graylog IP 或主機名
access_log syslog:server=graylog.example.com:12304 graylog_json;
- 原本的
access_log /var/log/nginx/access.log;請保留,並且和 syslog 那一行寫在同一層,本機才會繼續有記錄。 - 錯誤日誌另外處理:先用
grep -rn error_log /etc/nginx/找到原本那一行,在它的下一行(同一層,原本寫在http { }外面就寫在外面)加上 syslog:
error_log /var/log/nginx/error.log; # 原本那一行,照舊保留
error_log syslog:server=graylog.example.com:12305; # 新增在同一層
下層(http { }、server { })的 error_log 會蓋掉上層的設定。只在下層寫 syslog 那一行,本機的 error.log 可能就不再記錄。找不到原本那一行時,上面兩行一起寫在 http { } 外面。
- 站台設定檔的
server { }如果自己寫了access_log或error_log,就不會沿用上層的設定,該站台要另外加上對應的 syslog 那一行。可以用grep -rnE 'access_log|error_log' /etc/nginx/找出來。 - 如果採用做法二(內容包),不要在 syslog 那一行另外指定
tag=,內容包是用預設的nginx標籤辨識訊息。 - 檢查設定沒有錯誤後重新載入
nginx -t && nginx -s reload
上面的格式會把完整網址(含參數)與 referer 送到 Graylog。如果網址裡可能帶有 token、密碼或 Email,請改用後面「補充說明」的遮罩版。
Graylog 端:兩種做法擇一
為什麼 Graylog 端需要解析:用 syslog 傳送時,整段 JSON 只是訊息裡的一串文字(前面還帶有主機名與標籤),Graylog 的 Syslog Input 不會自動拆成欄位。要靠解析規則取出 JSON,之後才能用 response_status、remote_addr 這些欄位搜尋與統計。
**做法一(建議)**自己建立解析規則,不依賴外部內容包。做法二是匯入社群內容包的原做法,該內容包在 GitHub 上最後一次更新是 2022 年 1 月。兩種做法不要同時使用,否則同一筆記錄會被解析兩次。
做法一:自己建立 Input、Stream 與 Pipeline(建議)
以下畫面是 Graylog 7.2。
System -> Inputs選Syslog UDP後按Launch new input,Title自訂,Port填12304,再按Launch Input
- 接著會跳出
Input Setup Wizard。Stream 後面自己建立,這裡不用選,直接按Next,再按Start Input
沒有按到 Start Input(例如直接關掉精靈)時,Input 會停在 Setup 狀態,收不到資料。按該列的 Set-up Input 可以繼續完成,狀態要變成 Running。
- 在這個 Input 的
More -> Add static field新增靜態欄位:Field name填from_nginx、Field value填true
Streams按Create stream,Title填NGINX Access Logs。Remove matches from 'Default Stream'維持勾選時,這些訊息就不會再出現在Default Stream
- 在這個 Stream 按
Create Rule:Field填from_nginx、Type選match exactly、Value填true。建立後如果詢問Start stream?,按Start Stream
System -> Pipelines -> Manage rules新增以下規則
rule "nginx 存取日誌解析"
when
has_field("message") && contains(to_string($message.message), "\"nginx_access\"")
then
// syslog 訊息前面帶有主機名與標籤,取出第一個 { 之後的 JSON
let body = regex_replace("^[^{]*", to_string($message.message), "");
set_fields(to_map(parse_json(body)));
// 以 Nginx 記錄的時間(含時區)作為訊息時間
set_field("timestamp", parse_date(value: to_string($message.nginx_timestamp), pattern: "yyyy-MM-dd'T'HH:mm:ssZZ"));
// 訊息列表只顯示請求列
set_field("message", to_string($message.request));
remove_single_field("nginx_timestamp");
end
- 儲存前可以在編輯器下方的
Rule Simulation選Simple Message,貼一則測試訊息再按Run rule simulation(只做模擬,不會寫入資料)
web01 nginx: { "nginx_timestamp": "2026-11-29T10:15:30+08:00", "remote_addr": "192.0.2.10", "response_status": 200, "request": "GET /index.html HTTP/1.1", "nginx_access": true }
- 下面出現拆開的欄位就代表規則正常
-
remove_single_field是較新版本的函式,規則編輯器顯示找不到時,改用remove_field -
System -> Pipelines -> Manage pipelines建立 Pipeline,編輯Stage 0,在Stage rules加入這條規則
- 再按
Edit connections連到NGINX Access Logs(沒有連接就不會執行)
System -> Configurations -> Message Processors的順序要是Message Filter Chain→Stream Rule Processor→Pipeline Processor(舊版沒有Stream Rule Processor,Message Filter Chain排在Pipeline Processor前面即可)。順序不對時按Edit configuration調整
做法二:匯入 Graylog 內容包
這個內容包的 Pipeline NGINX JSON Processing 接在 Default Stream,不是接在 NGINX Access Logs,所以 Default Stream 裡其他來源的訊息也會經過比對。內容含 nginx: { 的訊息不論來源都會被拿去解析,message、timestamp 與同名欄位可能被改寫。Graylog 上已經有其他資料的話,安裝前先確認沒有其他來源符合這個格式。
- 到 Graylog 套件庫開啟 NGINX Syslog for Graylog,點選 View on Github
- 下載其中的
content_pack.json(或把內容貼到記事本另存成.json檔)後匯入,位置在System -> Content Packs
- 按右上角的
Upload
- 選擇剛剛下載好的
.json檔,然後按下Upload
- 上傳成功後,清單裡會出現
nginx-syslog(內容包多的話,用上方的Filter找),按下該列的Install
- 確認視窗下方的
Entity list會列出即將建立的 7 個項目:1 個 Input、1 個 Pipeline、4 條 pipeline rule、1 個 Stream(清單可以往下捲)。Install Comment與Add Collaborator可以留空,按右下角的Install
- 安裝後到
System -> Inputs確認nginx-syslog的狀態是Running;停在Setup的話按Set-up Input完成
做法二的 Inputs 設定(兩種方式擇一)
內容包安裝後會自動建立一個名稱為 nginx-syslog 的 Syslog UDP Input,埠號是 12401,並且已經帶好靜態欄位 from_nginx。
方式一:直接用內容包建立的 Input
- 把
nginx.conf的 syslogaccess_log埠號改成12401,後面遮罩版、健康檢查與tcpdump的12304也一樣要改成12401
access_log syslog:server=graylog.example.com:12401 graylog_json;
方式二:自己建立 Input(沿用上面的 12304)
- 自己建立一個 Syslog UDP Input(7.2 的設定精靈處理方式同做法一),埠號
12304(內容包建立的12401Input 用不到就停掉) - 到該 Input 的
More(舊版為More actions)新增static field
- 填入以下參數:
Field name填from_nginx、Field value填true。這樣在Streams裡的NGINX Access Logs就能看到收集到的Nginx資訊
錯誤日誌的 Input(兩種做法都要)
error_log送到12305,要在System -> Inputs另外建立一個Syslog UDPInput 才收得到(一樣要在設定精靈按到Start Input)。錯誤日誌是一般文字,不會像存取日誌那樣拆成欄位。
確認有收到資料
- 開一次網站,在
Streams的NGINX Access Logs應該看得到新訊息 - 展開訊息,應該有
response_status、remote_addr、request_time等欄位 - 如果沒有收到,先在 Graylog 主機確認封包有沒有進來
sudo tcpdump -ni any udp port 12304 -A
12304請換成你的 Input 埠號
補充說明
遮罩版:網址參數不送到 Graylog
用下面這一段取代前面的 log_format 與 syslog access_log(兩段的 log_format 名稱相同,不要兩段都貼);error_log 與本機的 access_log 不在這一段,維持原狀。欄位名稱完全相同,內容包不用修改;差別是 request 與 http_referrer 只保留問號前面的部分。
map $request_uri $gl_path {
"~^(?<gl_p>[^?]*)" $gl_p;
default $request_uri;
}
map $http_referer $gl_referer {
"~^(?<gl_r>[^?#]*)" $gl_r;
default $http_referer;
}
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_method $gl_path $server_protocol", '
'"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": "$gl_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 }';
# 位址與埠號改成你的(用內容包的 Input 時埠號是 12401)
access_log syslog:server=graylog.example.com:12304 graylog_json;
錯誤日誌(error_log)的內容無法用這個方式遮罩,裡面可能帶有完整網址。如果不能接受,請不要把 error_log 送到 Graylog。
- 寫在網址路徑裡的資料(例如
/user/[email protected])不會被遮掉。 - 遮罩版只處理網址參數;來源 IP、
X-Forwarded-For、remote_user、User-Agent 以及路徑裡的 token 仍然會送出。 - 本機的
access.log仍然是完整網址,請控管讀取權限與保存天數。
選用:健康檢查不送到 Graylog
監控系統頻繁打健康檢查網址時,可以把它排除,減少儲存量。路徑請改成自己網站實際使用的。
map $request_uri $gl_loggable {
"~^/(health|healthz|ping|favicon\.ico|robots\.txt)$" 0;
"~^/(health|healthz|ping|favicon\.ico|robots\.txt)[?]" 0;
default 1;
}
# 位址與埠號改成你的(用內容包的 Input 時埠號是 12401)
access_log syslog:server=graylog.example.com:12304 graylog_json if=$gl_loggable;
- 這一行
access_log是用來取代原本 syslog 的那一行,不要兩行並存,否則記錄會送兩次。 - 站台自己寫了 syslog
access_log的,那一行也要加上if=$gl_loggable。
疑難排解
- 錯誤訊息:
"map" directive is not allowed here或"log_format" directive is not allowed here解決方式: 內容放到http { }區塊外面了,移到區塊裡面。 - 錯誤訊息:
unknown log format "graylog_json"解決方式:access_log寫在log_format前面,或寫在比定義更早載入的位置,調整順序。 - 狀況: Graylog 完全沒收到
解決方式: 確認 Input 是
Running(停在Setup時按Set-up Input完成設定);用tcpdump確認封包有沒有到;檢查防火牆、埠號,以及站台是否自己寫了access_log。 - 狀況: 收到訊息,但沒有進
NGINX Access Logs解決方式: 確認 Stream 已啟動;自己建立的 Input 要有靜態欄位from_nginx=true。 - 狀況: 有進 Stream,但欄位沒有拆開
解決方式: 做法一確認 Pipeline 已連到
NGINX Access Logs;做法二確認內容包的 Pipeline 連到 Default Stream。另外System -> Configurations -> Message Processors的順序要是Message Filter Chain→Stream Rule Processor→Pipeline Processor(舊版沒有中間那一項)。 - 狀況: 大部分訊息正常,少數幾筆欄位沒有拆開
解決方式: 檢查該筆的
response_status是不是000或009。前面有 0 的數字不是合法的 JSON,整筆會解析失敗。
版本紀錄
- 2024-02-27:修改為 Graylog 新版本可以正常收集資料
- 2026-11-29:新增自建 Pipeline 做法、補充 Input 兩種設定方式、遮罩版設定、健康檢查排除與疑難排解,並加上 Graylog 7.2 操作畫面



















