nginx

ngx_brotli

꺼비72 2024. 5. 6. 19:49
반응형

https://github.com/google/ngx_brotli

 

GitHub - google/ngx_brotli: NGINX module for Brotli compression

NGINX module for Brotli compression. Contribute to google/ngx_brotli development by creating an account on GitHub.

github.com

 

# git clone --recurse-submodules -j8 https://github.com/google/ngx_brotli
# cd ngx_brotli/deps/brotli
# mkdir out && cd out

# cmake -DCMAKE_BUILD_TYPE=Release -DBUILD_SHARED_LIBS=OFF -DCMAKE_C_FLAGS="-Ofast -m64 -march=native -mtune=native -flto -funroll-loops -ffunction-sections -fdata-sections -Wl,--gc-sections" -DCMAKE_CXX_FLAGS="-Ofast -m64 -march=native -mtune=native -flto -funroll-loops -ffunction-sections -fdata-sections -Wl,--gc-sections" -DCMAKE_INSTALL_PREFIX=/usr/local ..
-- Build type is 'Release'
-- Compiler is not EMSCRIPTEN
-- Configuring done (0.1s)
-- Generating done (0.2s)
-- Build files have been written to: /root/ngx_brotli/deps/brotli/out


# cmake --build . --config Release --target install
[ 19%] Built target brotlicommon
[ 33%] Built target brotlidec
[ 94%] Built target brotlienc
[100%] Built target brotli
Install the project...
-- Install configuration: "Release"
-- Installing: /usr/local/bin/brotli
-- Installing: /usr/local/lib64/libbrotlienc.a
-- Installing: /usr/local/lib64/libbrotlidec.a
-- Installing: /usr/local/lib64/libbrotlicommon.a
-- Installing: /usr/local/include/brotli
-- Installing: /usr/local/include/brotli/decode.h
-- Installing: /usr/local/include/brotli/encode.h
-- Installing: /usr/local/include/brotli/port.h
-- Installing: /usr/local/include/brotli/shared_dictionary.h
-- Installing: /usr/local/include/brotli/types.h
-- Installing: /usr/local/lib64/pkgconfig/libbrotlicommon.pc
-- Installing: /usr/local/lib64/pkgconfig/libbrotlidec.pc
-- Installing: /usr/local/lib64/pkgconfig/libbrotlienc.pc

 

 

사전압축할 파일을 모두 brotli 최대 압축 level 11로 사전압축한 파일을 생성한다.

 

# find . \( -iname '*.json' -o -iname '*.js' \) -exec bash -c 'brotli --best "$0"' {} \;

 

 

org.conf에 brotli_static on 설정을 한다.

    # brotli config
    brotli on;
    brotli_static on;
    brotli_comp_level  6;
    brotli_types application/vnd.apple.mpegurl text/plain text/css application/json application/x-javascript text/javascript;
    brotli_min_length 100;
    brotli_buffers   16 8k

 

Accept-encoding : br로 요청 시 Content-Encoding: br로 응답이 오고, Content-Length: 64로 나타남

# curl org/1.json -H "accept-encoding: br" -I
HTTP/1.1 200 OK
Server: nginx
Date: Mon, 06 May 2024 10:53:06 GMT
Content-Type: application/json
Content-Length: 64
Last-Modified: Mon, 06 May 2024 04:25:31 GMT
Connection: keep-alive
Vary: Accept-Encoding
ETag: "66385bbb-40"
Content-Encoding: br
Cache-Control: max-age=120
X-Test: aaa

 

On-the-fley 압축과 다른점 Content-Length가 없고, ETag는 plain text값에 "W/" 추가됨, Transfer-encoding : chunked로 응답

# curl org/1.json -H "accept-encoding: br" -I
HTTP/1.1 200 OK
Server: nginx
Date: Mon, 06 May 2024 10:56:33 GMT
Content-Type: application/json
Last-Modified: Mon, 06 May 2024 04:25:31 GMT
Connection: keep-alive
Vary: Accept-Encoding
ETag: W/"66385bbb-1100"
Cache-Control: max-age=120
X-Test: aaa
Content-Encoding: br

# curl org/1.json -H "accept-encoding: br" -v
* processing: org/1.json
*   Trying 61.255.239.55:80...
* Connected to org (61.255.239.55) port 80
> GET /1.json HTTP/1.1
> Host: org
> User-Agent: curl/8.2.1
> Accept: */*
> accept-encoding: br
>
< HTTP/1.1 200 OK
< Server: nginx
< Date: Mon, 06 May 2024 10:56:39 GMT
< Content-Type: application/json
< Last-Modified: Mon, 06 May 2024 04:25:31 GMT
< Transfer-Encoding: chunked
< Connection: keep-alive
< Vary: Accept-Encoding
< ETag: W/"66385bbb-1100"
< Cache-Control: max-age=120
< X-Test: aaa
< Content-Encoding: br