James Duffy

Configuring uWSGI for Production Deployment

// By Updated:
Bookmark of Configuring uWSGI for Production DeploymentArchived Page

I constantly come back to this article when configuring uWSGI.

A majority of time I use a Load Balancer and adding nginx or another server is generally not required as long as the config is setup correctly.

Below is my default config when starting a project:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
[uwsgi]
strict = true
master = true
need-app = true
single-interpreter = true

pidfile = /var/run/uwsgi.pid

die-on-term = true                    ; Shutdown when receiving SIGTERM (default is respawn)

socket = /tmp/uwsgi.sock
chown-socket = www-data
chmod-socket = 664

harakiri = 65                        ; Forcefully kill workers after 60 seconds

max-requests = 10000                 ; Restart workers after this many requests
max-worker-lifetime = 10800          ; Restart workers after this many seconds
reload-on-rss = 512                  ; Restart workers after this much resident memory (MB)
worker-reload-mercy = 60             ; How long to wait before forcefully killing workers

module = app.main:create_app()       ; Basic Flask App
buffer-size=32768

hook-master-start = unix_signal:15 gracefully_kill_them_all

ignore-sigpipe = true
ignore-write-errors = true
disable-write-exception = true

http = 0.0.0.0:8000
http-auto-chunked = true
http-keepalive = 65                  ; Must be longer than LB Idle Timeout
http-timeout = 200

logformat = [%(ltime)] %(addr) "%(var.HTTP_X_FORWARDED_FOR)" "%(method) %(uri) %(proto)" %(status) %(size) %(micros) "%(referer)" "%(uagent)"

Additionally I will configure the number of processes and threads via environment variables to allow easier configuration without a redeploy/container rebuild.

1
2
UWSGI_PROCESSES="4"
UWSGI_THREADS="4"