Nginx配置Gzip

为什么要压缩

web服务器将组成网页的文件进行gzip压缩后再传输给浏览器,浏览器获取到压缩文件后进行解压并解析展示网页内容。原本的资源只在传输过程中进行压缩,既节约了带宽,同时也提升了页面的浏览速率。在实际工作中,网页压缩使用的比较多,这里就简单的记录下nginx服务器下Gzip的配置

参数配置

    #开启和关闭gzip模式
    gzip on|off;
    
    #gizp压缩起点,文件大于1k才进行压缩
    gzip_min_length 1k;
    
    # gzip 压缩级别,1-9,数字越大压缩的越好,也越占用CPU时间
    gzip_comp_level 1;
    
    # 进行压缩的文件类型。
    gzip_types text/plain application/javascript application/x-javascript text/css application/xml text/javascript ;
    
    #nginx对于静态文件的处理模块,开启后会寻找以.gz结尾的文件,直接返回,不会占用cpu进行压缩,如果找不到则不进行压缩
    gzip_static on|off
    
    # 是否在http header中添加Vary: Accept-Encoding,建议开启
    gzip_vary on;

    # 设置压缩所需要的缓冲区大小,以4k为单位,如果文件为7k则申请2*4k的缓冲区 
    gzip_buffers 2 4k;

    # 设置gzip压缩针对的HTTP协议版本
    gzip_http_version 1.1;

示例

http {
    gzip on;
    gzip_buffers 8 16k; ## 这个限制了nginx不能压缩大于128k的文件
    gzip_comp_level 2;
    gzip_disable "MSIE [1-6]\.(?!.*SV1)";
    gzip_min_length 512; ##单位byte
    gzip_http_version 1.0;
    gzip_proxied expired no-cache no-store private auth;
    gzip_types   text/plain application/x-javascript text/css application/xml;
    gzip_vary on;
    gzip_static  on;
    //......
}

外部链接 

https://segmentfault.com/a/1190000012694104

名称默认配置作用域官方说明中文解读模块gzipgzip off;http, server, location, if in locationEnables or disables gzipping of responses.设置是否开启对后端响应的gzip压缩,然后返回压缩内容给前端ngx_http_gzip_modulegzip_buffersgzip_buffers 32 4k或16 8k;http, server, locationSets the number and size of buffers used to compress a response. By default, the buffer size is equal to one memory page. This is either 4K or 8K, depending on a platform.设置用于压缩后端response的buffer的数量和每个的大小,默认每个buffer大小为一个内存页,根据平台不同可能是4k或8kngx_http_gzip_modulegzip_comp_levelgzip_comp_level 1;http, server, locationSets a gzip compression level of a response. Acceptable values are in the range from 1 to 9.指定gzip压缩的级别,默认为1,该值可设置的范围是1-9,1为最小化压缩(处理速度快),9为最大化压缩(处理速度慢),数字越大压缩的越好,也越占用CPU时间ngx_http_gzip_modulegzip_disable没有默认值http, server, locationDisables gzipping of responses for requests with User-Agent header fields matching any of the specified regular expressions.正则匹配User-Agent中的值,匹配上则不进行gzipngx_http_gzip_modulegzip_min_lengthgzip_min_length 20;http, server, locationSets the minimum length of a response that will be gzipped. The length is determined only from the Content-Length response header field.设定进行gzip压缩的阈值,当后端response的Content-Length大小小于该值则不进行gzip压缩ngx_http_gzip_modulegzip_http_versiongzip_http_version 1.1;http, server, locationSets the minimum HTTP version of a request required to compress a response.设定进行gzip压缩的最小http版本ngx_http_gzip_modulegzip_proxiedgzip_proxied off;http, server, locationEnables or disables gzipping of responses for proxied requests depending on the request and response. The fact that the request is proxied is determined by the presence of the "Via" request header field.根据request或响应的相关header的值来决定是否进行gzipngx_http_gzip_modulegzip_typesgzip_types text/html;http, server, locationEnables gzipping of responses for the specified MIME types in addition to "text/html". The special value "*" matches any MIME type (0.8.29). Responses with the "text/html" type are always compressed.指定哪些mime types启用gzip压缩,默认text/htmlngx_http_gzip_modulegzip_varygzip_vary off;http, server, locationEnables or disables inserting the "Vary: Accept-Encoding" response header field if the directives gzip, gzip_static, or gunzip are active.是否往response header里头写入Vary: Accept-Encodingngx_http_gzip_modulegzip_staticgzip_static off;http, server, locationEnables ("on") or disables ("off") checking the existence of precompressed files. The following directives are also taken into account: gzip_http_version, gzip_proxied, gzip_disable, and gzip_vary.开启之后,接到(静态文件)请求会到url相同的路径的文件系统去找扩展名为".gz"的文件,如果存在直接把它发送出去,如果不存在,则进行gzip压缩,再发送出去ngx_http_gzip_static_modu

