About the role of setting the http response header connection

  Some websites will go down after the server runs for a period of time. There are many reasons that may cause this phenomenon: for example, the tomcat heap and non-heap memory settings are insufficient, the program fails to release memory space, causing memory overflow, or some processes have been running and failed. Released, resulting in a large consumption of cup resources.

      But in addition to the program itself, it may also be caused by client access (of course, this client also includes search engines such as spider software), if the server and client establish a long link (you can use the "netstat -a" command to view Network access information), which requires certain settings for the connection of the http response header.

      The introduction is as follows:

1. Explain:

    In http1.1, a connection header field may appear in both the request and response headers. The meaning of this header is how to deal with long links when the client and server communicate.

    In http1.1, the client and server both support long links by default. If the client uses the http1.1 protocol, but does not want to use long links, it needs to specify the connection value in the header as close; if the server also If you don't want to support long links, you also need to clearly state that the value of connection is close in the response.

    Regardless of whether the header of the request or the response contains a connection with a value of close, it indicates that the tcp connection currently in use will be disconnected after the request is processed. In the future, when the client makes a new request, a new tcp connection must be created. The close setting of an HTTP Connection allows either the client or the server to close the underlying connection. Both parties will request that their TCP connection be closed after processing the request.

2. How to set in the program:

    Can be added to the filter : response.setHeader("connection", "close");

Related to this: Solving the problem of a large number of close_wait generated by the server

To solve this problem, you can modify the system parameters ( /etc/sysctl.conf file ), the default timeout of the system is 7200 seconds, which is 2 hours.

The default is as follows:

tcp_keepalive_time = 7200 seconds (2 hours)
tcp_keepalive_probes = 9
tcp_keepalive_intvl = 75 seconds


It means that if a TCP connection is idle for 2 hours, the kernel will initiate a probe. If the probe is unsuccessful 9 times (75 seconds each time), the kernel will give up completely and consider the connection to be invalid.


after modification


sysctl -w net.ipv4.tcp_keepalive_time=30
sysctl -w net.ipv4.tcp_keepalive_probes=2
sysctl -w net.ipv4.tcp_keepalive_intvl=2


After this modification, the server will recycle unclosed tcp connections in a short period of time.



Guess you like