根据前面几篇文章,我们已经成功搭建起基于nginx/nodejs的站点,可ftp上传/管理站点文件,可实现开机无登录自动启动服务。似乎,这样已经足够了,然鹅!我们还可以开启gzip提高访问速度,设置cache高效利用本地缓存。
开启Gzip压缩
打开nginx.conf文件(路径因安装方法不同而有所区别):
在http{}
定义区域添加:
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_comp_level 5;
gzip_types text/plain application/x-javascript text/javascript application/javascript text/css application/xml application/x-httpd-php image/jpeg image/gif image/png image/jpg;
gzip_vary off;
|
重启nginx服务,查看站点访问时返回的资源网络响应头信息中是否有:Content-Encoding: gzip
,如果有,说明资源已经成功Gzip压缩!
设置Cache-Control
参考文章:http://www.liulichao.com/2016/04/15/nginx-set-cache.html
在http{}
定义区域添加:
proxy_cache_path /usr/share/nginx/cache levels=1:2 keys_zone=cache_one:500m inactive=10d max_size=2g;
proxy_temp_path /usr/share/nginx/cache/temp;
|
在server{}
定义域内添加:
location /assets/ { root /var/www/ROOT; access_log off; proxy_set_header Host $host:$server_port; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504; proxy_cache cache_one; proxy_cache_valid 200 304 12h; proxy_cache_valid any 10m; proxy_cache_key $host$uri$is_args$args; add_header Nginx-Cache "$upstream_cache_status"; expires 1h; }
|
此时运行,依然会报错,因为cache路径权限未匹配: