Nginx 笔记与总结(13)Nginx 的 gzip 压缩
使用 FireFox(40.0)访问博客园(http://www.cnblogs.com/),观察 http 头信息
请求头信息:

Accept-Encoding gzip, deflate 表示浏览器接受的压缩方式有 gzip 和 deflate
响应头信息:

Content-Encoding gzip 表示服务器返回内容的压缩方式是 gzip
注意:图片/mp3 这样的二进制文件不必压缩,因为压缩比较小(而且压缩需要消耗 CPU 资源)
在 nginx 下使用 gzip
nginx gzip 相关语法见:http://nginx.org/en/docs/http/ngx_http_gzip_module.html
gzip 配置的常见参数:
| gzip on|off | 是否开启 gzip |
| gzip_buffers 32 4k|16 8k | 缓冲(压缩在内存中缓冲几块(32,16),每块的大小(4k,8k)) |
| gzip_comp_level [1-9] | 压缩级别(级别越高,压得越小,同时越消耗 CPU 计算资源)。推荐 6 |
| gzip_disable | 正则匹配。哪些 URI 不进行 gzip 压缩 |
| gzip_min_length 200 | 开始压缩的最小长度(单位字节),小于该长度就不压缩 |
| gzip_http_version 1.0|1.1 | 开始压缩的 http 版本协议(可以不设置,默认 1.1) |
| gzip_proxied | 设置请求代理服务器如何缓存 |
| gzip_types text/plain application/xml | 对哪些类型的文件使用压缩,如 html(默认)、xml、css、js |
| gzip_vary on|off | 是否传输 gzip 压缩标志 |
其中 gzip_type 的参数要使用文件 MIME 类型,可以在 /usr/local/nginx/conf/mime.type 文件中查看
[root@localhost nginx]# cat conf/mime.types
types {
text/html html htm shtml;
text/css css;
text/xml xml;
image/gif gif;
image/jpeg jpeg jpg;
application/javascript js;
application/atom+xml atom;
application/rss+xml rss;
text/mathml mml;
text/plain txt;
text/vnd.sun.j2me.app-descriptor jad;
text/vnd.wap.wml wml;
text/x-component htc;
image/png png;
image/tiff tif tiff;
image/vnd.wap.wbmp wbmp;
image/x-icon ico;
image/x-jng jng;
image/x-ms-bmp bmp;
image/svg+xml svg svgz;
image/webp webp;
application/font-woff woff;
application/java-archive jar war ear;
application/json json;
application/mac-binhex40 hqx;
application/msword doc;
application/pdf pdf;
application/postscript ps eps ai;
application/rtf rtf;
application/vnd.apple.mpegurl m3u8;
application/vnd.ms-excel xls;
application/vnd.ms-fontobject eot;
application/vnd.ms-powerpoint ppt;
application/vnd.wap.wmlc wmlc;
application/vnd.google-earth.kml+xml kml;
application/vnd.google-earth.kmz kmz;
application/x-7z-compressed 7z;
application/x-cocoa cco;
application/x-java-archive-diff jardiff;
application/x-java-jnlp-file jnlp;
application/x-makeself run;
application/x-perl pl pm;
application/x-pilot prc pdb;
application/x-rar-compressed rar;
application/x-redhat-package-manager rpm;
application/x-sea sea;
application/x-shockwave-flash swf;
application/x-stuffit sit;
application/x-tcl tcl tk;
application/x-x509-ca-cert der pem crt;
application/x-xpinstall xpi;
application/xhtml+xml xhtml;
application/xspf+xml xspf;
application/zip zip;
application/octet-stream bin exe dll;
application/octet-stream deb;
application/octet-stream dmg;
application/octet-stream iso img;
application/octet-stream msi msp msm;
application/vnd.openxmlformats-officedocument.wordprocessingml.document docx;
application/vnd.openxmlformats-officedocument.spreadsheetml.sheet xlsx;
application/vnd.openxmlformats-officedocument.presentationml.presentation pptx;
audio/midi mid midi kar;
audio/mpeg mp3;
audio/ogg ogg;
audio/x-m4a m4a;
audio/x-realaudio ra;
video/3gpp 3gpp 3gp;
video/mp2t ts;
video/mp4 mp4;
video/mpeg mpeg mpg;
video/quicktime mov;
video/webm webm;
video/x-flv flv;
video/x-m4v m4v;
video/x-mng mng;
video/x-ms-asf asx asf;
video/x-ms-wmv wmv;
video/x-msvideo avi;
}
mime.types
在 /usr/local/nginx/conf/nginx.conf 中开始配置:
[root@localhost nginx]# vim conf/nginx.conf
gzip 的上下文(context)可以是 http、server、location、if in location
如果针对针对整个网站发挥作用,就在 server 段中进行配置:
server {
listen ;
server_name localhost;
#gzip
gzip on;
gzip_buffers 4k;
gzip_comp_level ;
gzip_min_length ;
gzip_types text/css text/xml application/x-javascript;
平滑重启 nginx。
此时访问服务器上 ecshop 项目的某个 URL,观察 http 响应头:

由于是分块传输,因此看不到 Content-Length。
此时再访问一个内容较小的页面(内容长度小于 200 字节),观察 http 响应头:

此时内容没有被 gzip 压缩。
在项目中 gzip_min_length 可设置较大值,如 4000(4k)。
Nginx 笔记与总结(13)Nginx 的 gzip 压缩的更多相关文章
- 【nginx网站性能优化篇(1)】gzip压缩与expire浏览器缓存
gzip压缩 概述 网页在服务器端经过了gzip或者其他格式的压缩后的输出明显减少了content-length字节,当访问过百万时,这些减少的字节就会变为客观的流量给节约下来;从而减轻服务器的压力以 ...
- Nginx笔记总结十六:nginx优化指南
1.高层的配置 worker_processes 定义了nginx对外提供web服务时的worker进程数 worker_rlimit_nofile 更改worker进程最大打开文件数量限制,如果没有 ...
- Nginx 笔记(四)nginx 原理与优化参数配置 与 nginx 搭建高可用集群
个人博客网:https://wushaopei.github.io/ (你想要这里多有) 一.nginx 原理与优化参数配置 master-workers 的机制的好处 首先,对于每个 ...
- Nginx 笔记(三)nginx 配置实例 - 反向代理、负载均衡、动静分离
个人博客网:https://wushaopei.github.io/ (你想要这里多有) 一.反向代理 反向代理准备工作: (1)在 liunx 系统安装 tomcat,使用默认端口 8080 ...
- Nginx 笔记(二)nginx常用的命令和配置文件
个人博客网:https://wushaopei.github.io/ (你想要这里多有) 1.nginx常用的命令 (1)启动命令 在/usr/local/nginx/sbin 目录下执行 ./ ...
- Nginx 笔记(一)nginx简介与安装
个人博客网:https://wushaopei.github.io/ (你想要这里多有) Nginx 简介: 1.介绍 nginx 的应用场景和具体可以做什么事情 2.介绍什么是反向代理 3.介 ...
- Nginx笔记总结十四: nginx反向代理,用内网域名转发
user www www; worker_processes ; error_log logs/error.log; pid logs/nginx.pid; worker_rlimit_nofile ...
- Nginx笔记总结十九:nginx + fancy实现漂亮的索引目录
编译:./configure --prefix=/usr/local/nginx --add-module=../ngx-fancyindex-master 配置: location / { fanc ...
- Nginx笔记总结十八:nginx统计响应的http状态码信息(ngx-http-status-code-counter)
编译:./configure --prefix=/usr/local/nginx --add-module=../ngx_http_status_code_counter-master make &a ...
- Nginx笔记总结十五:nginx+keepalive+proxy_cache配置高可用nginx集群和高速缓存
nginx编译 wget http://labs.frickle.com/files/ngx_cache_purge-2.3.tar.gz ./configure --prefix=/usr/loca ...
随机推荐
- 自定义viewgroup实现ArcMenu
最终效果如下 实现思路 通过效果图,会有几个问题: a.动画效果如何实现 可以看出动画是从顶点外外发射的,可能有人说,那还不简单,默认元素都在定点位置,然后TraslateAnimation就好了:这 ...
- ImageLoader实现图片异步加载
ImageLoader是一个广泛使用的图片库,在向网络请求图片时,使用imageView和smartView常会产生outofmemory错误,这时ImageLoader可以起到很大的作用,主要有如下 ...
- redhad借用CentOs yum 安装
RedHat linux 默认是安装了yum软件的,但是由于激活认证的原因让redhat无法直接进行yum安装一些软件,如果我们需要在redhat下直接yum安装软件,我们只用把yum的源修改成Cen ...
- nginx: [warn] conflicting server name "locahost" on 0.0.0.0:80, ignored
里面域名重复: 在vhosts下多个虚拟机配置文件,都是基于域名配置的,其中两个配置文件,都起了localhost ,所以会报错!!!! 多个域名可以指向同一个目录,但同一个域名不可一指向多个目录!! ...
- 10.组合模式(Composite Pattern)
using System; using System.Collections.Generic; namespace ConsoleApplication8 { class Program { stat ...
- hdu 1215 筛法
求小于n的n的因子之和 Sample Input 3 2 10 20Sample Output 1 8 22 #include<cstdio> #include<iostream&g ...
- yum与rpm的使用
rpm常用的命令组合: rpm -ivh:安装显示安装进度--install--verbose--hash -Uvh:升级软件包--Update: -qpl:列出RPM软件包内的文件信息[Query ...
- &是什么运算符(转)
&表示两种运算符,其中一种表示取值运算符,一种是按位与 取值运算符 int a=1; int *p=&a; //其中&a表示的就是把a中的地址取出来,然后赋给指针变量,也就是说 ...
- 作用域与闭包:this,var
var 作用域 先来看个简单的例子: var parent = function () { var name = "parent_name"; var age = 13; var ...
- MATLAB信号与系统分析(一)——连续时间信号与系统的时域分析
一.连续时间信号的表示: 1.向量表示法: 在MATLAB中,是用连续信号在等时间间隔点的样值来近似表示连续信号,当取样时间间隔足够小时,这些离散的样值就能较好地近似出连续信号. 对于连续时间信号f( ...