跳到内容
SCRM DOC
创建于2024-05-19更新于 2025-03-22

Nginx Ingress 配置映射 ConfigMap

通过 Helm 安装 Nginx Ingress 的默认关联配置映射实例名称为 nginx-ingress-controller,用户可以通过修改资源对象 Deployment/DaemonSet 实例 nginx-ingress-controller 中的参数 --configmap 自定义关联配置映射实例的名称。

Nginx Ingress 控制器约定 Nginx Ingress 配置映射实例中的键值只能是字符串,即便是数字或布尔值时也要以字符串的形式书写,比如 "true"、"false"、"100","[]string" 或 "[]int" 的 Slice 类型则表示内部数据是以 "," 分隔的字符串。根据配置涉及的功能可以有如下分类。

1、Nginx 原生配置指令

用以提供向 Nginx 配置中添加 Nginx 原生配置指令,功能说明如下表所示。

名称类型默认值功能描述
main-snippetstring""在 main 指令域添加 Nginx 配置指令
http-snippetstring""在 http 指令域添加 Nginx 配置指令
server-snippetstring""在 server 指令域添加 Nginx 配置指令
location-snippetstring""在 location 指令域添加 Nginx 配置指令

配置样例如下:

echo '
apiVersion: v1
kind: ConfigMap
data:
    http-snippet: |
        ancient_browser "UCWEB";
        ancient_browser_value oldweb;
        server {
            listen 8080;
            if ($ancient_browser) {
                rewrite ^ /${ancient_browser}.html; # 重定向到oldweb.html
            }
        }
metadata:
    name: nginx-ingress-controller
    namespace: nginx-ingress
' | kubectl create -f -

2、通用配置

提供 Nginx 核心配置相关配置指令的配置,功能说明如下表所示。

名称类型默认值Nginx 指令功能描述
worker-processesstringautoworker_processes参见《Nginx进程配置指令》一节
worker-cpu-affinitystring""worker_cpu_affinity参见《Nginx进程配置指令》一节
worker-shutdown-timeoutstring10sworker_shutdown_timeout参见《Nginx进程配置指令》一节
max-worker-connectionsstring--worker_connections参见《Nginx进程配置指令》一节
max-worker-open-filesstring--worker_rlimit_nofile参见《Nginx进程配置指令》一节
enable-multi-acceptbooltruemulti_accept参见《Nginx进程配置指令》一节
keep-aliveint75keepalive_timeout--
keep-alive-requestsint100keepalive_requests--
variables-hash-bucket-sizeint128variables_hash_bucket_size--
variables-hash-max-sizeint2048variables_hash_max_size--
server-name-hash-max-sizeint1024server_names_hash_max_size--
server-name-hash-bucket-sizeintCPU 缓存行的大小server_names_hash_bucket_size--
map-hash-bucket-sizeint64map_hash_bucket_size--
bind-address[]string""listen设置虚拟主机绑定的 IP 地址
reuse-portbooltruelisten设置监听端口启用 reuseport 参数,由 Linux 内核以套接字分片方式实现进程调度
disable-ipv6boolfalselisten--
disable-ipv6-dnsboolfalseresolver设置是否关闭域名解析的 IPv6 地址查找
enable-underscores-in-headersboolfalseunderscores_in_headers参见《Nginx处理HTTP请求》一节
ignore-invalid-headersbooltrueignore_invalid_headers参见《Nginx处理HTTP请求》一节
client-header-buffer-sizestring1kclient_header_buffer_size参见《Nginx处理HTTP请求》一节
client-header-timeoutint60client_header_timeout参见《Nginx处理HTTP请求》一节
client-body-buffer-sizestring8kclient_body_buffer_size参见《Nginx处理HTTP请求》一节
client-body-timeoutint69client_body_timeout参见《Nginx处理HTTP请求》一节
large-client-header-buffersstring4 8klarge_client_header_buffers参见《Nginx处理HTTP请求》一节
http-redirect-codeint308return设置 URL 跳转时的响应码,可选项为 301,302,307 和 308
use-geoipbooltrue--启用 geoip 功能
use-geoip2boolfalse--启用 geoip2 功能,由第三方模块实现
nginx-status-ipv4-whitelist[]string127.0.0.1--设置允许访问路径 /nginx_status 的 IPv4 地址
nginx-status-ipv6-whitelist[]string::1--设置允许访问路径 /nginx_status 的 IPv6 地址
server-tokensbooltrueserver_tokens参见《Nginx处理HTTP请求》一节
lua-shared-dictsstring""--设置 Lua 共享内存字典,Oper_Resty 扩展指令

配置样例如下:

