google / ngx_brotli

NGINX module for Brotli compression
BSD 2-Clause "Simplified" License
2.1k stars 216 forks source link

Problem open_file_cache directive and brotli_static on #105

Open kvakanet opened 4 years ago

kvakanet commented 4 years ago

Hello!

  1. Created precompressed files .br

  2. Set in nginx

    brotli_static on;
    open_file_cache max=2048 inactive=20s;
  3. Sent request: curl https://example.ru/bitrix/js/ui/bootstrap4/css/bootstrap.min.css -I -A curl-Bortli -H 'Accept-Encoding: br' and got response

    server: nginx
    date: Fri, 24 Jul 2020 06:16:29 GMT
    content-type: text/css
    content-length: 140936
    last-modified: Wed, 22 Jul 2020 12:07:04 GMT
    etag: "5f182be8-22688"
    expires: Fri, 31 Jul 2020 06:16:29 GMT
    cache-control: max-age=604800
    accept-ranges: bytes

    but if you check response several times:

    server: nginx
    date: Fri, 24 Jul 2020 06:19:37 GMT
    content-type: text/css
    content-length: 15812
    last-modified: Wed, 22 Jul 2020 12:07:04 GMT
    etag: "5f182be8-3dc4"
    content-encoding: br
    expires: Fri, 31 Jul 2020 06:19:37 GMT
    cache-control: max-age=604800

    and so with a probability of 50%

  4. If enable in nginx gzip_static on and create precompressed files .gz and send request curl https://example.ru/bitrix/js/ui/bootstrap4/css/bootstrap.min.css -I -A curl-Gzip -H 'Accept-Encoding: gzip'

  5. Then get response

    server: nginx
    date: Fri, 24 Jul 2020 06:22:53 GMT
    content-type: text/css
    content-length: 20942
    last-modified: Fri, 24 Jul 2020 03:17:35 GMT
    etag: "5f1a52cf-51ce"
    content-encoding: gzip
    expires: Fri, 31 Jul 2020 06:22:53 GMT
    cache-control: max-age=604800

    100% every time.

  6. If remove from nginx open_file_cache max=2048 inactive=20s; And send request curl https://example.ru/bitrix/js/ui/bootstrap4/css/bootstrap.min.css -I -A curl-Bortli -H 'Accept-Encoding: br' Then Response:

    HTTP/2 200 
    server: nginx
    date: Fri, 24 Jul 2020 06:28:43 GMT
    content-type: text/css
    content-length: 15812
    last-modified: Wed, 22 Jul 2020 12:07:04 GMT
    etag: "5f182be8-3dc4"
    content-encoding: br
    expires: Fri, 31 Jul 2020 06:28:43 GMT
    cache-control: max-age=604800

    100% every time

  7. Ok. I created patch

    
    diff -ruN orig/static/ngx_http_brotli_static_module.c my/static/ngx_http_brotli_static_module.c                                                                                                                    
    --- orig/static/ngx_http_brotli_static_module.c 2020-04-23 13:55:31.000000000 +0300                                                                                                                                
    +++ my/static/ngx_http_brotli_static_module.c   2020-07-23 18:37:00.978000000 +0300                                                                                                                                
    @@ -178,7 +178,9 @@                                                                                                                                                                                                
    if (last == NULL) return NGX_HTTP_INTERNAL_SERVER_ERROR;                                                                                                                                                        
    /* +1 for reinstating the terminating 0. */                                                                                                                                                                     
    ngx_cpystrn(last, kSuffix, kSuffixLen + 1);                                                                                                                                                                     
    -  path.len += kSuffixLen;                                                                                                                                                                                         
    +  /* Kuka Patch for work with open_file_cache like gzip_static */                                                                                                                                               
    +  path.len = last - path.data;                                                                                                                                                                                    
    +                                                                                                                                                                                                                  
    
    log = req->connection->log;                                                                                                                                                                                     
    ngx_log_debug1(NGX_LOG_DEBUG_HTTP, log, 0, "http filename: \"%s\"",                                                                                                                                             
compile dynamic module and check again with settings nginx `open_file_cache max=2048 inactive=20s;`
Sent request:
`curl https://example.ru/bitrix/js/ui/bootstrap4/css/bootstrap.min.css?1595419624140936 -I -A curl-Bortli -H 'Accept-Encoding: br'`
Then response:

HTTP/2 200 server: nginx date: Fri, 24 Jul 2020 06:34:50 GMT content-type: text/css content-length: 15812 last-modified: Wed, 22 Jul 2020 12:07:04 GMT etag: "5f182be8-3dc4" content-encoding: br expires: Fri, 31 Jul 2020 06:34:50 GMT cache-control: max-age=604800


100% every time like gzip_static
8. But there is the problem with my patch. If I add `open_file_cache_errors on;` setting to nginx and request html page with many resources
then sometimes I got 404 error for resources (.png).
If I set `open_file_cache_errors off;` the everything works fine.