Using Swoole Coroutine FastCGI Client to Call PHP‑FPM and Proxy WordPress
This article introduces the FastCGI protocol, explains Swoole's task processes, demonstrates how to use the coroutine FastCGI client to invoke PHP‑FPM scripts and concurrently handle blocking calls, and shows a one‑click WordPress proxy implementation with performance benchmarks.
What is FastCGI? FastCGI (Fast Common Gateway Interface) is a binary protocol that lets web servers such as Nginx, Apache or IIS forward HTTP requests to a PHP‑FPM process, allowing the PHP application to focus on business logic while the server handles protocol parsing efficiently.
Swoole Task Processes are dedicated worker processes used in Swoole's asynchronous/ coroutine servers to execute synchronous, blocking code without blocking the main workers. They are invoked via $server->task and enable a hybrid asynchronous‑synchronous architecture.
Using the Coroutine FastCGI Client – after starting a PHP‑FPM instance, you can create a simple PHP script (e.g., /tmp/greeter.php ) and call it from Swoole with:
php -n -dextension=swoole -r "Co\run(function() { echo Co\FastCGI\Client::call('127.0.0.1:9000', '/tmp/greeter.php', ['who' => 'Swoole']); });"The call is non‑blocking; multiple coroutines can invoke different PHP‑FPM scripts concurrently, reducing total latency to the maximum of individual request times.
For a blocking example, a blocking.php script that sleeps for one second can be invoked concurrently with the following coroutine code:
use Swoole\Coroutine; use Swoole\Coroutine\FastCGI\Client; use Swoole\FastCGI\HttpRequest;
$s = microtime(true);
Coroutine\run(function () {
for ($n = 0; $n < 2; $n++) {
Co::create(function () use ($n) {
try {
$client = new Client('127.0.0.1', 9000);
$request = (new HttpRequest())
->withScriptFilename('/path/to/blocking.php')
->withMethod('POST')
->withBody(['id' => $n]);
$response = $client->execute($request);
echo "Result: {$response->getBody()}\n";
} catch (Client\Exception $e) {
echo "Error: {$e->getMessage()}\n";
}
});
}
});
$s = microtime(true) - $s;
echo 'use ' . $s . ' s' . "\n";The output shows both results returned after roughly one second, confirming true non‑blocking behavior, limited only by the number of PHP‑FPM workers.
One‑Click WordPress Proxy – the coroutine FastCGI client also provides a proxy that converts incoming Swoole\Http\Request objects into FastCGI requests and forwards FastCGI responses back as HTTP responses. A minimal proxy server looks like:
declare(strict_types=1);
use Swoole\Constant;
use Swoole\Coroutine\FastCGI\Proxy;
use Swoole\Http\Request;
use Swoole\Http\Response;
use Swoole\Http\Server;
$documentRoot = '/path/to/wordpress';
$server = new Server('0.0.0.0', 80, SWOOLE_BASE);
$server->set([
Constant::OPTION_WORKER_NUM => swoole_cpu_num() * 2,
Constant::OPTION_HTTP_PARSE_COOKIE => false,
Constant::OPTION_HTTP_PARSE_POST => false,
Constant::OPTION_DOCUMENT_ROOT => $documentRoot,
Constant::OPTION_ENABLE_STATIC_HANDLER => true,
Constant::OPTION_STATIC_HANDLER_LOCATIONS => ['/'],
]);
$proxy = new Proxy('127.0.0.1:9000', $documentRoot);
$server->on('request', function (Request $request, Response $response) use ($proxy) {
$proxy->pass($request, $response);
});
$server->start();The core of the proxy is a single line: (new Proxy('127.0.0.1:9000', $documentRoot))->pass($request, $response); , enabling WordPress to run behind a high‑performance coroutine server.
Behind the Client – the coroutine FastCGI client is open‑source (see https://github.com/swoole/library) and written entirely in PHP, leveraging coroutine sockets for low‑overhead binary FastCGI parsing. It demonstrates that PHP‑level components can achieve performance comparable to Nginx when the I/O model is non‑blocking.
Other Swoole components such as WaitGroup, connection pools, and coroutine servers share the same design philosophy, providing memory‑safe, developer‑friendly APIs without extra dependencies.
Overall, the coroutine FastCGI client offers a powerful bridge between modern asynchronous PHP servers and existing PHP‑FPM applications, allowing seamless migration, performance gains, and easy integration of legacy code.
Xueersi Online School Tech Team
The Xueersi Online School Tech Team, dedicated to innovating and promoting internet education technology.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.