Native HTTP support
HTTP sockets
The http-socket <bind>
option will make uWSGI natively speak HTTP. If yourweb server does not support the uwsgi protocol but is able tospeak to upstream HTTP proxies, or if you are using a service like Webfactionor Heroku to host your application, you can use http-socket
. If you planto expose your app to the world with uWSGI only, use the http
optioninstead, as the router/proxy/load-balancer will then be your shield.
The uWSGI HTTP/HTTPS router
uWSGI includes an HTTP/HTTPS router/proxy/load-balancer that can forwardrequests to uWSGI workers. The server can be used in two ways: embedded andstandalone. In embedded mode, it will automatically spawn workers and setupthe communication socket. In standalone mode you have to specify the address ofa uwsgi socket to connect to.
Embedded mode:
- ./uwsgi --http 127.0.0.1:8080 --master --module mywsgiapp --processes 4
This will spawn a HTTP server on port 8080 that forwards requests to a pool of4 uWSGI workers managed by the master process.
Standalone mode:
- ./uwsgi --master --http 127.0.0.1:8080 --http-to /tmp/uwsgi.sock
This will spawn a HTTP router (governed by a master for your safety) that willforward requests to the uwsgi socket /tmp/uwsgi.sock
. You can bind tomultiple addresses/ports.
- [uwsgi]
- http = 0.0.0.0:8080
- http = 192.168.173.17:8181
- http = 127.0.0.1:9090
- master = true
- http-to = /tmp/uwsgi.sock
And load-balance to multiple nodes:
- [uwsgi]
- http = 0.0.0.0:8080
- http = 192.168.173.17:8181
- http = 127.0.0.1:9090
- master = true
- http-to = /tmp/uwsgi.sock
- http-to = 192.168.173.1:3031
- http-to = 192.168.173.2:3031
- http-to = 192.168.173.3:3031
- If you want to go massive (virtualhosting and zero-conf scaling) combine theHTTP router with the uWSGI Subscription Server.
- You can make the HTTP server pass custom uwsgi variables to workers with the
http-var KEY=VALUE
option. - You can use the
http-modifier1
option to pass a custom modifier1 valueto workers.
HTTPS support
HTTP Keep-Alive
If your backends set the correct HTTP headers, you can use thehttp-keepalive
option. Your backends must either set a validContent-Length
in each response, or you can use chunked encoding withhttp-auto-chunked
. Simply setting “Connection: close” is not enough.
Also remember to set “Connection: Keep-Alive” in your response. You canautomate that using the add-header = Connection: Keep-Alive
option.
Since uWSGI 2.1 (master branch) you can use the http11-socket
option.http11-socket
may replace the add-header
and http-keepalive
options(but it doesn’t touch tcp stuff as so-keepalive
does).Once set the server will try to maintain the connection opened if a bunch ofrules are respected. This is not a smart http 1.1 parser (to avoid parsing thewhole response) but assumes the developer is generating the right headers.http11-socket
has been added to support RTSP protocol for video streaming.
HTTP auto gzip
With the http-auto-gzip
option, uWSGI can automatically gzip content if theuWSGI-Encoding
header is set to gzip while Content-Length
andContent-Encoding
are not set.
Can I use uWSGI’s HTTP capabilities in production?
If you need a load balancer/proxy it can be a very good idea. It willautomatically find new uWSGI instances and can load balance in various ways.If you want to use it as a real webserver you should take into account thatserving static files in uWSGI instances is possible, but not as good as using adedicated full-featured web server. If you host static assets in the cloud oron a CDN, using uWSGI’s HTTP capabilities you can definitely avoid configuringa full webserver.
Note
If you use Amazon’s ELB (Elastic Load Balancer) in HTTP mode infront of uWSGI in HTTP mode, either a valid Content-Length
_must be set_by the backend, or chunked encoding must be used, e.g., withhttp-auto-chunked
. The ELB “health test” may still fail in HTTP moderegardless, in which case a TCP health test can be used instead.
Note
In particular, the Django backend does not set Content-Length
bydefault, while most others do. If running behind ELB, either use chunkedencoding as above, or force Django to specify Content-Length
with theCommonMiddleware
(ConditionalGetMiddleware
in Django < 1.11)