Windows XP Windows 7 Windows 2003 Windows Vista Windows 綜合教學 Linux 系統教學
Windows 10 Windows 8 Windows 2008 Windows NT Windows Server 電腦軟體教學
您现在的位置: Windows教程網 >> Windows教程 >  >> Linux 系統教學 >> Linux 教學

Nginx 啟用 Gzip 壓縮傳輸:核心參數配置與最佳實踐

編輯:Linux 教學

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-92-4(平衡 CPU 與壓縮比)
gzip_buffers壓縮緩衝區數量與大小4 16k
gzip_vary添加 Vary: Accept-Encoding 頭on

驗證壓縮生效

curl -I -H "Accept-Encoding: gzip" https://example.com/
# 響應頭包含 Content-Encoding: gzip 即成功
Copyright © Windows教程網 All Rights Reserved