为什么 uWSGI 不遵守“--http-keepalive”标志?

Why isn't uWSGI respecting the "--http-keepalive" flag?

我使用以下命令在 Docker 容器 运行 ubuntu:16.04 中安装了 uWSGI:

apt-get update
apt-get install -y build-essential python-dev python-pip
pip install uwsgi

然后我创建了一个静态文件:

cd /root
mkdir static
dd if=/dev/zero bs=1 count=1024 of=static/data

...最后使用以下命令启动 uWSGI:

uwsgi \
    --buffer-size 32768 \
    --http-socket 0.0.0.0:80 \
    --processes 4 \
    --http-timeout 60 \
    --http-keepalive \
    --static-map2=/static=./

我能够毫无问题地访问静态文件。但是,尽管传递了 --http-keepalive 选项,但使用 cURL 发出多个请求会导致以下输出:

# curl -v 'http://myserver/static/data' -o /dev/null 'http://myserver/static/data' -o /dev/null
*   Trying 192.168.1.101...
...
> GET /static/data HTTP/1.1
> Host: 192.168.1.101:8100
> User-Agent: curl/7.47.0
> Accept: */*
> 
< HTTP/1.1 200 OK
< Content-Length: 1024
< Last-Modified: Sat, 03 Dec 2016 22:06:49 GMT
< 
{ [1024 bytes data]
100  1024  100  1024    0     0   577k      0 --:--:-- --:--:-- --:--:-- 1000k
* Connection #0 to host 192.168.1.101 left intact
* Found bundle for host 192.168.1.101: 0x563fbc855630 [can pipeline]
* Connection 0 seems to be dead!
* Closing connection 0
...

特别感兴趣的是行:

* Connection 0 seems to be dead!

这已通过 WireShark 确认:

如您所见,有两个完全独立的 TCP 连接。第一个被 uWSGI 关闭(packet #10 - [FIN, ACK])。

我做错了什么?为什么 uWSGI 不遵守 --http-keepalive 标志而不是立即关闭连接?

通过从 --http-socket 切换到简单的 --http,我终于能够让 keepalive 正常工作。根据 uWSGI 文档:

If your web server does not support the uwsgi protocol but is able to speak to upstream HTTP proxies, or if you are using a service like Webfaction or Heroku to host your application, you can use http-socket. If you plan to expose your app to the world with uWSGI only, use the http option instead, as the router/proxy/load-balancer will then be your shield.

在我的特殊情况下,还需要加载 http 插件。

就我而言,我遇到了来自 aws ALB / ELB 的随机 502 响应。 我通过 .ini 文件提供配置,例如:

http-keepalive = true

但经过几个小时的调试,我在 Wireshark 中看到了类似的画面——每次响应后,连接都被服务器关闭,所以 keep-alive 选项被忽略了

https://github.com/unbit/uwsgi/issues/2018 the discussion points that it should be an integer (https://uwsgi-docs.readthedocs.io/en/latest/Options.html#http-keepalive) 中,但不幸的是找不到确切的信息它代表套接字生命周期的秒数,还是简单的“1”。进行此更改后 - 随机 502 消失,uwsgi 开始以预期模式工作。

希望对某些人也有帮助。