HTTP Server to Python script

While there are many tutorials how to setup NGINX with uWSGI or gunicorn and Python code, almost none of them give an overview how request goes from NGINX to Python.

Input

  1. NGINX listens to TCP port.
  2. Client opens connection and sends HTTP request.
  3. NGINX accepts connection and parses request.

Request within NGINX

NGINX decides, accorting to configuration, what to do with request. It can:

uWSGI

  1. uWSGI listens to TCP port or Unix socket. (Latter is preferred because of performance.)
  2. NGINX sends request formed accroding to binary uwsgi protocol. first byte of request packet header, modifier1, is 0.
  3. uWSGI accepts it and calls Python script.

uwsgi protocol documentation also supports other types of requests, not only WSGI; it’s defined by first byte of request packet header, modifier1.

Configuring NGINX to pass requests to uWSGI.

gunicorn

  1. gunicorn listens to TCP port or Unix socket. (Latter is preferred because of performance.)
  2. NGINX sends (possibly modified) HTTP request.
  3. gunicorn accepts it and calls Python script.

Configuring NGINX to pass requests to gunicorn.

Python script

CPython provides binary API to call Python callables from native, binary code. That’s why uWSGI works specifically with CPython. Other interpreters, specifically PyPy and Jython, are supported individually.