Gzip 由 ngx_http_gzip_module 提供,實時壓縮響應體,減少帶寬、提升傳輸速度。
gzip on;
gzip_min_length 1000; # 最小壓縮長度 1KB,小於此值不壓縮(避免越壓越大)
gzip_proxied expired no-cache no-store private auth; # 反向代理場景啟用條件
gzip_types text/plain application/x-javascript text/css text/html application/xml; # 壓縮 MIME 類型
| 指令 | 說明 | 建議值 |
|---|---|---|
gzip on|off | 啟用/禁用模塊 | on |
gzip_min_length | 最小壓縮字節數(從 Content-Length 獲取) | 1024 (1KB) |
gzip_proxied | 反向代理時對後端響應啟用條件 | expired no-cache no-store private auth |
gzip_types | 壓縮 MIME 類型(text/html 默認始終壓縮) | 文本類:text/plain text/css application/json application/javascript text/xml application/xml |
gzip_comp_level | 壓縮等級 1-9 | 2-4(平衡 CPU 與壓縮比) |
gzip_buffers | 壓縮緩衝區數量與大小 | 4 16k |
gzip_vary | 添加 Vary: Accept-Encoding 頭 | on |
curl -I -H "Accept-Encoding: gzip" https://example.com/
# 響應頭包含 Content-Encoding: gzip 即成功