nginx开启和配置gzip

查看ngix编译参考,是否启用了gzip模块,运行命令:

/path/to/binary/nginx -V

nginx version: nginx/1.2.0
configure arguments: --prefix=/usr/local/nginx --sbin-path=/usr/local/sbin/nginx --conf-path=/usr/local/conf/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --with-http_stub_status_module --with-http_gzip_static_module --with-http_sub_module --with-http_flv_module --with-http_mp4_module --with-http_random_index_module --with-cpu-opt=pentium4 --add-module=../nginx-lib/nginx-upload-progress-module-v0.9.0/

修改nginx配置文件,nginx配置文件目录参看nginx配置参数里的--conf-path。按我的配置,修改/usr/local/conf/nginx/nginx.conf,在http节点加入gzip配置即可。

你直接复制到你的nginx的主机配置文件中就行了,如果有兴趣,下面再来分别解释它们是做什么的。
 代码如下     复制代码

gzip on;
gzip_min_length 1k;
gzip_buffers 16 64k;
gzip_http_version 1.1;
gzip_comp_level 6;
gzip_types text/plain application/x-javascript text/css application/xml;
gzip_vary on;
 

测试配置是否有语法错误
nginx -t

输出消息

nginx: the configuration file /usr/local/conf/nginx/nginx.conf syntax is ok
nginx: configuration file /usr/local/conf/nginx/nginx.conf test is successful

确认无误,重新加载nginx配置

nginx -s reload

这些配置是做什么的?

随着nginx的发展,越来越多的网站使用nginx,因此nginx的优化变得越来越重要,今天我们来看看nginx的gzip压缩到底是怎么压缩的呢?

gzip(GNU- ZIP)是一种压缩技术。经过gzip压缩后页面大小可以变为原来的30%甚至更小,这样,用户浏览页面的时候速度会块得多。gzip的压缩页面需要浏览 器和服务器双方都支持,实际上就是服务器端压缩,传到浏览器后浏览器解压并解析。浏览器那里不需要我们担心,因为目前的巨大多数浏览器都支持解析gzip 过的页面。
Nginx的压缩输出有一组gzip压缩指令来实现。相关指令位于http{….}两个大括号之间。
 

每个设置的含义

gzip on;

//该指令用于开启或关闭gzip模块(on/off)

gzip_min_length 1k;

//设置允许压缩的页面最小字节数,页面字节数从header头得content-length中进行获取。默认值是0,不管页面多大都压缩。建议设置成大于1k的字节数,小于1k可能会越压越大。

gzip_buffers 4 16k;

//设置系统获取几个单位的缓存用于存储gzip的压缩结果数据流。4 16k代表以16k为单位,安装原始数据大小以16k为单位的4倍申请内存。

gzip_http_version 1.1;

//识别http的协议版本(1.0/1.1)

gzip_comp_level 2;

//gzip压缩比,1压缩比最小处理速度最快,9压缩比最大但处理速度最慢(传输快但比较消耗cpu)

gzip_types text/plain application/x-javascript text/css application/xml

//匹配mime类型进行压缩,无论是否指定,”text/html”类型总是会被压缩的。

gzip_vary on;

//和http头有关系,加个vary头,给代理服务器用的,有的浏览器支持压缩,有的不支持,所以避免浪费不支持的也压缩,所以根据客户端的HTTP头来判断,是否需要压缩

nginx.conf例子

user root;
worker_processes  1;

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

#pid        logs/nginx.pid;


events {
    worker_connections  10240;
}


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;

    gzip  on;
    gzip_min_length 1k;
    gzip_buffers 16 64k;
    gzip_http_version 1.1;
    gzip_comp_level 2;
    gzip_types application/json text/plain text/css;
    gzip_vary on;

    fastcgi_intercept_errors on;

	upstream tomcat_server {
	   server   127.0.0.1:8080;
#      server   127.0.0.1:8081;
#      server   127.0.0.1:8082;
	}


############################
########   server start ####
############################
    server {
        listen       80;
        server_name  www.xxx.com;
        root    /data/www/www.xxx.com;

        location /{
        	index index.htm index.html index.jsp;
        }

        location ~ ^/(WEB-INF|META-INF)/{
                deny all;
        }

        location ~ \.(jsp|do)?$ {
	        proxy_set_header Host  $host:80;
	        proxy_set_header X-Forwarded-For  $remote_addr;
	        proxy_pass http://tomcat_server;
    	}

    	location ~ .*\.(js|css|gif|jpg|jpeg|png|bmp|swf)$ {
	        expires 24h;
	    }

        if (!-e $request_filename){
                rewrite "^/a/eg/([0-9]+)/([0-9]+)/([0-9]+)$" /api/eventlog.do?game_id=$1&platform_id=$2&channel_id=$3 last;
                rewrite "^/games/game_([0-9]+)\.html$" /games/game_show.jsp?id=$1 last;
        	rewrite "^/games/page_([0-9]+)\.html$" /games/index.jsp?p=$1 last;
        	rewrite "^/games/category_([0-9]+)\.html$" /games/index.jsp?c=$1 last;
        	rewrite "^/games/category_([0-9]+)/page_([0-9]+)\.html$" /games/index.jsp?c=$1&p=$2 last;
        	rewrite "^/blogs/blog_([0-9]+)\.html$" /blogs/blog_show.jsp?id=$1 last;
        	rewrite "^/blogs/page_([0-9]+)\.html$" /blogs/index.jsp?p=$1 last;
        	rewrite "^/blogs/category_([0-9]+)\.html$" /blogs/index.jsp?c=$1 last;
        	rewrite "^/blogs/category_([0-9]+)/page_([0-9]+)\.html$" /blogs/index.jsp?c=$1&p=$2 last;
        	rewrite "^/links/category_([0-9]+)\.html$" /links/index.jsp?c=$1 last;
        }
    }
############################
########   server end   ####
############################
}

猜你喜欢

转载自stephen830.iteye.com/blog/2208334