大家好,我是?WeiyiGeek,一名深耕安全運維開發(fā)(SecOpsDev)領(lǐng)域的技術(shù)從業(yè)者,致力于探索DevOps與安全的融合(DevSecOps),自動化運維工具開發(fā)與實踐,企業(yè)網(wǎng)絡(luò)安全防護,歡迎各位道友一起學(xué)習(xí)交流、一起進步 ,若此文對你有幫助,一定記得倒點個關(guān)注?與小紅星??,收藏學(xué)習(xí)不迷路??。
Ingress-nginx 如何針對虛擬主機域名定義多種日志格式
描述:最近采集 ingress-nginx 中間件訪問日志時,由于日志平臺需要根據(jù)不同域名自定義多種日志格式,如生產(chǎn)環(huán)境有個項目需要打開 nginx 日志的 http_x_info 頭信息記錄到日志中,但是如果把默認的日志格式改掉的話,又會影響其他項目日志的采集,所以就想到了能不能針對某個域名定義不同的日志格式。遂經(jīng)過查閱官方文檔以及Google搜索,找到了解決辦法,所以簡單記錄以備后查。
原文鏈接:?https://articles.zsxq.com/id_vjzitlh9zogb.html
k8s 的 ingress-nginx 其實就是 nginx 封裝了一層,所以 nginx 可以做的,ingress 也都可以做,nginx 我們知道可以定義不同的日志格式,然后不同的虛擬主機來引用就好了。
先查看官方文檔,發(fā)現(xiàn)了 nginx-ingress 有一個有幾個自定義日志格式相關(guān)參數(shù),如下所示:
log-format-escape-none:設(shè)置escape參數(shù)是否完全禁用變量中的字符轉(zhuǎn)義(“true”)或由日志格式escape-json(“false”)控制。
log-format-escape-json:設(shè)置escape參數(shù)是否允許JSON(“true”)或變量中的默認字符轉(zhuǎn)義(“false”)設(shè)置nginx日志格式。
log-format-upstream:設(shè)置全局缺省日志格式。(常用)
log-format-stream:設(shè)置全局缺省流日志格式。
http-snippet:在 http 塊下面增加一段原生的 Nginx 配置。(常用)
相關(guān)參數(shù)使用參考:https://kubernetes.github.io/ingress-nginx/user-guide/nginx-configuration/configmap/#log-format-upstream
默認配置使用自定義日志格式來添加有關(guān)上游、響應(yīng)時間和狀態(tài)的其他信息,其字段變量配置如下:
log_format upstreaminfo
? ??'$remote_addr - $remote_user [$time_local] "$request" '
? ??'$status $body_bytes_sent "$http_referer" "$http_user_agent" '
? ??'$request_length $request_time [$proxy_upstream_name] [$proxy_alternative_upstream_name] $upstream_addr '
? ??'$upstream_response_length $upstream_response_time $upstream_status $req_id';
日志格式化以及參數(shù)介紹參考:https://kubernetes.github.io/ingress-nginx/user-guide/nginx-configuration/log-format/
實踐目的:開啟 ingress-nginx 日志持久化存儲,配置全局缺省自定義日志格式,針對某個域名定義不同的日志格式:
實踐環(huán)境
# ingress-nginx 版本信息:
app.kubernetes.io/version: 1.12.1
helm.sh/chart: ingress-nginx-4.12.1
配置實踐
步驟 01.添加 PVC 持久化卷,這里作者使用的是NFS動態(tài)存儲卷,編輯 ingress-nginx 的 deployment,添加自定義日志格式配置
# 1.添加持久化日存儲PVC
tee ingress-nginx-pvc.yaml <<'EOF'
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
? name: ingress-nginx
? namespace: ingress-nginx
spec:
? storageClassName: nfs-dev
? volumeMode: Filesystem
? accessModes: ["ReadWriteMany"]
? resources:
? ? requests:
? ? ? storage: 5Gi
EOF
kubectl apply -f ingress-nginx-pvc.yaml
# 2.添加卷掛載相配置
$ kubectl edit deployments.apps ingress-nginx-controller -n ingress-nginx
......
? ? ? ? volumeMounts:
? ? ? ? - mountPath: /var/log/nginx/vhost/
? ? ? ? ? name: logs
? ? ? volumes:
? ? ? - name: logs
? ? ? ? persistentVolumeClaim:
? ? ? ? ? claimName: ingress-nginx
......
步驟 02.編輯 ingress-nginx 的 ConfigMap,添加自定義日志格式配置,保存后會自動更新 ingress-nginx 的配置。
$ kubectl edit cm -n ingress-nginx ingress-nginx-controller?
......
apiVersion: v1
kind: ConfigMap
data:
? max-worker-connections:?"32767"
? max-worker-open-files:?"10240"
enable-multi-accept:?"true"
enable-brotli:?"true"
? brotli-level:?"3"
? brotli-min-length:?"10240"
? brotli-types: text/html text/plain text/css text/javascript application/javascript
? ? application/x-javascript application/xml application/x-httpd-php application/x-font-ttf
? ? image/x-icon application/json application/font-woff image/svg+xml font/ttf font/opentype
enable-underscores-in-headers:?"true"
? use-forwarded-headers:?"true"
? compute-full-forwarded-for:?"true"
? forwarded-for-header: X-Forwarded-For
? real_ip_header: X-Forwarded-For
? real_ip_recursive:?"on"
? set_real_ip_from: 10.68.0.0/16
? proxy-body-size: 100M ? ?# 對應(yīng)nginx client_max_body_size
? client-body-buffer-size:?"8M"
? client-header-buffer-size:?"2M"
? proxy-connect-timeout:?"300"
? proxy-read-timeout:?"300"
? proxy-send-timeout:?"300"
? proxy-buffer-size:?" 32k"
? connection-proxy-header:?"keep-alive"
# 允許在規(guī)則中使用自定義片段
? allow-snippet-annotations:?"true"
? annotations-risk-level: Critical
# 定義轉(zhuǎn)換字符轉(zhuǎn)義為JSON格式 (關(guān)鍵配置)
log-format-escape-json:?"true"
# 定義默認的日志格式 (關(guān)鍵配置)
log-format-upstream:?'{"timestamp": "$time_iso8601","ip": "$http_x_forwarded_for","client":
? ? "$remote_addr","method": "$request_method","scheme": "$scheme","protocol": "$server_protocol","domain":
? ? "$host","uri": "$uri","args": "$args","status": $status,"size": $body_bytes_sent,"time":
? ? "$request_time","upname": "$proxy_upstream_name","upaddr":"$upstream_addr","uptime":
? ? "$upstream_response_time","referer": "$http_referer","ua": "$http_user_agent"}'
# 定義了一個新的日志格式 (關(guān)鍵配置),以及按照天數(shù)分隔日志文件
? http-snippet: |
? ? log_format upstreamjson escape=json?'{"timestamp": "$time_iso8601",'
? ? ??'"ip": "$http_x_forwarded_for",'
? ? ??'"client": "$remote_addr",'
? ? ??'"method": "$request_method",'
? ? ??'"scheme": "$scheme",'
? ? ??'"protocol": "$server_protocol",'
? ? ??'"domain": "$host",'
? ? ??'"uri": "$uri",'
? ? ??'"args": "$http_x_info",'
? ? ??'"status": $status,'
? ? ??'"size": $body_bytes_sent,'
? ? ??'"time": "$request_time",'
? ? ??'"upname": "$proxy_upstream_name",'
? ? ??'"upaddr": "$upstream_addr",'
? ? ??'"uptime": "$upstream_response_time",'
? ? ??'"referer": "$http_referer",'
? ? ??'"ua": "$http_user_agent"'
? ? ??'}';
? ? map?$time_iso8601$logdate?{
? ? ??'~^(?<ymd>d{4}-d{2}-d{2})'$ymd;
? ? ? default?'default';
? ? }
步驟 03.然后,編輯 ingress 規(guī)則,添加自定義配置片段,保存后會自動更新 ingress-nginx 的配置。
$ kubectl edit ingress -n app dev?
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
? annotations:
? ? ingressclass.kubernetes.io/is-default-class:?"true"
? ? nginx.ingress.kubernetes.io/client-body-buffer-size: 50m
? ? nginx.ingress.kubernetes.io/configuration-snippet: |
? ? ? access_log /var/log/nginx/vhost/dev-${logdate}.log?upstreamjson; ?# 關(guān)鍵配置
步驟 04.訪問的創(chuàng)建的持久化pvc卷中存儲的日志文件,如下所示:
??cd?/storage/dev/pvc/ingress-nginx-ingress-nginx-pvc-bc108248-ee08-4a29-9560-15885230020b/
? ls
dev-2025-06-10.log ?dev-2025-06-11.log
weiyigeek.top-使用 configuration-snippet 注解圖
至此,為使用 helm 部署的 ingress-nginx 添加了持久化存儲卷以及自定義日志格式,針對某個域名定義不同的日志格式的實踐就完成了。若要在生產(chǎn)環(huán)境中使用則建議通本地存儲卷,或者 syslog 直接上傳到日志平臺,亦或者使用 Sidecar 容器收集日志。
Ingress-Nginx 中其它日志相關(guān)參數(shù)補充介紹:
disable-access-log:禁用整個Ingress控制器的訪問日志。默認值:false
access-log-path:設(shè)置訪問日志的路徑。默認值:/var/log/nginx/access.log
access-log-params:設(shè)置訪問日志參數(shù),在優(yōu)化日志存儲常常使用。例如 buffer=16k, gzip, flush=1m
??# 實際上上述兩條指令,等用于 Nginx 中的 access_log path [format [buffer=size] [gzip[=level]] [flush=time] [if=condition]];
??# 此外,還可以使用內(nèi)置變量來選擇當(dāng)前請求是否寫入到日志文件中。
??# 例如:如果condition求值為“0”或空字符串,則不會記錄請求。在下面的示例中,響應(yīng)代碼為2xx和3xx的請求將不會被記錄:
? map?$status?$loggable?{
? ? ~^[23] ?0;
? ? default 1;
? }
? access_log /path/to/access.log combined?if=$loggable;
參考來源:
- https://kubernetes.github.io/ingress-nginx/user-guide/nginx-configuration/configmap/https://nginx.org/en/docs/http/ngx_http_log_module.html#access_log
加入:作者【全棧工程師修煉指南】知識星球
獲?。鹤髡吖ぷ鲗W(xué)習(xí)全棧筆記
作者整理了10年的工作學(xué)習(xí)筆記(涉及網(wǎng)絡(luò)、安全、運維、開發(fā)),需要學(xué)習(xí)實踐筆記的看友,可添加作者微信或者回復(fù)【工作學(xué)習(xí)實踐筆記】,當(dāng)前價格¥199,除了獲得從業(yè)筆記的同時還可進行問題答疑以及每月遠程技術(shù)支持,希望大家多多支持,收獲定大于付出!