nginx gzip

warning: htmlspecialchars(): Invalid multibyte sequence in argument in /home/vhosts/datahunter.org/web/includes/bootstrap.inc on line 867.

最後更新: 2022-12-29

目錄

  • GZIP Settings
  • gzip_vary
  • gzip 對 Proxy 的影響
  • Labs (curl get gzip encode file)
  • gzip_static

 


GZIP Settings

 

# Enable gzip compression at the http block level
gzip                on;

# "User-Agent" header fields matching any of the specified regular expressions.
gzip_disable        "msie6";

# 1 (Default) -> 9
gzip_comp_level     6;

# file size > 4 kbyte 才 gzip (bootstrap.min=34kb, gzip 後 23kb)
# determined only from the “Content-Length” response header field.
gzip_min_length     4k;

# Default 只 gzip "text/html", 如果想 gzip 其他, 那要加上
# 對應 mime.types
gzip_types          text/plain
                    text/css
                    text/javascript
                    application/javascript
                    application/x-javascript
                    application/x-font-woff
                    text/xml
                    application/xml;

# 設定在 proxy 的情況下會否 gzip 資料
# off: disables compression for all proxied requests
# no_last_modified: enables compression if a response header does not include the “Last-Modified” field; 
# 其他 options
#   Cache-Control field: expired, no-cache, no-store, private, no_etag
#   Authorization field: auth
# i.e. "gzip_proxied    expired no-cache no-store private auth;"
gzip_proxied        any;

 


gzip_vary

 

 * 同一個 URL 可以提供多份不同的檔案

會影響版本的 Header

  • Accept
  • Accept-Language
  • Accept-Charset
  • Accept-Encoding

i.e.

Vary: Accept-Encoding

"告訴代理服務器緩存兩種版本的資源:壓縮和非壓縮"

Client ---> Server

Accept    text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Encoding    gzip, deflate
Accept-Language    en-GB,en;q=0.5

Server ---> Client

Content-Encoding    gzip
Content-Type    text/html; charset=UTF-8

# Setting

# Default: gzip_vary off;
gzip_vary on;

 * 它會在 Response Header 加上 "Vary Accept-Encoding"

 


gzip 對 Proxy 的影響

 

 * gzip in the Accept-Encoding header, you set the Content-Encoding: gzip

# vary 解決了 proxy server cache 了一個檔案的多個版本 (raw, gzip)

 * 請求只有完全匹配緩存的「Vary」信息,緩存才被使用

 * Note that the Vary header is present in the response regardless of whether or not compression is used.

First, a request comes in for an object, without an Accept-Encoding header.
The object is not in the cache, so we request it from the origin, which returns it with the Vary header.

When Fastly stores the object in the cache, the "Vary header" is noted,
and the values of the relevant headers from the request are stored as well.

So now there's an object in the cache that has a little flag on it that says
"only to be used for requests that have no Accept-Encoding in the request."

If the Vary header hadn't been there in the first response,
we wouldn't have known that we couldn't use the cached object for the second request.

 


Labs (curl get gzip encode file)

 

curl -I https://datahunter.org/share/test.txt

content-type: text/plain
content-length: 130
...
accept-ranges: bytes

curl -I -H 'Accept-Encoding: gzip' https://datahunter.org/share/test.txt

content-type: text/plain
...                       # 沒有 content-length
content-encoding: gzip

when using dynamic Gzip then the Content-Length header is not sent, as the Transfer-Encoding is chunked

wget gzip http connection

# --compression=type

wget --compression=auto https://datahunter.org/417.html

 


gzip_static

 

module: ngx_http_gzip_static_module

It sending precompressed files with the “.gz” filename extension instead of regular files.

* This module is not built by default (--with-http_gzip_static_module)

# Context: http, server, location

# on | off | alway

# alway: gzipped file is used in all cases, without checking if the client supports it.

gzip_static  on;

 * 當以 'Accept-Encoding: gzip' 訪問 test.txt 時, 即使不存在, 也會提供 test.txt.gz

curl -I # 404

curl -I -H"Accept-Encoding: gzip" https://datahunter.org/417.html    # 200

應用

  error_page 417              /417.html;
  location = /417.html {
    root  /var/www/html;
    modsecurity     off;
    gzip_static      on;
    log_not_found   off;
    access_log      off;
  }