apisix icon indicating copy to clipboard operation
apisix copied to clipboard

feat: support `backlog` in listen and `if=` condition in access_log via nginx_config

Open Ethan-Zhang-ZH opened this issue 1 month ago • 3 comments

Description

I'd like to propose two small config-surface enhancements to apisix/cli/ngx_tpl.lua. Both are currently impossible without patching the template or maintaining a custom nginx.conf, but nginx itself fully supports them.

1. backlog for listen sockets

Currently node_listen renders:

listen {* item.ip *}:{* item.port *} default_server {% if enable_reuseport then %} reuseport {% end %};

There is no way to tune backlog=N from config.yaml. Under connection spikes the kernel accept queue can overflow (SYN drops, TCP: request_sock_TCP: Possible SYN flooding on port 9080), and operators have to fix net.core.somaxconn on the host but still cannot raise nginx's backlog (default 511) for APISIX.

Proposal: allow an optional backlog field per listener:

nginx_config:
  http:
    node_listen:              # 9080
      - ip: 0.0.0.0
        port: 9080
        backlog: 8192         # optional, absent -> rendered as today

2. if= condition for access_log

Currently the access log is rendered as:

access_log {* http.access_log *} main buffer={* http.access_log_buffer *} flush=3;

nginx supports access_log path [format [buffer=size] [gzip[=level]] [flush=time] [if=condition]] (https://nginx.org/en/docs/http/ngx_http_log_module.html). The condition is evaluated in the log phase and can reference variables defined by map/split_clients, which makes things like log sampling or per-status/per-endpoint log exclusion possible with plain nginx config — no Lua plugin needed.

Note that nginx's parser treats the 2nd parameter unconditionally as the format name; if= is only recognized from the 3rd parameter onward. Since the template already appends main right after the path, an optional if= can simply be inserted after main (before or after buffer=, both are accepted), which keeps the current output byte-for-byte identical when the option is absent.

Proposal:

nginx_config:
  http:
    access_log_if: $loggable   # optional; renders `... main buffer=16384 flush=3 if=$loggable;`

The condition variable itself would be declared by users via nginx_config.http_configuration_snippet, e.g. sampling 200s into a separate file:

# http_configuration_snippet
map $status $loggable_200 {
    ~^200$  $sample_flag;
    default 1;
}
map $request_id $sample_flag {
    default "";
}
access_log logs/access_200_sample.log main if=$loggable_200;

Motivation

  • backlog: production gateways behind high-QPS ingress regularly hit accept-queue overflow during traffic spikes and restarts (warm-up), and the only supported knob today is the host-level somaxconn. Exposing backlog closes the gap.
  • if=: log volume is a common operational pain. if= is the standard nginx mechanism for conditional logging (status filtering, sampling, excluding health checks / endpoints with sensitive query params). It would also be the generic fix for #10407, which asks for excluding certain endpoints from access logging.

Both changes are template-only (ngx_tpl.lua + conf/config.yaml.example + docs), optional fields, fully backward compatible. I'm willing to submit the PRs.

Ethan-Zhang-ZH avatar Oct 09 '26 10:10 Ethan-Zhang-ZH

Hi, I'd like to contribute to this issue, preferably starting with the backlog configuration enhancement. Is this part still available to work on? I can update the template, example configuration, documentation, and relevant tests while preserving the existing default behavior.

aryan9948 avatar Oct 09 '26 19:10 aryan9948

Hi @Baoyuantop , sorry for the interruption. Could you please help review this PR when you have time?

Ethan-Zhang-ZH avatar Oct 19 '26 10:10 Ethan-Zhang-ZH

Hi @guoqqqi @Baoyuantop, I’d like to follow up on this issue. Could you please assign it to me? Thanks!

Ethan-Zhang-ZH avatar Nov 04 '26 03:11 Ethan-Zhang-ZH