sajad torkamani

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 via stdin.
  • 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

FeatureCGIFastCGI
Process ModelNew process per requestReuses long-running processes
PerformanceSlowFast and efficient
Resource UsageHighLow
Still used today?RarelyYes (PHP-FPM, uWSGI, etc.)
Tagged: Misc