Joey Yi Zhao
Joey Yi Zhao

Reputation: 42482

How can I enable keep-alive on flask running behind uwsgi?

I am running a python(3.6) with Flask. It runs behind uwsgi. I need to enable keep-alive on this app so I added --so-keepalive parameter on uwsgi command like below:

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

and when inspect the request response header, keep-alive doesn't appear. It only appears on the request header. The response header is:

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

I use curl to test the connection:

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

And you can see that it creates two connections #0 and #1. It doesn't seem to use keep-alive connection on my server.

I wonder is keep-alive enabled on my application? How can I enable it?

Upvotes: 0

Views: 3174

Answers (1)

zhangchi
zhangchi

Reputation: 21

you should use http11_socket parame, such as

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

please refer https://uwsgi-docs.readthedocs.io/en/latest/HTTP.html

Upvotes: 2

Related Questions