Please answer these questions before submitting your issue. Thanks!
Setup Swoole server with document_root setting pointing to my public resources folder. My server has Gzip and Brotli support working for dynamic content.
$http = new Server("0.0.0.0", 80);
$http
->set([
'open_http2_protocol' => true,
'document_root' => __DIR__ . '/../public',
'enable_static_handler' => true,
'timeout' => 7,
'http_compression' => true,
'http_compression_level' => 6,
])
;
Any way to control cache and compression policy for my static resources folder. Is there any built-in way to manage this, or should I handle all static routes in my own source code? Does using document_root has any performance advantage over doing this manually?
No compression or cache headers which result in much slower page load.

php --ri swoole)?swoole
Swoole => enabled
Author => Swoole Team team@swoole.com
Version => 4.5.2
Built => Jul 4 2020 15:12:50
coroutine => enabled
epoll => enabled
eventfd => enabled
signalfd => enabled
cpu_affinity => enabled
spinlock => enabled
rwlock => enabled
sockets => enabled
http2 => enabled
zlib => 1.2.11
brotli => E16777223/D16777223
mutex_timedlock => enabled
pthread_barrier => enabled
futex => enabled
async_redis => enabled
Directive => Local Value => Master Value
swoole.enable_coroutine => On => On
swoole.enable_library => On => On
swoole.enable_preemptive_scheduler => Off => Off
swoole.display_errors => On => On
swoole.use_shortname => On => On
swoole.unixsock_buffer_size => 8388608 => 8388608
uname -a & php -v & gcc -v) ?PHP 7.4 official Docker image on MacOS.
static_handler use sendifle, it is zero-copy operation, but we can not compress it, the OS will send the file directly
If you want to compress it, you have to load the content into your memory
@twose got it, thanks for clarifying this issue. This just lead me to another question on the same matter. Is there a way to disable compression on specific requests?
For example, my API uses WEBP to compress images, and having BR/GZIP compression on top of that actually makes the response size bigger probably slower because of the wasted compression time. Tried to manipulate the request header like this: $request->header['accept'] = ''; with no luck.
@eldadfux You can use the Response::write() method, The write() method will disable compression.
$http = new Swoole\Http\Server("127.0.0.1", 9501);
$http->set(['http_compression' => true]);
$http->on('request', function ($request, $response) {
// compressed
if ($request->server['request_uri'] == '/test.html') {
$response->end(file_get_contents("test.html"));
}
// non-compressed
elseif ($request->server['request_uri'] == '/test.webp') {
$response->write(file_get_contents("test.webp"));
}
});
$http->start();
Thanks!
Most helpful comment
static_handler use
sendifle, it is zero-copy operation, but we can not compress it, the OS will send the file directlyIf you want to compress it, you have to load the content into your memory