大家好,我是 WeiyiGeek,一名深耕安全运维开发(SecOpsDev)领域的技术从业者,致力于探索DevOps与安全的融合(DevSecOps),自动化运维工具开发与实践,企业网络安全防护,欢迎各位道友一起学习交流、一起进步 ,若此文对你有帮助,一定记得倒点个关注⭐与小红星❤️,收藏学习不迷路 。
Ingress-nginx 如何针对虚拟主机域名定义多种日志格式
描述:最近采集 ingress-nginx 中间件访问日志时,由于日志平台需要根据不同域名自定义多种日志格式,如生产环境有个项目需要打开 nginx 日志的 http_x_info 头信息记录到日志中,但是如果把默认的日志格式改掉的话,又会影响其他项目日志的采集,所以就想到了能不能针对某个域名定义不同的日志格式。遂经过查阅官方文档以及Google搜索,找到了解决办法,所以简单记录以备后查。
原文链接: https://articles.zsxq.com/id_vjzitlh9zogb.html
k8s 的 ingress-nginx 其实就是 nginx 封装了一层,所以 nginx 可以做的,ingress 也都可以做,nginx 我们知道可以定义不同的日志格式,然后不同的虚拟主机来引用就好了。
先查看官方文档,发现了 nginx-ingress 有一个有几个自定义日志格式相关参数,如下所示:
log-format-escape-none:设置escape参数是否完全禁用变量中的字符转义(“true”)或由日志格式escape-json(“false”)控制。
log-format-escape-json:设置escape参数是否允许JSON(“true”)或变量中的默认字符转义(“false”)设置nginx日志格式。
log-format-upstream:设置全局缺省日志格式。(常用)
log-format-stream:设置全局缺省流日志格式。
http-snippet:在 http 块下面增加一段原生的 Nginx 配置。(常用)
相关参数使用参考:https://kubernetes.github.io/ingress-nginx/user-guide/nginx-configuration/configmap/#log-format-upstream
默认配置使用自定义日志格式来添加有关上游、响应时间和状态的其他信息,其字段变量配置如下:
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';
日志格式化以及参数介绍参考:https://kubernetes.github.io/ingress-nginx/user-guide/nginx-configuration/log-format/
实践目的:开启 ingress-nginx 日志持久化存储,配置全局缺省自定义日志格式,针对某个域名定义不同的日志格式:
实践环境
# ingress-nginx 版本信息:
app.kubernetes.io/version: 1.12.1
helm.sh/chart: ingress-nginx-4.12.1
配置实践
步骤 01.添加 PVC 持久化卷,这里作者使用的是NFS动态存储卷,编辑 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 # 对应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"
# 允许在规则中使用自定义片段
allow-snippet-annotations: "true"
annotations-risk-level: Critical
# 定义转换字符转义为JSON格式 (关键配置)
log-format-escape-json: "true"
# 定义默认的日志格式 (关键配置)
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"}'
# 定义了一个新的日志格式 (关键配置),以及按照天数分隔日志文件
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 规则,添加自定义配置片段,保存后会自动更新 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; # 关键配置
步骤 04.访问的创建的持久化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 添加了持久化存储卷以及自定义日志格式,针对某个域名定义不同的日志格式的实践就完成了。若要在生产环境中使用则建议通本地存储卷,或者 syslog 直接上传到日志平台,亦或者使用 Sidecar 容器收集日志。
Ingress-Nginx 中其它日志相关参数补充介绍:
disable-access-log:禁用整个Ingress控制器的访问日志。默认值:false
access-log-path:设置访问日志的路径。默认值:/var/log/nginx/access.log
access-log-params:设置访问日志参数,在优化日志存储常常使用。例如 buffer=16k, gzip, flush=1m
# 实际上上述两条指令,等用于 Nginx 中的 access_log path [format [buffer=size] [gzip[=level]] [flush=time] [if=condition]];
# 此外,还可以使用内置变量来选择当前请求是否写入到日志文件中。
# 例如:如果condition求值为“0”或空字符串,则不会记录请求。在下面的示例中,响应代码为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
加入:作者【全栈工程师修炼指南】知识星球
获取:作者工作学习全栈笔记
作者整理了10年的工作学习笔记(涉及网络、安全、运维、开发),需要学习实践笔记的看友,可添加作者微信或者回复【工作学习实践笔记】,当前价格¥199,除了获得从业笔记的同时还可进行问题答疑以及每月远程技术支持,希望大家多多支持,收获定大于付出!
1244