uwsgi支持http长链接

时间:2022-06-21 02:24:07

http1.1支持长链接,而http1.0不支持,所以,在切换http版本号或者升级服务端版本时候,尤其要注意这个造成的影响。

当客户端以http1.1长链接方式连接服务端时,服务端如果不支持1.1,客户端会进行协议降级,用1.0通信,即使用短连接,此时服务端可以正常提供服务;

一旦服务端http版本由于某种原因切换到1.1,则长链接就会生效,如果服务端不支持长链接,则就会造成频繁的502发生;

因为,短连接在服务端return后就会向客户端发一个reset消息,客户端就会切断链路,如果客户端认为是长链接,就会复用该通道,此时reset,会造成内核态的数据被清空,造成数据丢失,表现为502;

如果发现http1.0正常,1.1会经常出现502,则可以考虑此种可能。

附一个检测长链接的脚本:

def query():
i = 2000
conn = httplib2.HTTPConnectionWithTimeout("*.*.*.*", port, timeout=60)
while i > 0:
headers = {
'content-type': 'application/json',
'accept': 'application/json',
'Connection': 'Keep-Alive',
}
conn.request('GET', '***', headers=headers)
response = conn.getresponse()
resp_status = str(response.status)
rsp = response.read()
print rsp
status_ok_list = [HTTP_200_OK, HTTP_201_CREATED, HTTP_204_NO_CONTENT, HTTP_202_ACCEPTED]
if resp_status not in status_ok_list:
print 'get error rsp, resp_status is %s' % resp_status
i -= 1

django1.11之前,不支持http 1.1,因为不会自动添加Content_Length,但1.1后会,所以,1.1后是可以支持1.1的,一般情况下,我们配置uwsgi是采用短连接的方式,即,http_socket = :8000或socket = 0.0.0.0:8000,protocol = http;此种配置不支持长链接,如要支持长链接,需要使用http11_soscket = :8000,http-keepalive = true,具体配置如下:

[uwsgi]
http11-socket = :8000
http-keepalive = true
master = true
hara-kiri = 180
chdir = /home/test_server
manage-script-name = true
mount = /client=/home/test_server/test/wsgi.py
processes = 4
threads = 40
pidfile = /usr/local/bin/uwsgi.pid
daemonize = /home/test_server/logs/uwsgi.log
buffer-size = 32768
listen = 1024
reload-on-rss = 1024
cheaper = 2
cheaper-initial = 2
die-on-term = true
py-tracebacker = /tmp/tbsocket.
log-maxsize = 50000000