51 Nginx的Gzip模块配置指令(二)
51 Nginx的Gzip模块配置指令(二)
51.1 gzip_comp_level
gzip_comp_level:用于设置Gzip压缩程度,级别从1~9,1表示压缩程度最低,效率最高,9表示压缩程度最高,效率最低、最费时间
| 语法 | gzip_comp_level level; |
| 默认值 | gzip_comp_level 1; |
| 位置 | http 、server 、location |
[root@nginx-100 /usr/local/nginx/conf]# cat nginx.conf user www; worker_processes 2; error_log logs/error.log; pid logs/nginx.pid; #daemon on; # 默认on events { accept_mutex on; multi_accept on; worker_connections 1024; use epoll; } http { include mime.types; default_type application/octet-stream; sendfile on; tcp_nopush on; tcp_nodelay on; keepalive_timeout 65; gzip on; gzip_types application/javascript; #gzip_comp_level 1; server { listen 80; server_name localhost; location / { root html; index index.html index.htm; } error_page 500 502 503 504 404 /50x.html; location = /50x.html { root html; } } } [root@nginx-100 /usr/local/nginx/conf]# nginx -t && nginx -s reload nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
浏览器访问:http://10.0.0.100/jquery.js,默认 1 级,31.4KB

[root@nginx-100 /usr/local/nginx/conf]# cat nginx.conf user www; worker_processes 2; error_log logs/error.log; pid logs/nginx.pid; #daemon on; # 默认on events { accept_mutex on; multi_accept on; worker_connections 1024; use epoll; } http { include mime.types; default_type application/octet-stream; sendfile on; tcp_nopush on; tcp_nodelay on; keepalive_timeout 65; gzip on; gzip_types application/javascript; gzip_comp_level 6; server { listen 80; server_name localhost; location / { root html; index index.html index.htm; } error_page 500 502 503 504 404 /50x.html; location = /50x.html { root html; } } } [root@nginx-100 /usr/local/nginx/conf]# nginx -t && nginx -s reload nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
浏览器访问:http://10.0.0.100/jquery.js,打开 6 级,7.9KB

[root@nginx-100 /usr/local/nginx/conf]# cat nginx.conf user www; worker_processes 2; error_log logs/error.log; pid logs/nginx.pid; #daemon on; # 默认on events { accept_mutex on; multi_accept on; worker_connections 1024; use epoll; } http { include mime.types; default_type application/octet-stream; sendfile on; tcp_nopush on; tcp_nodelay on; keepalive_timeout 65; gzip on; gzip_types application/javascript; gzip_comp_level 9; server { listen 80; server_name localhost; location / { root html; index index.html index.htm; } error_page 500 502 503 504 404 /50x.html; location = /50x.html { root html; } } } [root@nginx-100 /usr/local/nginx/conf]# nginx -t && nginx -s reload nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
浏览器访问:http://10.0.0.100/jquery.js,打开 9 级,7.5KB

注意:gzip_comp_level 压缩等级不是越高越好,不仅随着压缩级别提升浪费CPU资源,并且效率也在下降 级别:6 -> 9,存储:7.9KB -> 7.5KB,建议使用中间等级,例如:6
51.2 gzip_vary
gzip_vary:用于设置使用Gzip进行压缩时,发送是否携带"Vary:Accept-Encoding"头域的响应头部。用于告诉接收方,发送的数据经过Gzip压缩处理
| 语法 | gzip_vary on | off; |
| 默认值 | gzip_vary off; |
| 位置 | http 、server 、 location |
[root@nginx-100 /usr/local/nginx/conf]# cat nginx.conf user www; worker_processes 2; error_log logs/error.log; pid logs/nginx.pid; #daemon on; # 默认on events { accept_mutex on; multi_accept on; worker_connections 1024; use epoll; } http { include mime.types; default_type application/octet-stream; sendfile on; tcp_nopush on; tcp_nodelay on; keepalive_timeout 65; gzip on; gzip_types application/javascript; gzip_comp_level 6; gzip_vary on; server { listen 80; server_name localhost; location / { root html; index index.html index.htm; } error_page 500 502 503 504 404 /50x.html; location = /50x.html { root html; } } } [root@nginx-100 /usr/local/nginx/conf]# nginx -t && nginx -s reload nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
浏览器访问:http://10.0.0.100/jquery.js

HTTP/1.1 200 OK Server: nginx/1.16.1 Date: Fri, 15 May 2026 06:28:27 GMT Content-Type: application/javascript Last-Modified: Thu, 14 May 2026 08:55:52 GMT Transfer-Encoding: chunked Connection: keep-alive Vary: Accept-Encoding ETag: W/"6a058e18-3cc6c" Content-Encoding: gzip
对比,未添加此参数
HTTP/1.1 200 OK Server: nginx/1.16.1 Date: Fri, 15 May 2026 06:02:21 GMT Content-Type: application/javascript Last-Modified: Thu, 14 May 2026 08:55:52 GMT Transfer-Encoding: chunked Connection: keep-alive ETag: W/"6a058e18-3cc6c" Content-Encoding: gzip
51.3 gzip_buffer
gzip_buffer:处理用于请求压缩的缓冲区数量和大小,相乘即为缓冲区总大小,例:32 x 4k
| 语法 | gzip_buffer number size; |
| 默认值 | gzip_buffer 32 4k | 16 8k; |
| 位置 | http 、 server 、location |
number:指定 nginx 服务器向系统申请缓冲空间个数
size:每个缓冲空间的大小
gzip_buffer 影响压缩速度、压缩率、占用内存有关,此项设定一般和服务器操作系统有关
———————————————————————————————————————————————————————————————————————————
无敌小马爱学习
浙公网安备 33010602011771号