cat>test.yaml<<EOF
apiVersion: v1
kind: ConfigMap
data:
    keep-alive:   "60"
    disable-ipv6: "true"
metadata:
    name: nginx-ingress-controller
    namespace: nginx-ingress
EOF

kubectl create -f test.yaml

3、响应数据配置

提供响应信息头修改及响应数据压缩相关功能的配置,功能说明如下表所示。

名称类型默认值Nginx 指令功能描述
add-headersstring""add_header--
use-gzipbooltruegzip启用 gzip
gzip-levelint5gzip_comp_level参见《Nginx开启gzip》一节
gzip-typesstringapplication/atom+xml application/javascript application/x-javascript application/json application/rss+xml application/vnd.ms-fontobject application/x-font-ttf application/x-web-app-manifest+json application/xhtml+xml application/xml font/opentype image/svg+xml image/x-icon text/css text/java-script text/plain text/x-componentgzip_types参见《Nginx开启gzip》一节
enable-brotliboolfalse--设置是否加载 brotli 模块
brotli-levelint4--设置 brotli 的压缩级别
brotli-typesstringapplication/xml+rss application/atom+xml application/javascript application/x-javascript application/json applica-tion/rss+xml application/vnd.ms-fontobject application/x-font-ttf application/x-web-app-manifest+json application/xhtml+xml application/xml font/opentype image/svg+xml image/x-icon text/css text/javascript text/plain text/x-com-ponent--设置 brotli 的压缩类型

4、访问控制

提供限制连接数、访问速度、访问连接及防火墙的配置,功能说明如下表所示。

名称类型默认值Nginx 指令功能描述
limit-conn-zone-variablestring$binary_remote_addrlimit_conn_zone参见《Nginx并发连接数限制模块》一节
limit-conn-status-codeint503limit_conn_status参见《Nginx并发连接数限制模块》一节
limit-rateint0limit_rate--
limit-rate-afterint0limit_rate_after--
limit-req-status-codeint503limit_req_status参见《Nginx请求频率限制模块》一节
whitelist-source-range[]string []string{}--allow设置允许访问的源 IP 地址
block-cidrs[]string""deny禁止设置 IP 地址的访问
block-user-agents[]string""map禁止设置信息头字段 User-Agent 匹配值的访问
block-referers[]string""map禁止设置信息头字段 Referer 匹配值的访问
enable-modsecurityboolfalse--设置是否加载 Mod-Security 连接模块
enable-owasp-modsecurity-crsboolfalse--设置是否加载 OWASP ModSecurity 核心规则库

5、HTTPS 配置

提供与 HTTPS 相关的配置,功能说明如下表所示。

名称类型默认值Nginx 指令功能描述
ssl-ciphersstringECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384: ECDHE-ECDSA-CHACHA20-POLY-1305:ECDHE-RSA-CHACHA20-POLY1305: ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256: ECDHE-ECDSA-AES256-SHA-384:ECDHE-RSA-AES256-SHA384:ECDHE-ECDSA -AES-128-SHA256:ECDHE-RSA-AES-128-SHA256ssl-ciphers参见《Nginx HTTPS服务器搭建》一节
ssl-ecdh-curvestringautossl_ecdh_curve参见《Nginx HTTPS服务器搭建》一节
ssl-dhparamstring""ssl_dhparam参见《Nginx HTTPS服务器搭建》一节
ssl-protocolsstringTLSv1.2ssl_protocols参见《Nginx HTTPS服务器搭建》一节
ssl-early-databooltruessl_early_data参见《Nginx HTTPS服务器搭建》一节
ssl-session-cachebooltruessl_session_cache参见《Nginx HTTPS服务器搭建》一节
ssl-session-cache-sizestring10mssl_session_cache参见《Nginx HTTPS服务器搭建》一节
ssl-session-ticketsbooltruessl_session_tickets参见《Nginx HTTPS服务器搭建》一节
ssl-session-ticket-keystring--ssl_session_tickets参见《Nginx HTTPS服务器搭建》一节
ssl-session-timeoutstring10mssl_session_timeout参见《Nginx HTTPS服务器搭建》一节
ssl-buffer-sizestring4kssl_buffer_size参见《Nginx HTTPS服务器搭建》一节
ssl-redirectbooltrue--当被配置的虚拟主机启用了 TLS 支持,则将虚拟主机的 HTTP 请求跳转到 HTTPS 请求
no-tls-redirect-locationsstring/.well-known/acme-challenge--一个以 "," 分隔的 location 列表,对列表中 location 的 HTTP 请求永远不会跳转到 HTTPS 请求

6、HSTS 配置

