James Duffy

Configuring uWSGI for Production Deployment

Bookmark of Configuring uWSGI for Production Deployment — Archived 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[uwsgi]
 2strict = true
 3master = true
 4need-app = true
 5single-interpreter = true
 6
 7pidfile = /var/run/uwsgi.pid
 8
 9die-on-term = true                    ; Shutdown when receiving SIGTERM (default is respawn)
10
11socket = /tmp/uwsgi.sock
12chown-socket = www-data
13chmod-socket = 664
14
15harakiri = 65                        ; Forcefully kill workers after 60 seconds
16
17max-requests = 10000                 ; Restart workers after this many requests
18max-worker-lifetime = 10800          ; Restart workers after this many seconds
19reload-on-rss = 512                  ; Restart workers after this much resident memory (MB)
20worker-reload-mercy = 60             ; How long to wait before forcefully killing workers
21
22module = app.main:create_app()       ; Basic Flask App
23buffer-size=32768
24
25hook-master-start = unix_signal:15 gracefully_kill_them_all
26
27ignore-sigpipe = true
28ignore-write-errors = true
29disable-write-exception = true
30
31http = 0.0.0.0:8000
32http-auto-chunked = true
33http-keepalive = 65                  ; Must be longer than LB Idle Timeout
34http-timeout = 200
35
36logformat = [%(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.

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