我们先进行一个简单的压缩配置
这里只是开启了一下压缩
1 server { 2 listen 8087; 3 server_name xiao.xatyk.com; 4 index index.html index.htm index.php; 5 root /kaka; 6 location / { 7 rewrite ^/$/index.php last; 8 rewrite ^/(?!index\.php|robots\.txt|static|uploads)(.*)$ /index.php/$1 last; 9 } 10 11 gzip on; 12 location ~ \.php($|/) { 13 fastcgi_pass 127.0.0.1:9000; 14 fastcgi_index index.php; 15 fastcgi_split_path_info ^(.+\.php)(.*)$; 16 fastcgi_param PATH_INFO $fastcgi_path_info; 17 fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; 18 fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info; 19 include fastcgi_params; 20 } 21 22 if (!-e $request_filename) { 23 rewrite ^/(.*)$ /index.php/$1 last; 24 break; 25 } 26 access_log off; 27 }
测试
这是原文本大小
下面是压缩后的大小,可以看到文件大小压缩了近5倍
在补充几个参数
# 开启gzip gzip on; # 启用gzip压缩的最小文件,小于设置值的文件将不会压缩 gzip_min_length 1k; # gzip 压缩级别,1-10,数字越大压缩的越好,也越占用CPU时间,后面会有详细说明 gzip_comp_level 2; # 进行压缩的文件类型。javascript有多种形式。其中的值可以在 mime.types 文件中找到。 gzip_types text/plain application/javascript application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png; # 是否在http header中添加Vary: Accept-Encoding,建议开启 gzip_vary on; # 禁用IE 6 gzip gzip_disable "MSIE [1-6]\.";
这一片文章写得有点杂,后边在实战的时候在补充