HSTS(HTTP Strict Transport Security)是一种新的 Web 安全协议,HSTS 配置启用后,将强制客户端使用 HTTPS 协议与服务器建立连接,配置映射提供的 HSTS 功能配置功能说明如下表所示。

名称类型默认值Nginx 指令功能描述
hstsbooltrueadd_header是否运行 SSL 时在消息头中添加 HSTS 属性字段
hsts-include-subdomainsbooltrueadd_header是否在当前域名的所有子域中启用 HSTS
hsts-max-agestring15724800add_header设置 HSTS Header 的过期时间
hsts-preloadboolfalseadd_header设置是否启用 HSTS 的预加载支持

7、认证转发配置

提供认证转发功能的全局配置,功能说明如下表所示。

名称类型默认值Nginx 指令功能描述
global-auth-urlstring""auth_request设置外部身份验证的 URL
global-auth-methodstring""proxy_method外部认证 URL 的 HTTP 方法
global-auth-signinstring""--当外部认证返回 401 时跳转的 URL,通常为提示输人用户名和密码的 URL
global-auth-response-headersstring""--设置认证请求完成后传递到真实后端的头信息
global-auth-request-redirectstring""--设置发送给认证服务器请求头中 X-Auth-Request-Redirect 的值
global-auth-snippetstring""--可以自定义在外部认证指令区域添加 Nginx 配置指令
global-auth-cache-keystring""--启用认证缓存,并设置认证缓存的关键字
global-auth-cache-durationstring200 202 401 5m--基于响应码设置认证缓存的有效时间
no-auth-locationsstring/.well-known/acme-challenge--一个以 "," 分隔的 location 列表,列表中被记录的请求将不进行身份认证

8、代理配置

设置 Nginx 的代理功能配置,相关配置说明如下表所示。

名称类型默认值Nginx 指令功能描述
retry-non-idempotentboolfalseproxy_next_upstream参见《Nginx HTTP代理服务器》一节
proxy-set-headerstring""proxy_set_header参见《Nginx HTTP代理服务器》一节
proxy-headers-hash-max-sizeint512proxy_headers_hash_max_size参见《Nginx HTTP代理服务器》一节
proxy-headers-hash-bucket-sizeint64proxy_headers_hash_bucket_size参见《Nginx HTTP代理服务器》一节
hide-headersstring arrayemptyproxy_hide_header参见《Nginx HTTP代理服务器》一节
proxy-body-sizestring1mclient_max_body_size--
allow-backend-server-headerboolfalseproxy_pass_header设置是否允许将被代理服务器的消息头字段 Server 的值代替 Nginx 的默认值返回给客户端
proxy-connect-timeoutint5proxy_connect_timeout参见《Nginx HTTP代理服务器》一节
proxy-read-timeoutint60proxy_read_timeout参见《Nginx HTTP代理服务器》一节
proxy-send-timeoutint60proxy_send_timeout参见《Nginx HTTP代理服务器》一节
proxy-buffers-numberint4proxy_buffers参见《Nginx HTTP代理服务器》一节
proxy-buffer-sizestring4kproxy_buffer_size参见《Nginx HTTP代理服务器》一节
proxy-cookie-pathstringoffproxy_cookie_path参见《Nginx HTTP代理服务器》一节
proxy-cookie-domainstringoffproxy_cookie_domain参见《Nginx HTTP代理服务器》一节
proxy-next-upstreamstringerror timeoutproxy_next_upstream参见《Nginx HTTP代理服务器》一节
proxy-next-upstream-timeoutint0proxy_next_upstream_timeout参见《Nginx HTTP代理服务器》一节
proxy-next-upstream-triesint3proxy_next_upstream_tries参见《Nginx HTTP代理服务器》一节
proxy-redirect-fromstringoffproxy_redirect此处为添加要替换的源文本
proxy-redirect-tostringoffproxy_redirect此处为添加要替换的目标文本
proxy-request-bufferingstringonproxy_request_buffering参见《Nginx HTTP代理服务器》一节
proxy-bufferingstringoffproxy_buffering参见《Nginx HTTP代理服务器》一节
proxy-add-original-uri-headerbooltrueproxy_set_header为发送到后端的请求头添加一个头属性字段 X-Original-Uri,记录原始请求
use-forwarded-headersboolfalseproxy_set_header设置是否使用传入的头属性字段 X-Forwarded
forwarded-for-headerstringX-Forwarded-Forproxy_set_header设置用于标识客户端源 IP 的头属性字段名称 X-For-warded
compute-full-forwarded-forboolfalseproxy_set_header设置是否将远程地址附加到头属性字段 X-For-warded-For 中,而不是替换它
custom-http-errors[]intofferror_page该配置会自动启用 Nginx 配置指令 proxy_intercept_errors,默认是关闭的
proxy-stream-timeoutstring600sproxy_timeout参见《Nginx TCP/UDP代理》一节
proxy-stream-responsesint1proxy_responses参见《Nginx TCP/UDP代理》一节
use-proxy-protocolboolfalselisten启用 proxy_protocol 支持
proxy-protocol-header-timeoutstring5dproxy_protocol_timeout设置接收 proxy-protocol 头的超时时间,可防止 TLS 传递处理程序无限期地等待已断开的连接
proxy-real-ip-cidr[]string0.0.0.0/0set_real_ip_from当启用 proxy-protocol 时设置授信 IP,用于后端获得真实客户端 IP
use-http2booltruelisten启用 HTTP2 监听
http2-max-field-sizestring4khttp2_max_field_size参见《Nginx HTTP2模块配置》一节
http2-max-header-sizestring16khttp2_max_header_size参见《Nginx HTTP2模块配置》一节
http2-max-requestsint1000http2_max_requests参见《Nginx HTTP2模块配置》一节

