Mastering TCP: Decode the 11 Connection States and Solve Nginx TIME_WAIT Issues
This article explains the 11 TCP connection states—including the three‑way handshake and four‑way termination—illustrated with packet captures, shows how to inspect them using netstat or ss, and discusses common Nginx TIME_WAIT problems and strategies for troubleshooting and performance tuning.
Before tackling Nginx TIME_WAIT problems, it is essential to understand the 11 TCP connection states that appear during connection establishment and termination.
Operators typically use
netstator
ssto view current TCP states.
The 11 states consist of five states in the three‑way handshake and six states in the four‑way termination. The diagram below summarizes them:
Three‑Way Handshake
A captured packet trace shows the complete handshake process, matching the state diagram.
CLOSED : Initial state, connection is not open.
LISTEN : Server socket is listening for incoming connections.
SYN_RCVD : Server has received a SYN from the client; a short‑lived intermediate state.
SYN_SENT : Client has sent a SYN and is awaiting the server's SYN‑ACK.
ESTABLISHED : Connection is fully established and data can be transferred.
During the handshake,
CLOSEDand
ESTABLISHEDappear on both client and server, while
LISTENand
SYN_RCVDare typically server‑side and
SYN_SENTis client‑side. Nginx often acts as both client and server, so distinguishing roles by port is advisable.
Four‑Way Termination
The following diagram illustrates the six termination states:
FIN_WAIT_1 : Client initiates close, sends FIN and waits for ACK.
FIN_WAIT_2 : After receiving ACK, client waits for the server's FIN.
CLOSE_WAIT : Passive side has received FIN and awaits its own close.
LAST_ACK : Passive side has sent FIN and waits for the final ACK.
TIME_WAIT : Active side has received the server's FIN and stays in this state to ensure all packets are drained.
CLOSING : Both sides simultaneously send FIN, leading to this rare intermediate state before moving to CLOSED.
Understanding these states helps quickly locate performance bottlenecks and configure appropriate kernel parameters.
The article sets the stage for a follow‑up discussion on how to efficiently recycle TIME_WAIT sockets in Nginx.
Efficient Ops
This public account is maintained by Xiaotianguo and friends, regularly publishing widely-read original technical articles. We focus on operations transformation and accompany you throughout your operations career, growing together happily.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.