Skip to content

uWSGI behind Nginx for Python applications

Configure uWSGI for a Python WSGI app with the binary uwsgi protocol, an Emperor-managed vassal, and Nginx as the front door.

uWSGI is still in the field — large, mature, and surprisingly fast when configured correctly. Teams that picked it years ago often have no reason to migrate to Gunicorn, but the configuration surface is wide enough that operators inherit weird tunings. This article sets up uWSGI with the binary uwsgi protocol, Nginx in front, and Emperor mode if you run more than one app on the box.

How to verify

Look at the current uWSGI install and its status:

which uwsgi && uwsgi --version
systemctl status uwsgi --no-pager | head
systemctl status uwsgi-emperor --no-pager | head 2>/dev/null
ls -la /run/uwsgi/ 2>/dev/null
ls /etc/uwsgi/apps-enabled/ 2>/dev/null
sudo ss -lxn | grep uwsgi

If both uwsgi.service and uwsgi-emperor.service are loaded, only one should be enabled — running both produces duplicate workers binding the same sockets.

What’s happening

uWSGI speaks the binary uwsgi protocol over a UNIX socket and Nginx has a built-in uwsgi_pass directive that uses it. That handoff is faster than HTTP because there is no header parsing on either side — the framing is fixed and the protocol overhead is a few bytes per request.

Two operational shapes exist. Single-app mode runs uwsgi --ini /etc/uwsgi/app.ini under one systemd unit. Emperor mode runs a single supervisor process that watches a directory (/etc/uwsgi/vassals/) and spawns a “vassal” per .ini file, restarting any vassal whose .ini is modified. Emperor mode is the right choice when you host multiple Python apps on one VM; single-app is simpler when you do not.

The protocol matters too. http mode makes uWSGI speak plain HTTP — easier to debug, slightly slower. socket mode (socket = /run/uwsgi/app.sock) uses the binary protocol. Use binary in production, HTTP only for local testing.

The procedure

  1. Install uWSGI into the project venv (system packages exist but are usually out of date):

    sudo -iu deploy
    /opt/venvs/app/bin/pip install "uwsgi" 
    /opt/venvs/app/bin/uwsgi --version
  2. Create the app .ini. /etc/uwsgi/apps-available/app.ini:

    [uwsgi]
    chdir = /var/www/app
    module = app.wsgi:application
    home = /opt/venvs/app
    master = true
    processes = 5
    threads = 2
    socket = /run/uwsgi/app.sock
    chmod-socket = 660
    chown-socket = deploy:www-data
    vacuum = true
    die-on-term = true
    harakiri = 60
    max-requests = 1000
    buffer-size = 32768
    log-master = true
    logto = /var/log/uwsgi/app.log

    harakiri = 60 kills a worker stuck on one request for 60 s — uWSGI’s equivalent of Gunicorn’s timeout. vacuum = true removes the socket file when the master exits.

  3. For single-app mode, drop a systemd unit at /etc/systemd/system/uwsgi-app.service:

    [Unit]
    Description=uWSGI for app
    After=network.target
    
    [Service]
    User=deploy
    Group=www-data
    RuntimeDirectory=uwsgi
    ExecStart=/opt/venvs/app/bin/uwsgi --ini /etc/uwsgi/apps-available/app.ini
    ExecReload=/bin/kill -HUP $MAINPID
    KillSignal=SIGQUIT
    Restart=on-failure
    NotifyAccess=all
    
    [Install]
    WantedBy=multi-user.target

    KillSignal=SIGQUIT is what uWSGI expects for a clean shutdown.

  4. For Emperor mode, install Emperor as a separate unit and put vassal .ini files in /etc/uwsgi/vassals/:

    # /etc/systemd/system/uwsgi-emperor.service
    [Service]
    ExecStart=/opt/venvs/app/bin/uwsgi --emperor /etc/uwsgi/vassals --master --die-on-term --uid deploy --gid www-data
    Restart=on-failure
    KillSignal=SIGQUIT

    touch /etc/uwsgi/vassals/app.ini triggers a graceful reload of just that vassal.

  5. Point Nginx at the socket using uwsgi_pass, not proxy_pass:

    location / {
      include uwsgi_params;
      uwsgi_pass unix:/run/uwsgi/app.sock;
      uwsgi_read_timeout 90s;
      client_max_body_size 25m;
    }

    uwsgi_params ships with the Nginx distro and sets the protocol’s expected envelope variables.

Operational notes

  • threads > 1 only helps when the WSGI app does meaningful I/O. CPU-bound code with threads will be slower than the same code with more processes because of the GIL.
  • The master = true directive turns on the master process that supervises workers. Without it, you have N workers and nothing watching them — never run without master = true in production.
  • harakiri kills workers but does not log the request payload that triggered it. Pair it with harakiri-verbose = true and you get the URL in the log; without that, you only know “a request was slow”.
  • Emperor mode broadcasts vassal failures up the chain — set vassal-config-fallback-throttle = 30 so a broken .ini does not respawn 100 times a second.
  • uWSGI does not graceful-reload across binary upgrades by default. Use touch /tmp/uwsgi-app.fifo with the master-fifo option for chain-reload to roll workers one at a time without dropping connections.

For most new projects Gunicorn is the friendlier choice (see the Gunicorn article) — but inherited uWSGI is fine to keep running if it is tuned. In the engagements Stack Harbor takes over, we standardize the supervisor and the protocol so a host you operated last year still feels like the same one today — that consistency is part of managed operations.