UDP sessions and timeouts
Understand UDP pseudo-sessions
UDP itself has no connection handshake. Nginx maintains temporary associations for forwarding datagrams. For Linux listeners use udp and reuseport as documented to help maintain affinity across workers. The example is intentionally one request and one response per association: a later datagram can open a new session. Do not use this setting for protocols expecting multiple replies without review. The lab uses two toy datagram responders, not DNS.
upstream udp_echo { server 127.0.0.1:19114; server 127.0.0.1:19115;}server { listen 127.0.0.1:19111 udp reuseport; proxy_pass udp_echo; proxy_requests 1; proxy_responses 1; proxy_timeout 3s;}Choose bounded timeout and failure behavior
proxy_timeout is an inactivity interval for stream I/O rather than an overall session deadline. proxy_connect_timeout is a separate limit on establishing the upstream connection. UDP response counts describe expected replies, not guaranteed delivery. When no reply arrives, a UDP client should have its own deadline. The following alternative configuration explicitly disables upstream switching on connection failure; retrying after application data may be unsafe and a lost TCP connection cannot generally be replayed transparently.
server { listen 127.0.0.1:19120; proxy_pass tcp_echo; proxy_connect_timeout 1s; proxy_timeout 5s; proxy_next_upstream off;}Read session logs instead of HTTP access logs
Use a stream-specific format: protocol, session status, byte counts, duration and selected peer. Session status is Nginx session-processing status, not an HTTP response code from the application. A successful session does not prove the echo result is correct. Avoid logging payloads or credentials. The following stream-context directives assume the log path is writable under your isolated prefix.
log_format session '$protocol $status $bytes_received $bytes_sent ' '$session_time $upstream_addr';access_log logs/stream.log session;Compare a connection and a datagram test
A TCP client must frame application messages itself; our fixture uses one newline-terminated token per message and reads one reply line. UDP uses one small token per datagram and checks the returned datagram. Use fixed loopback ports, at most four probes per transport, and a client deadline. The command below runs offline tests only; the separate lab guide describes optional live probes.
python3 -B verify_offline.py# Expected: all offline fixture tests pass; no Nginx traffic is generated.Practice and next steps
Explain why UDP has session state at the proxy and choose response counts and timeout for a one-reply protocol.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
UDP sessions and timeouts
Explain state and limits under UDP proxying
Why can Nginx track UDP sessions?
A protocol sends one reply per datagram. Which lab settings fit?
What does proxy_timeout limit?
What does stream status 200 prove?
What should a client do when a UDP reply is lost?