nginx

nginx gzip, brotli (ngx_gzip, ngx_brotli) 설정

꺼비72 2024. 4. 29. 10:04
반응형

1. 원본에 Accept-Encoding에 따라 동적으로 압축해 컨텐츠를 설정 추가
(원본설정) gzip, br 압축을 동적으로 처리

    # gzip config
    gzip on;
    gzip_types application/vnd.apple.mpegurl text/plain text/css application/json application/x-javascript text/javascript;
    gzip_min_length 100;
    gzip_vary on;
    gzip_http_version 1.1;
    gzip_buffers      16 8k;
    gzip_comp_level   4;

    # brotli config
    brotli on;
    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;
    brotli_comp_level   6;
    brotli_static   on;


 

(원본 테스트)
# curl org/1.json -I
HTTP/1.1 200 OK
Server: nginx/1.25.4
Date: Sat, 27 Apr 2024 14:19:14 GMT
Content-Type: application/json
Content-Length: 4346
Last-Modified: Wed, 21 Feb 2024 06:27:24 GMT
Connection: keep-alive
Vary: Accept-Encoding
ETag: "65d597cc-10fa"
Cache-Control: max-age=120
Accept-Ranges: bytes"

# curl org/1.json -I -H "accept-encoding: gzip"
HTTP/1.1 200 OK
Server: nginx/1.25.4
Date: Sat, 27 Apr 2024 14:18:59 GMT
Content-Type: application/json
Last-Modified: Wed, 21 Feb 2024 06:27:24 GMT
Connection: keep-alive
Vary: Accept-Encoding
ETag: W/"65d597cc-10fa"
Cache-Control: max-age=120
Content-Encoding: gzip


# curl org/1.json -I -H "accept-encoding: br"
HTTP/1.1 200 OK
Server: nginx/1.25.4
Date: Sat, 27 Apr 2024 14:18:44 GMT
Content-Type: application/json
Last-Modified: Wed, 21 Feb 2024 06:27:24 GMT
Connection: keep-alive
Vary: Accept-Encoding
ETag: W/"65d597cc-10fa"
Cache-Control: max-age=120
Content-Encoding: br




(동작방식) https://www.rfc-editor.org/rfc/rfc9112#field.transfer-encoding
http 1.0의 경우 Content-Length 해더가 Connection이 종료됨에 따라 결정되므로 별도의 Contents-Length를 계산해서 보내지 않음
http 1.1의 경우 1개의 Connection에 멀티 전송이 가능하므로 Contents전송 시 원본에서 Content-Length 계산해서 보냄
하지만 동적 Compress (gzip, br 등)의 경우 압축된 Contents size를 알 수 없어 Content-Length는 해더를 먼저보냄
해더에  "Transfer-Encoding: chunked", "Content-Encoding: gzip",  "Vary: Accept-Encoding" 가 추가되고 
"Content-Length: 4346" 는 알수 없으므로 제거됨, ETag: W/"65d597cc-10fa" 는 이와 같이 W/ 로 변경됨

 

원본이 gzip, br일 경우 nginx proxy설정

(relay 테스트)
# curl relay/1.json -I
HTTP/1.1 200 OK
Server: nginx/1.25.4
Date: Sat, 27 Apr 2024 14:19:59 GMT
Content-Type: application/json
Content-Length: 4346
Connection: keep-alive
Last-Modified: Wed, 21 Feb 2024 06:27:24 GMT
Vary: Accept-Encoding
ETag: "65d597cc-10fa"
Cache-Control: max-age=120
X-CACHE-STATUS: MISS
Accept-Ranges: bytes

# curl relay/1.json -H "accept-encoding: gzip" -I
HTTP/1.1 200 OK
Server: nginx/1.25.4
Date: Sat, 27 Apr 2024 14:20:38 GMT
Content-Type: application/json
Connection: keep-alive
Last-Modified: Wed, 21 Feb 2024 06:27:24 GMT
Vary: Accept-Encoding
ETag: W/"65d597cc-10fa"
Cache-Control: max-age=120
Content-Encoding: gzip
X-CACHE-STATUS: MISS

# curl relay/1.json -H "accept-encoding: br" -I
HTTP/1.1 200 OK
Server: nginx/1.25.4
Date: Sat, 27 Apr 2024 14:22:01 GMT
Content-Type: application/json
Connection: keep-alive
Last-Modified: Wed, 21 Feb 2024 06:27:24 GMT
Vary: Accept-Encoding
ETag: W/"65d597cc-10fa"
Cache-Control: max-age=120
Content-Encoding: br
X-CACHE-STATUS: MISS


(동작방식) Client의 accept-encoding을 그대로 원본서버에 전달 제대로된 Contents-Encoding값을 받음
특이사항 : 동일한 Cache key설정이나 모두 MISS가 나타남 (proxy_cache_key $host$uri$is_args$args;)

다시 동일 요청을 하면 HIT가 이뤄짐 (Accept-Encoding을 cache key에 넣지 않아도 캐쉬 키가 달라짐)


아래와 같이 accept-encoding을 조금만 다르게 하면 다시 MISS가 발생함

 

# curl relay/1.json -H "accept-encoding: gzip br" -I
HTTP/1.1 200 OK
Server: nginx/1.25.4
Date: Sat, 27 Apr 2024 14:25:23 GMT
Content-Type: application/json
Connection: keep-alive
Last-Modified: Wed, 21 Feb 2024 06:27:24 GMT
Vary: Accept-Encoding
ETag: W/"65d597cc-10fa"
Cache-Control: max-age=120
Content-Encoding: br
X-CACHE-STATUS: MISS


즉, 원본이 압축을 지원할 경우 accept-encdoding에 따라 cache key가 달라지므로

원본 압축상황에서 reverse proxy cache에서는 특수할 설정이 필요함

'nginx' 카테고리의 다른 글

gunzip을 사용해 Cache key절약  (0) 2024.04.29
accept-encoding nomalization  (0) 2024.04.29
How to purge the Nginx cache  (0) 2023.06.19
nginx proxy hierarchy  (0) 2023.06.19
nginx upstream module - part 3 (dns lookup)  (0) 2023.06.19