跳转至

全局配置

Nginx 全局配置

官方文档:https://kubernetes.github.io/ingress-nginx/user-guide/nginx-configuration/configmap/

参考文档:https://rocdu.gitbook.io/ingress-nginx-docs-cn/docs/user-guide/nginx-configuration/configmap

控制器启动参数中通过标志 --configmap 指定了一个全局的 ConfigMap 对象,比如这里用于全局配置的 ConfigMap 名为 ingress-nginx-controller

containers:
  - args:
    - /nginx-ingress-controller
    - --configmap=$(POD_NAMESPACE)/ingress-nginx-controller
    ......

常用配置

日志格式

官方默认配置介绍文档:https://kubernetes.github.io/ingress-nginx/user-guide/nginx-configuration/log-format/

参考文档:https://rocdu.gitbook.io/ingress-nginx-docs-cn/docs/user-guide/nginx-configuration/log-format

默认配置使用自定义日志记录格式来添加有关上游、响应时间和状态的附加信息:

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';

json 输出配置示例 1:

data:
  log-format-upstream: '{"nginx_timestamp":"$time_iso8601","clientip":"$remote_addr","nginx_host":"$server_addr","host":"$http_host","request":"$request","url":"$request_uri","upstreamhost":"$upstream_addr","status":"$status","body_bytes_sent":"$body_bytes_sent","request_time":"$request_time","upstream_response_time":"$upstream_response_time","xff":"$http_x_forwarded_for","referer":"$http_referer","http_user_agent":"$http_user_agent","request_length":"$request_length","request_method":"$request_method"}'

json 输出示例 1:

{
  "nginx_timestamp": "2022-10-26T07:15:42+00:00",
  "clientip": "127.0.0.1",
  "nginx_host": "127.0.0.1",
  "host": "blog.liaosirui.com:10443",
  "request": "GET /_assets/js/prism/prism-json.min.js HTTP/2.0",
  "url": "/_assets/js/prism/prism-json.min.js",
  "upstreamhost": "10.4.250.114:3000",
  "status": "200",
  "body_bytes_sent": "403",
  "request_time": "0.002",
  "upstream_response_time": "0.001",
  "xff": "-",
  "referer": "https://blog.liaosirui.com:10443/e/zh/kubernetes/%E7%BD%91%E7%BB%9C/ingress-nginx/%E5%85%A8%E5%B1%80%E9%85%8D%E7%BD%AE",
  "http_user_agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/106.0.0.0 Safari/537.36",
  "request_length": "42",
  "request_method": "GET"
}

json 输出配置示例 2:

data:
  log-format-upstream: '{"time": "$time_iso8601", "remote_addr": "$proxy_protocol_addr", "x_forwarded_for": "$proxy_add_x_forwarded_for", "request_id": "$req_id", "remote_user": "$remote_user", "bytes_sent": $bytes_sent, "request_time": $request_time, "status": $status, "vhost": "$host", "request_proto": "$server_protocol", "path": "$uri", "request_query": "$args", "request_length": $request_length, "duration": $request_time,"method": "$request_method", "http_referrer": "$http_referer", "http_user_agent": "$http_user_agent" }'

json 输出示例 2:

{
  "time": "2022-10-26T07:18:03+00:00",
  "remote_addr": "10.244.244.186",
  "x_forwarded_for": "127.0.0.1",
  "request_id": "d49c13eb2823b0e8988d771818f2875c",
  "remote_user": "-",
  "bytes_sent": 357,
  "request_time": 0.002,
  "status": 200,
  "vhost": "blog.liaosirui.com",
  "request_proto": "HTTP/2.0",
  "path": "/graphql",
  "request_query": "-",
  "request_length": 278,
  "duration": 0.002,
  "method": "POST",
  "http_referrer": "https://blog.liaosirui.com:10443/e/zh/kubernetes/%E7%BD%91%E7%BB%9C/ingress-nginx/%E5%85%A8%E5%B1%80%E9%85%8D%E7%BD%AE",
  "http_user_agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/106.0.0.0 Safari/537.36"
}