转载于:https://www.cnblogs.com/luxiaojun/p/11579776.html

<think>好的,用户想了解如何在Nginx配置Gzip压缩。我之前回答过类似的问题,但需要根据提供的引用内容来组织答案。首先,我需要确认用户的需求是配置Gzip压缩,包括启用、优化和相关参数。 根据引用内容,引用[1]提到了启用Gzip的基本配置,包括gzip on和gzip_types,还有压缩级别gzip_comp_level。引用[2]提到了gunzip功能,虽然用户可能暂时需要,但可以作为补充信息。引用[3]详细说明了Gzip的工作原理和步骤,包括配置文件的路径和常用参数。 我需要将这些信息整合成一个结构清晰的回答。首先,应该分步骤说明如何配置,比如启用Gzip、设置压缩类型、调整压缩级别等。同时,需要提到验证配置和注意事项,比如要压缩已压缩的文件,以及压缩级别对CPU的影响。 还需要生成相关问题,可能涉及优化方法、参数调优、与CDN的配合等。确保回答中使用正确的LaTeX格式,比如变量用$...$,但根据用户提供的示例,似乎数学公式的例子可能需要,但需要检查是否有必要的地方使用。过当前问题主要涉及配置指令,可能数学公式较少,但要注意用户提到的系统级指令中的格式要求。 需要检查是否有遗漏的重要参数,比如gzip_min_length、gzip_vary等,引用中提到的内容是否需要包含。引用[3]提到gzip_types需要明确指定,而默认可能只压缩text/html,所以需要强调添加其他MIME类型。 最后,确保回答中使用中文,代码块正确,引用标识正确添加。例如,在提到压缩级别时添加[^1],在解压功能处添加[^2],在启用步骤引用[^3]。 总结步骤: 1. 启用Gzipgzip on; 2. 配置压缩类型:gzip_types 3. 设置压缩级别:gzip_comp_level 4. 其他参数:gzip_min_length、gzip_vary、gzip_proxied 5. 验证和重载配置 6. 注意事项和优化建议 相关问题可以包括如何验证是否生效、优化压缩效果、解决CPU负载问题等。</think>### 如何在Nginx配置Gzip压缩 #### 1. 启用Gzip压缩模块 在Nginx配置文件中(通常为`nginx.conf`或站点配置文件)添加以下基础配置: ```nginx http { gzip on; # 启用Gzip压缩[^1][^3] gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript; # 指定压缩的文件类型[^3] } ``` #### 2. 配置压缩参数 根据需求调整压缩级别和其他优化参数: ```nginx http { gzip_comp_level 6; # 压缩级别(1-9,数值越大压缩率越高但CPU消耗越大)[^1] gzip_min_length 1024; # 仅压缩大于1KB的文件 gzip_vary on; # 添加Vary头,兼容支持压缩的客户端[^3] gzip_proxied any; # 对代理请求也启用压缩 } ``` #### 3. 验证配置并重载服务 ```bash nginx -t # 检查配置语法 nginx -s reload # 重载配置使生效 ``` #### 4. 验证压缩效果 通过浏览器开发者工具查看响应头,若包含`Content-Encoding: gzip`则表示配置成功。 #### 注意事项 - 避免重复压缩:图片、视频等已压缩文件无需再次压缩[^3] - 压缩级别推荐:通常设为5-6,平衡压缩率与性能[^1] - 动态内容压缩:需确保后端服务未压缩响应,或启用`gunzip on`解压功能[^2] ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值