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.

 

http://m.baidu.com/from=1015241i/bd_page_type=1/ssid=0/uid=0/pu=usm%401%2Csz%40320_1001%2Cta%40iphone_2_5.0_3_537/baiduid=B94EE8A48BD52CAC5643B53C9D144072/w=0_10_/t=iphone/l=3/tc?ref=www_iphone&lid=11740332057149392145&order=4&fm=alop&tj=www_normal_4_0_10_title&vit=osres&m=8&srd=1&cltj=cloud_title&asres=1&title=%E5%85%B3%E4%BA%8E%E8%AE%BE%E7%BD%AEhttp%E5%93%8D%E5%BA%94%E5%A4%B4connection%E7%9A%84%E4%BD%9C%E7%94%A8-%E5%B0%8F%E7%94%9F%E5%AD%A6%E8%89%BA-...&dict=30&w_qd=IlPT2AEptyoA_yixCFOxnh4ZOl5jt7Z7okpUswyLMlm6o53ye3TmHte&sec=22958&di=2e101b56dab3205e&bdenc=1&tch=124.0.20.777.1.945&nsrc=IlPT2AEptyoA_yixCFOxXnANedT62v3IJRqYKytQ2zSqmEeyhPOlGMJfSzK6M8SLZpPPxmqFhgoDlnCh0mQi9K&eqid=a2ee09cd9385480010000003598338a4&wd=&clk_info=%7B%22srcid%22%3A%221599%22%2C%22tplname%22%3A%22www_normal%22%2C%22t%22%3A1501771950589%2C%22sig%22%3A%2210434%22%2C%22xpath%22%3A%22div-a-h3%22%7D&sfOpen=1

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327031939&siteId=291194637