CGI protocol
18 May 2025 (Updated 18 May 2025)
What is the Common Gateway Interface (CGI)?
The Common Gateway Interface (CGI) is an early protocol (superseded by FastCGI) that defines how web servers like Nginx or Apache should interface with external scripts or programs (PHP, Python, Perl, etc) to generate dynamic web content.
CGI protocol in action
- A user requests a web page at
https://example.com/user?name=Sajad.php
- Nginx (the web server) starts a new process and executes PHP script:
/usr/bin/php user.php
, passing it all the request data (e.g., path =/user
and?name=Sajad
) as environment variables viastdin
. - The PHP script executes and returns an HTTP response as a string (including the HTTP headers and response body) to
stdout
. - Nginx takes the PHP script’s rsponse from
stdout
and sends it back to the user as an HTTP response.
CGI vs FastCGI
Feature | CGI | FastCGI |
---|---|---|
Process Model | New process per request | Reuses long-running processes |
Performance | Slow | Fast and efficient |
Resource Usage | High | Low |
Still used today? | Rarely | Yes (PHP-FPM, uWSGI, etc.) |
Tagged:
Misc