Cgi Mode
CGI Mode
Deploy agents as CGI scripts on traditional web servers like Apache or nginx. The SDK automatically detects CGI environments and handles requests appropriately.
CGI Overview
CGI (Common Gateway Interface) allows web servers to execute scripts and return their output as HTTP responses.
Benefits:
- Works with shared hosting
- Simple deployment - just upload files
- No separate process management
- Compatible with Apache, nginx
Drawbacks:
- New process per request (slower)
- No persistent connections
- Limited scalability
CGI Detection
The SDK detects CGI mode via the GATEWAY_INTERFACE environment variable:
Basic CGI Script
Make it executable:
CGI Request Flow

Apache Configuration
Enable CGI
Virtual Host Configuration
nginx Configuration
nginx doesn’t natively support CGI, but you can use FastCGI with fcgiwrap:
CGI Host Configuration
In CGI mode, the SDK needs to know the external hostname for generating URLs:
Or set environment variable:
Testing CGI Locally
Use swaig-test to simulate CGI environment:
Authentication in CGI Mode
The SDK checks basic auth in CGI mode:
If authentication fails, returns 401 with WWW-Authenticate header.
Directory Structure
Shared Hosting Deployment
For shared hosting where you can’t install system packages:
Install packages locally:
CGI Best Practices
Performance
- Keep imports minimal - each request starts fresh
- Consider FastCGI for better performance
- Cache what you can (but remember process dies)
Security
- Set proper file permissions (750 or 755)
- Don’t expose .py files directly if possible
- Use HTTPS always
- Set auth credentials as environment variables
Debugging
- Check web server error logs
- Verify shebang line (#!/usr/bin/env python3)
- Test script from command line first
- Ensure proper line endings (LF, not CRLF)
Common CGI Issues
Migration from CGI
When you outgrow CGI:
CGI → FastCGI
Keep same code, use fcgiwrap or gunicorn. Better performance, persistent processes.
CGI → Server Mode
Same code works - just run differently (python agent.py instead of CGI). Add systemd service, nginx reverse proxy.
CGI → Serverless
Same code works with minor changes. Add Lambda handler wrapper. Deploy to AWS/GCP/Azure.