【问题标题】:How can I enable keep-alive on flask running behind uwsgi?如何在 uwsgi 后面运行的烧瓶上启用 keep-alive?
【发布时间】:2020-03-11 02:53:30
【问题描述】:

我正在使用 Flask 运行 python(3.6)。它在uwsgi 后面运行。我需要在这个应用程序上启用 keep-alive,所以我在 uwsgi 命令上添加了--so-keepalive 参数,如下所示:

uwsgi \
  --uid uwsgi \
  --master \
  --http :8080 \
  --enable-threads \
  --so-keepalive \
  --wsgi-file api/uwsgi.py

当检查请求响应标头时,keep-alive 不会出现。它仅出现在请求标头上。响应头是:

HTTP/1.1 200 OK
Content-Type: application/json
Content-Length: 99

我使用curl来测试连接:

curl -Iv http://localhost:9301/v2/health  --next http://localhost:9301/v2/health

*   Trying ::1...
* TCP_NODELAY set
* Connected to localhost (::1) port 9301 (#0)
> HEAD /v2/health HTTP/1.1
> Host: localhost:9301
> User-Agent: curl/7.64.1
> Accept: */*
>
< HTTP/1.1 200 OK
HTTP/1.1 200 OK
< Content-Type: application/json
Content-Type: application/json
< Content-Length: 5
Content-Length: 5

<
* Connection #0 to host localhost left intact
* Found bundle for host localhost: 0x7fb4db60b7d0 [can pipeline]
* Could pipeline, but not asked to!
* Connection 0 seems to be dead!
* Closing connection 0
* Hostname localhost was found in DNS cache
*   Trying ::1...
* TCP_NODELAY set
* Connected to localhost (::1) port 9301 (#1)
> GET /v2/health HTTP/1.1
> Host: localhost:9301
> User-Agent: curl/7.64.1
> Accept: */*
>
< HTTP/1.1 200 OK
< Content-Type: application/json
< Content-Length: 5
<
"OK"
* Connection #1 to host localhost left intact
* Closing connection 1

您可以看到它创建了两个连接#0#1。我的服务器上似乎没有使用保持连接。

我想知道我的应用程序是否启用了 keep-alive?如何启用它?

【问题讨论】:

标签: python uwsgi


【解决方案1】:

你应该使用http11_socket参数,比如

http11-socket = 0.0.0.0:8080
add-header = Connection: Keep-Alive

请参考https://uwsgi-docs.readthedocs.io/en/latest/HTTP.html

【讨论】:

    猜你喜欢
    • 2012-01-04
    • 2014-04-07
    • 2015-05-08
    • 1970-01-01
    • 2019-09-05
    • 1970-01-01
    • 2017-11-16
    • 2013-08-26
    • 1970-01-01
    相关资源
    最近更新 更多