# ============================================ # Nginx http 配置 # ============================================ # ---------- 1. 哈希桶大小 ---------- # 当 Nginx 启动时报错 "server_names_hash_bucket_size" 相关错误时, # 需要增大此值。这里设为 128,可支持较多或较长的 server_name。 # 注意:该指令只能出现在 http 块中。 server_names_hash_bucket_size 128; # ---------- 2. 客户端请求体缓冲区大小 ---------- # 设置读取客户端请求体时使用的缓冲区大小。 # 如果请求体大于此值,Nginx 会将请求体写入临时文件, # 从而避免占用过多内存。适用于全局默认配置。 client_body_buffer_size 128K; # ---------- 3. 自定义日志格式 ---------- # 定义名为 stripsecrets 的日志格式。 # 使用 $secretfilter 变量(由后面的 map 定义)代替原始请求行, # 从而在日志中隐藏敏感参数(如 api_key)。 log_format stripsecrets '$remote_addr $host - $remote_user [$time_local] ' '"$secretfilter" $status $body_bytes_sent ' '$request_length $request_time $upstream_response_time ' '"$http_referer" "$http_user_agent"'; # ---------- 4. 使用 map 过滤敏感参数 ---------- # map 指令在 http 块中定义变量 $secretfilter, # 它根据 $request(原始请求行,如 "GET /path?api_key=abc123&foo=bar HTTP/1.1") # 匹配正则并替换 api_key 参数值为 "***"。 # 匹配时保留参数名和其他参数,只隐藏值。 map $request $secretfilter { # 匹配小写 api_key= 的情况 ~*^(?.*[\?&]api_key=)([^&]*)(?.*)$ "${prefix1}***$suffix1"; # 匹配首字母大写 ApiKey= 的情况 ~*^(?.*[\?&]ApiKey=)([^&]*)(?.*)$ "${prefix1}***$suffix1"; # 如果没有匹配到,则原样返回请求行 default $request; } # ---------- 5. 包含站点配置 ---------- # 加载 sites-enabled 目录下的所有配置文件(通常是 server 块)。 # 这允许将不同站点的配置分开管理,类似于 Apache 的做法。 include sites-enabled/*;