Swoole-src: Cache Control and Compression for static resources

Created on 6 Jul 2020  路  4Comments  路  Source: swoole/swoole-src

Please answer these questions before submitting your issue. Thanks!

  1. What did you do? If possible, provide a simple script for reproducing the error.

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,
    ])
;
  1. What did you expect to see?

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?

  1. What did you see instead?

No compression or cache headers which result in much slower page load.

image

  1. What version of Swoole are you using (show your php --ri swoole)?
    [Preloader] Preloaded 1257 classes

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

  1. What is your machine environment used (show your uname -a & php -v & gcc -v) ?

PHP 7.4 official Docker image on MacOS.

question

Most helpful comment

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

All 4 comments

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!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

daslicht picture daslicht  路  4Comments

AndyChanCode picture AndyChanCode  路  3Comments

rovico picture rovico  路  4Comments

sagesan picture sagesan  路  4Comments

sshymko picture sshymko  路  3Comments