启动gzip本身非常简单,只需要在server
中加入配置:、
gzip on;
gzip_vary on;
gzip_proxied any;
gzip_types text/css text/plain application/javascript application/x-javascript text/javascript;
gzip_min_length 1000;
gzip_min_length
表示只有数据超过这一长度之后,才启用压缩。这是因为,对于小块的数据,压缩之后文件的尺寸反而可能增加。并且压缩过程本身需要解压,如果加上压缩解压的时间,可能还没有直接传输数据速度快,因此对于小于1k的数据不启用压缩。
gzip_types
列表表示对于那些内容启用压缩,有时候,比如对于jpg
文件,或者一些音频文件,我们是无需压缩的。
服务器会根据请求中的头信息来判断是否传输压缩数据,默认的使用curl
发送的请求并没有要求服务器返回压缩数据:
$ curl http://example.com/ --silent --write-out "%{size_download}\n" --output /dev/null
而加上Accept-Encoding: gzip,deflate
头信息告诉服务器,客户端可以处理压缩数据,这样服务器将会返回压缩过的数据(如果响应模块启动了的话):
$ curl http://example.com/ --silent -H "Accept-Encoding: gzip,deflate" --write-out "%{size_download}\n" --output /dev/null
下面是一个实际的例子
$ curl http://10.144.244.143:8030/GisServer/openlayer/OpenLayers.js -s -w "%{size_download}\n" -o /dev/null
739725
$ curl http://10.144.244.143:8030/GisServer/openlayer/OpenLayers.js -H "Accept-Encoding: gzip,deflate,sdch" -s -w "%{size_download}\n" -o /dev/null
225319
可以看到,压缩之后,数据只有之前的1/3
。
$curl http://10.144.244.143:8030/GisServer/openlayer/OpenLayers.js -H "Accept-Encoding: gzip,deflate,sdch" -s -I HTTP/1.1 200 OK
...
Content-Encoding: gzip
location ~* ^/(galaxy-geo-web/|GisServer/).+\.(css|js|gif|png|html|jpe?g)$ {
root /opt/nastar/webapps/;
expires 1h;
}
对于css/js/html
等静态文件,在应用程序发布之后,可能很久之后才会改动,这时可以启动expires
这样的设置。默认的,如果不设置任何的超时设置,浏览器每次会发送请求,如果服务器端返回304之后,浏览器就使用本地的缓存。这个过程虽然没有实际数据的传输(会传输HTTP头),但是仍然会建立连接,释放连接。
如果设置了expires
,那么在超时之前,浏览器会直接从缓存中读取数据,而不发送任何请求。这在很多情景下都是适用的。
$curl http://10.144.244.143:8030/GisServer/openlayer/OpenLayers.js -H "Accept-Encoding: gzip,deflate,sdch" -s -I HTTP/1.1 200 OK
...
Expires: Fri, 21 Feb 2014 07:07:40 GMT
Cache-Control: max-age=3600
...
这个头信息定义了该资源何时过期。每次刷新浏览器之后,浏览器都会检查已经缓存的资源,查看资源十分已经过期,如果没有则直接使用,否则会发送请求到服务器端更新本地缓存。
首先需要
使用命令lsb_release -a
来查看系统环境:
omcuser@nassvr:/opt/tmp> lsb_release -a
LSB Version: core-2.0-noarch:core-3.0-noarch:core-2.0-x86_64:core-3.0-x86_64:desktop-3.1-amd64:desktop-3.1-noarch:graphics-2.0-amd64:graphics-2.0-noarch:graphics-3.1-amd64:graphics-3.1-noarch
Distributor ID: SUSE LINUX
Description: SUSE Linux Enterprise Server 10 (x86_64)
Release: 10
Codename: n/a
先下载所有的源码包nginx
, openssl
, pcre
, zlib
。openssl
用以在nginx中启用https,pcre
用以启用正则表达式,zlib
则用以启动压缩。
下载之后,将所有源码放到/opt/tmp
目录下:
omcuser@nassvr:/opt/tmp> ls -al
total 14
drwxr-x--- 7 omcuser omcsysm 232 2014-02-21 14:50 .
drwxrwxr-x 16 root root 576 2014-02-19 11:34 ..
drwxr-x--- 2 omcuser omcsysm 192 2014-02-19 11:38 libs
-rwxr-x--- 1 omcuser omcsysm 350 2014-02-19 11:41 make_nginx.sh
drwxr-x--- 9 omcuser omcsysm 376 2014-02-19 11:41 nginx-master
drwxr-x--- 23 omcuser omcsysm 1640 2014-02-19 11:44 openssl-1.0.1c
drwxr-xr-x 8 omcuser omcsysm 5704 2014-02-19 11:46 pcre-8.30
drwxr-x--- 14 omcuser omcsysm 2136 2014-02-19 11:46 zlib-1.2.8
然后运行上边的命令make_nginx.sh
来将nginx安装到/opt/nastar/nginx
目录下。
$ /opt/nastar/nginx/nginx -t #测试配置文件是否正确
$ /opt/nastar/nginx/nginx #启动nginx,默认配置文件为nginx.conf
$ /opt/nastar/nginx/nginx -s reload #修改配置文件之后,重新加载配置文件
omcuser@nassvr:~> lsof -i :8010 | wc -l
102