There is a new PHP extension I've written to work with Varnish. The extension allows interaction from within a PHP script with a running Varnish instance. For now there is a basic useful functionality such as ban URLs or set/get Varnish configuration. There will be more with the time. For now I would give a couple of examples for the extension usage, as there is not really any docs for the extension here.
PHP:
<?php
/*
* VarnishAdmin expects an array with configuration options
* host - ip4 address or ip6 address where varnish is running
* port - port varnish is running on
* secret - varnish secret, normally located in the /etc/varnish/secret or something like that
* timeout - read timeout in milliseconds
*/
$args = array(
"host" => "::1",
"port" => 6082,
"secret" => "5174826b-8595-4958-aa7a-0609632ad7ca",
"timeout" => 300,
);
$va = new VarnishAdmin($args);
/* connect to the varnish instance given in the configuration options */
try {
if(!$va->connect()) {
throw new VarnishException("Connection failed\n");
}
} catch (VarnishException $e) {
echo $e->getMessage();
exit(3);
}
/* authenticate on the varnish instance using the secret configuration option */
try {
if(!$va->auth()) {
throw new VarnishException("Auth failed\n");
}
} catch (VarnishException $e) {
echo $e->getMessage();
exit(3);
}
From now on we can start to do things. The first, lets read the Varnish configuration:
Lets check, either varnish worker is actually running, and start it, if it isn't
PHP:
if(!$va->isRunning()) {
$va->start();
}
Go further and read the varnish params:
PHP:
Which outputs:
PHP:
array (
'acceptor_sleep_decay' => '0.900000',
'acceptor_sleep_incr' => '0.001000',
'acceptor_sleep_max' => '0.050000',
'auto_restart' => true,
'ban_dups' => true,
'ban_lurker_sleep' => '0.010000',
'between_bytes_timeout' => '60.000000',
'cli_buffer' => 8192,
'cli_timeout' => 10,
'clock_skew' => 10,
'connect_timeout' => '0.700000',
'critbit_cooloff' => '180.000000',
'default_grace' => '10.000000',
'default_keep' => '0.000000',
'default_ttl' => '120.000000',
'diag_bitmap' => 0,
'esi_syntax' => 0,
'expiry_sleep' => '1.000000',
'fetch_chunksize' => 128,
'fetch_maxchunksize' => 2,
......
)
As next, try to set some params
PHP:
echo $va->setParam("thread_pools", 42);
which would normally output 200, or VARNISH_STATUS_OK (see extension constants). Doublecheck the set param using varnishadm.
Also you can pugre some URLs from the varnish cache
PHP:
echo $va->banUrl("/hello");
Which should also return an OK code on success, or some other, depending on the situation. Note, that for now no quotes should be used in the VCL expressions, as the parsing of the VCL is very primitive now.
That's it, now you can try the Varnish PHP binding, using this short excursus and extension info. Get it from the PECL. The documentation will follow anyway,
Cheers