登录 nginx 服务器查看配置文件
$ nginx -t
ngx_http_fastdfs_set pid=1568
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
编辑配置文件
$ vim /usr/local/nginx/conf/nginx.conf
在 vim 界面输入 /server 按下回车搜索,键盘 n 寻找下一个。
在 server:80 添加配置,并保存退出。
# 开启和关闭gzip模式
gzip on;
# gizp压缩起点,文件大于1k才进行压缩
gzip_min_length 1k;
# 设置压缩所需要的缓冲区大小,以4k为单位,如果文件为7k则申请2*4k的缓冲区
gzip_buffers 4 16k;
# 设置gzip压缩针对的HTTP协议版本
gzip_http_version 1.0;
# gzip 压缩级别,1-9,数字越大压缩的越好,也越占用CPU时间
gzip_comp_level 5;
# 进行压缩的文件类型
gzip_types text/plain application/javascript text/css application/xml;
# 是否在http header中添加Vary: Accept-Encoding,建议开启
gzip_vary on;
nginx 配置测试成功后重启。
$ nginx -t
ngx_http_fastdfs_set pid=1972
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
$ nginx -s reload
刷新浏览器,查看 gzip 是否生效。