分享

Nginx的gzip压缩

 JhouShuai 2015-01-08


cat /usr/local/nginx/conf/nginx.conf

user  www;
worker_processes  2;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;


events {
    use epoll;
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;
    client_max_body_size 20m;

    ####默认情况下,Nginx的gzip压缩是关闭的
    #gzip  on;

    server {
        listen       80;
        server_name  localhost;
        root html;
index index.php index.html index.htm

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        location ~ \.php$ {
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }

    server {
      listen       80;
      server_name  hipigo.51.local;
      root   /home/www/hipigo.cn/1.5.1/public;
      index  index.html index.htm index.php;

      charset utf-8;
      location ~ ^/(images|javascript|js|css|flash|media|static)/  {      
        expires 30d; 
        log_not_found off;
      }
      
      # 设置不被防问的目录
      location ~ ^/(application|library|var)/ {
        deny all;
      }
      
      #将request指向index.php
      location / {
        
        if (-f $request_filename) {
          break;
        }
        if (-d $request_filename) {
          break;
        }
        rewrite ^(.+)$ /index.php last;
      }
      
     
      #引用PHP CGI
      location ~ .*\.(php|php5)?$ {
          fastcgi_pass   127.0.0.1:9000;
          include fastcgi_params;
          fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name; 
          fastcgi_read_timeout 600;
      }
    }

    server {
      listen       80;
      server_name  hipigo.53.local;
      root   /home/www/hipigo.cn/1.5.3/public;
      index  index.html index.htm index.php;

      charset utf-8;
      location ~ ^/(images|javascript|js|css|flash|media|static)/  {
        expires 30d;
        log_not_found off;
      }

      # 设置不被防问的目录
      location ~ ^/(application|library|var)/ {
        deny all;
      }

      #将request指向index.php
      location / {

        if (-f $request_filename) {
          break;
        }
        if (-d $request_filename) {
          break;
        }
        rewrite ^(.+)$ /index.php last;
      }


      #引用PHP CGI
      location ~ .*\.(php|php5)?$ {
          fastcgi_pass   127.0.0.1:9000;
          include fastcgi_params;
          fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
          fastcgi_read_timeout 600;
      }
    }

    server {
      listen       80;
      server_name  hipigo.5.local;
      root   /home/www/hipigo.cn/1.5/public;
      index  index.html index.htm index.php;

      charset utf-8;
      location ~ ^/(images|javascript|js|css|flash|media|static)/  {      
        expires 30d; 
        log_not_found off;
      }
      
      # 设置不被防问的目录
      location ~ ^/(application|library|var)/ {
        deny all;
      }
      
      #将request指向index.php
      location / {
        
        if (-f $request_filename) {
          break;
        }
        if (-d $request_filename) {
          break;
        }
        rewrite ^(.+)$ /index.php last;
      }
      
     
      #引用PHP CGI
      location ~ .*\.(php|php5)?$ {
          fastcgi_pass   127.0.0.1:9000;
          include fastcgi_params;
          fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name; 
          fastcgi_read_timeout 600;
      }
    }


    server {
      listen       80;
      server_name  dev.2bi.net;
      root   /home/www/2bi/wwwroot/;
      index  index.php index.htm index.html;

      charset utf-8;
     
     location /admin/ {
       if (-f $request_filename) {
          break;
        }
        if (-d $request_filename) {
          break;
        }
        rewrite ^(.+)$ /admin/index.php last;
      }
     
     # 设置不被防问的目录
     # location ~ ^/home/ {
     #   deny all;
     # }
     #零售官网首页
     location /home/ {
       if (-f $request_filename) {
          break;
        }
        if (-d $request_filename) {
          break;
        }
        rewrite ^(.+)$ /home/index.php last;
      }

 
     location / {
        
        if (-f $request_filename) {
            break;
        }
        if (-d $request_filename) {
          break;
        }
        rewrite ^(.+)$ /index.php last;
      }
      
     
      #引用PHP CGI
      location ~ .*\.(php|php5)?$ {
          fastcgi_pass   127.0.0.1:9000;
          include fastcgi_params;
          fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name; 
          fastcgi_read_timeout 600;
      }
    }


   server {
      listen       80;
      server_name  pay.dev.hipigo.cn;
      root   /home/www/hipigo.cn/pay/wwwroot/;
      index  index.php index.htm index.html;

      charset utf-8;
      location / {

        if (-f $request_filename) {
          break;
        }
        if (-d $request_filename) {
          break;
        }
        rewrite ^(.+)$ /index.php last;
      }


      #引用PHP CGI
      location ~ .*\.(php|php5)?$ {
          fastcgi_pass   127.0.0.1:9000;
          include fastcgi_params;
          fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
          fastcgi_read_timeout 600;
      }
    }

    
   server {
      listen       80;
      server_name  dev.cmen.com;
      root   /home/www/cmen/wwwroot/;
      index  index.php index.htm index.html;

      charset utf-8;

     location /admin/ {
       if (-f $request_filename) {
          break;
        }
        if (-d $request_filename) {
          break;
        }
        rewrite ^(.+)$ /admin/index.php last;
      }

     # 设置不被防问的目录
     # location ~ ^/home/ {
     #   deny all;
     # }
     #官网首页
     location /home/ {
       if (-f $request_filename) {
          break;
        }
        if (-d $request_filename) {
          break;
        }
        rewrite ^(.+)$ /home/index.php last;
      }


     location / {

        if (-f $request_filename) {
            break;
        }
        if (-d $request_filename) {
          break;
        }
        rewrite ^(.+)$ /index.php last;
      }

      #引用PHP CGI
      location ~ .*\.(php|php5)?$ {
          fastcgi_pass   127.0.0.1:9000;
          include fastcgi_params;
          fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
          fastcgi_read_timeout 600;
      }
    }





    server {
      listen       80;
      server_name  wx.dev.hipigo.cn;
      root   /home/www/hipigo.cn/2.0/trunck/wwwroot;
      index  index.php index.htm index.html;

      charset utf-8;
    
      location /biz {

        if (-f $request_filename) {
          break;
        }
        if (-d $request_filename) {
          break;
        }
        rewrite ^(.+)$ /biz/index.php last;
      }

      location /wapi {

        if (-f $request_filename) {
          break;
        }
        if (-d $request_filename) {
          break;
        }
        rewrite ^(.+)$ /wapi/index.php last;
      }
location / {
        if (-f $request_filename) {
expires  max;
          break;
        }
if (!-e $request_filename){
rewrite ^/(.*)$ /index.php last;
}
}     
     
#引用PHP CGI
location ~ .*\.(php|php5)?$ {
fastcgi_pass   127.0.0.1:9000;
include fastcgi_params;
fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name; 
fastcgi_read_timeout 600;
}
}

    server {
      listen       80;
      server_name  manage.dev.xrenwu.com;
      root   /home/www/it008.com.xrenwu/manage/public;
      index  index.php index.htm index.html;

      charset utf-8;
      location / {

        if (-f $request_filename) {
          break;
        }
        if (-d $request_filename) {
          break;
        }
        rewrite ^(.+)$ /index.php last;
      }


      #引用PHP CGI
      location ~ .*\.(php|php5)?$ {
          fastcgi_pass   127.0.0.1:9000;
          include fastcgi_params;
          fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
          fastcgi_read_timeout 600;
      }
   }

   server {
      listen       80;
      server_name  api.dev.xrenwu.com;
      root   /home/www/it008.com.xrenwu/service/public;
      index  index.php index.htm index.html;

      charset utf-8;
      location / {
        rewrite ^(.+)$ /index.php last;
      }


      #引用PHP CGI
      location ~ .*\.(php|php5)?$ {
          fastcgi_pass   127.0.0.1:9000;
          include fastcgi_params;
          fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
          fastcgi_read_timeout 600;
      }
   }

   server {
      listen       80;
      server_name  dev.xrenwu.com;
      root   /home/www/it008.com.xrenwu/website/public;
      index  index.php index.htm index.html;

      charset utf-8;
      location / {

        if (-f $request_filename) {
          break;
        }
        if (-d $request_filename) {
          break;
        }
        rewrite ^(.+)$ /index.php last;
      }


      #引用PHP CGI
      location ~ .*\.(php|php5)?$ {
          fastcgi_pass   127.0.0.1:9000;
          include fastcgi_params;
          fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
          fastcgi_read_timeout 600;
      }
   }
    server {
      listen       80;
      server_name  att.it008.com;
      root   /home/www/hipigo.cn/att;
      index  index.php index.htm index.html;

      charset utf-8;
      location / {
        
        if (-f $request_filename) {
          break;
        }
        if (-d $request_filename) {
          break;
        }
        rewrite ^(.+)$ /index.php last;
      }
      
     
      #引用PHP CGI
      location ~ .*\.(php|php5)?$ {
          fastcgi_pass   127.0.0.1:9000;
          include fastcgi_params;
          fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name; 
          fastcgi_read_timeout 600;
    }
   }
}

以下内容来自:

默认情况下,Nginx的gzip压缩是关闭的, gzip压缩功能就是可以让你节省不少带宽,但是会增加服务器CPU的开销哦,Nginx默认只对text/html进行压缩 ,如果要对html之外的内容进行压缩传输,我们需要手动来调。

开启gzip的指令如下:

 代码如下复制代码

gzip on;
gzip_http_version 1.0;
gzip_disable "MSIE [1-6].";
gzip_types text/plain application/x-javascript text/css text/javascript;

关于gzip_types,如果你想让图片也开启gzip压缩,那么用以下这段吧:

gzip_types text/plain application/x-javascript text/css text/javascript application/x-httpd-php image/jpeg image/gif image/png;

网站开启gzip压缩,不仅能够节省带宽,也能够快速响应用户的访问,在nginx中,开启gzip压缩的方法为:
gzip on;      (启用 gzip 压缩功能)

    gzip_proxied any;  (nginx 做前端代理时启用该选项,表示无论后端服务器的headers头返回什么信息,都无条件启用压缩)

    gzip_min_length  1024; (最小压缩的页面,如果页面过于小,可能会越压越大,这里规定大于1K的页面才启用压缩)

    gzip_buffers     4 8k; (设置系统获取几个单位的缓存用于存储gzip的压缩结果数据流)

    gzip_comp_level 3; (压缩级别,1压缩比最小处理速度最快,9压缩比最大但处理最慢,同时也最消耗CPU,一般设置为3就可以了)

    gzip_types       text/plain text/css application/x-javascript application/javascript application/xml; (什么类型的页面或文档启用压缩)


注意:

1. 其中的gzip_http_version的设置,它的默认值是1.1,就是说对HTTP/1.1协议的请求才会进行gzip压缩
如果我们使用了proxy_pass进行反向代理,那么nginx和后端的upstream server之间是用HTTP/1.0协议通信的
This module makes it possible to transfer requests to another server.
It is an HTTP/1.0 proxy without the ability for keep-alive requests yet. (As a result, backend connections are created and destroyed on every request.) Nginx talks HTTP/1.1 to the browser and HTTP/1.0 to the backend server. As such it handles keep-alive to the browser.
如果我们使用nginx通过反向代理做Cache Server,而且前端的nginx没有开启gzip
同时,我们后端的nginx上没有设置gzip_http_version为1.0,那么Cache的url将不会进行gzip压缩
 
2. gzip_disable的设置是禁用IE6的gzip压缩,又是因为杯具的IE6
IE6的某些版本对gzip的压缩支持很不好,会造成页面的假死,今天产品的同学就测试出了这个问题
后来调试后,发现是对img进行gzip后造成IE6的假死,把对img的gzip压缩去掉后就正常了
为了确保其它的IE6版本不出问题,所以就加上了gzip_disable的设置


    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多