9、负载均衡配置

Nginx Ingress 为方便上游服务器组的动态管理,其基于 Lua 实现了轮询调度及峰值指数加权移动平均(Peak Exponentially Weighted Moving-Average,Peak EWMA)负载均衡算法。配置映射的配置为全局负载均衡的配置,详见本节的注解负载均衡说明。配置映射还提供了被代理服务器长连接的配置支持,配置说明如下表所示。

名称类型默认值Nginx 指令功能描述
load-balancestringround_robin--设置负载均衡算法,支持轮询 round_robin 和 Peak EWMA 两种模式,基于 OpenResty 的 balancer_by_lua 模块实现
upstream-keepalive-connectionsint32keepalive参见《Nginx负载均衡模块》一节
upstream-keepalive-timeoutint60keepalive_timeout参见《Nginx负载均衡模块》一节
upstream-keepalive-requestsint100keepalive_requests参见《Nginx负载均衡模块》一节

10、日志配置

设置 Nginx 的日志功能配置,相关配置说明如下表所示。

名称类型默认值Nginx 指令功能描述
disable-access-logboolfalseaccess-log设置 HTTP 指令域 access-log 的指令值为 off
access-log-paramsstring""access-log设置访问日志的参数
access-log-pathstring/var/log/nginx/access.logaccess-log设置访问日志路径
log-format-escape-jsonboolfalselog_format设置日志格式为 JSON
log-format-upstreamstring%v-[$the_real_ip]-$remote_user[$time_local] "$request" $status $body_bytes_sent "$http_referer" "$http_user_agent" $request_length $request_time [$proxy_up-stream_name] $upstream_addr $upstream_response_length $upstream_response_time $upstream_status $req_idlog_formatHTTP 日志模板
skip-access-log-urls[]string[]string{}access-log设置不进行访问日志记录的 URL,access-log 的 if 参数
enable-access-log-for-default-backendboolfalseaccess-log是否开启默认后端的访问日志记录
log-format-streamstring[$time_local] $protocol $status $bytes_sent $bytes_received $session_timelog_formatTCP/UDP 日志模板
error-log-pathstring/var/log/nginx/error.logerror_log错误日志路径
error-log-levelstringnoticeerror_log错误日志级别

11、分布式跟踪配置

设置分布式跟踪功能的配置,配置键及功能描述如下表所示。

名称类型默认值功能描述
generate-request-idbooltrue如果请求头中没有属性字段 X-Request-ID,则为该请求随机创建一个,用于分布式链路跟踪
enable-opentracingboolfalse设置是否加载 opentracing 模块,启用分布式跟踪支持
zipkin-collector-hoststring""设置用于上传跟踪信息的 zipkin 主机地址
zipkin-collector-portint9411设置用于上传跟踪信息的 zipkin 主机端口
zipkin-service-namestringnginx设置用于在 zipkin 中创建跟踪的服务名称
zipkin-sample-ratefloat1.0设置 zipkin 跟踪的采样率
jaeger-collector-hoststring""设置用于上传跟踪信息的 jaeger 主机地址
jaeger-collector-portint6831设置用于上传跟踪信息的 jaeger 主机端口
jaeger-service-namestringnginx设置用于在 jaeger 中创建跟踪的服务名称
jaeger-sampler-typestringconst设置 jaeger 的采样器名称,可选项为 const、probabilistic、ratelimiting、remote
jaeger-sampler-paramstring1设置 jaeger 采样器的参数
jaeger-sampler-hoststringhttp://127.0.0.1设置 jaeger 采样器为 remote 时的主机地址
jaeger-sampler-portint5778设置 jaeger 采样器为 remote 时的主机端口

本页目录