Backend Development 17 min read

Understanding CGI, FastCGI, PHP‑FPM, WSGI, uWSGI and ASGI in Python Web Development

This article explains the origins, principles, and interactions of CGI, FastCGI, PHP‑FPM, WSGI, uWSGI, and ASGI, detailing how they enable dynamic Python web applications to communicate with web servers such as Nginx and Apache while addressing performance and concurrency concerns.

Python Programming Learning Circle
Python Programming Learning Circle
Python Programming Learning Circle
Understanding CGI, FastCGI, PHP‑FPM, WSGI, uWSGI and ASGI in Python Web Development

CGI

CGI (Common Gateway Interface) is a protocol that defines how external programs communicate with a web server, allowing the server to invoke programs written in languages such as VB, C, PHP, or Python to generate dynamic responses.

Origin

Early web servers only served static HTML files. As dynamic content became necessary, the CGI protocol was introduced to let the server pass request parameters to external programs and return the program's output as the HTTP response.

The server and CGI program exchange data via standard input/output streams and environment variables.

Working Principle

The web server decides how to pass data to the CGI program, typically using STDIN/STDOUT and a set of predefined environment variables (e.g., CONTENT_TYPE , CONTENT_LENGTH , QUERY_STRING , REQUEST_METHOD , etc.).

Variable

Description

CONTENT_TYPE

MIME type of the incoming data, usually

application/x-www-form-urlencoded

.

CONTENT_LENGTH

Number of bytes read from STDIN when the request method is POST.

HTTP_COOKIE

Contents of the client’s Cookie header.

HTTP_USER_AGENT

Information about the client’s browser.

PATH_INFO

Extra path information following the CGI script name.

QUERY_STRING

The part of the URL after the ‘?’ when the request method is GET.

REMOTE_ADDR

IP address of the client making the request.

REMOTE_HOST

Hostname of the client (if available).

REQUEST_METHOD

HTTP method used to invoke the script (GET or POST).

SCRIPT_FILENAME

Full filesystem path of the CGI script.

SCRIPT_NAME

Name of the CGI script.

SERVER_NAME

Hostname or IP address of the web server.

SERVER_SOFTWARE

Web server name and version (e.g., Apache/2.2.14).

Each request spawns a new CGI process, which exits after handling the request; this model does not scale well under high concurrency.

FastCGI

FastCGI extends CGI by keeping a pool of persistent processes (a master process and multiple workers) to avoid the overhead of forking a new process for every request, thereby improving performance and resource utilization.

php‑fpm

PHP‑FPM (FastCGI Process Manager) is the PHP implementation of the FastCGI protocol. It manages a pool of PHP worker processes, supports graceful reloads, uses Unix sockets for communication, provides detailed status and slow‑log output, and reduces memory consumption compared with traditional CGI.

WSGI / uwsgi / uWSGI

In Python web development, WSGI (Web Server Gateway Interface) is a specification that decouples web servers from web applications. Frameworks such as Flask, Django, and Bottle implement the WSGI application side, while servers like uWSGI or Gunicorn implement the server side.

WSGI

WSGI defines a callable that receives the request environment and returns an iterable response, enabling any WSGI‑compatible server to run any WSGI‑compatible framework.

uwsgi (protocol)

uwsgi is a binary protocol used by the uWSGI server to exchange data with other servers (e.g., Nginx). It is distinct from the WSGI specification.

uWSGI (server)

uWSGI is a high‑performance web server written in C that implements both the WSGI and uwsgi protocols, provides process management, monitoring, load balancing, and can act as a middleware between Nginx and Python applications.

Nginx + uWSGI

Typical deployment: Nginx acts as a reverse proxy handling static assets and forwarding dynamic requests to uWSGI, which then passes them to a Django or Flask application. uWSGI communicates with Nginx via the uwsgi protocol, while the application communicates with uWSGI via WSGI.

ASGI

ASGI (Asynchronous Server Gateway Interface) extends the idea of WSGI to support asynchronous protocols such as HTTP/2 and WebSocket, allowing Python applications to handle long‑living connections and real‑time communication.

Difference between WSGI and ASGI

WSGI is synchronous and only supports the request/response model of HTTP, whereas ASGI adds support for asynchronous communication, making it suitable for modern web features like WebSocket while remaining backward compatible with WSGI.

Summary

CGI is a basic protocol for invoking external programs from a web server.

FastCGI improves CGI by keeping persistent worker processes.

PHP‑FPM is the FastCGI implementation for PHP.

WSGI is a Python‑specific gateway interface; uwsgi is a binary protocol; uWSGI is a server that implements both.

ASGI adds asynchronous capabilities to the Python web stack.

PythonWeb DevelopmentCGIFastCGIWSGIuwsgiASGI
Python Programming Learning Circle
Written by

Python Programming Learning Circle

A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.