Nginx performance optimization (4)

A, gzip compression

1. gzip Introduction

gzip is an online real-time data ngx_http_gzip_module nginx server module provides compression. , Compression processing may be performed by the data server in response to opening gzip function, into smaller binary files. In the application of high PV's website, you can save vast amounts of bandwidth.

As shown, the client browser request header issued the following statement in view of the type of support gzip compressed file, if the server is enabled gzip compression, then the response data is compressed and then returned to the client, the client is responsible for decompression re-rendering:


8448742-f10bb8167f5808cd.png
http request header declare their support for compression type

2. gzip Configuration

Common gzip configuration parameters:

gzip on | off;    # 是否开启gzip
gzip_buffers 32 4K | 16 8K    # 缓冲(压缩数据缓冲在内存中,当达到N块内存,每块MK时,输出缓存)
gzip_comp_level [1-9]    # 压缩级别,级别越高,压缩比越大,越消耗CPU资源,推荐6
gzip_disable    # 指定不进行gzip压缩的uri
gzip_min_length 200    # 压缩文件长度超过200的文件
gzip_http_version 1.0 | 1.1    # 指定进行压缩的最小http协议版本
gzip_proxied    # 如果请求者是代理服务器,该如何缓存
gzip_types text/plain application/xml    # 指定要压缩的文件的类型
gzip_vary on | off    # 是否传输gzip压缩标志

Before opening gzip compression, data length page response under observation before we visited:


8448742-1a854246e299348b.png
Open gzip data length before transmission

Then open gzip:

[root@localhost conf]# vim nginx.conf
# 在http上下文中添加gzip配置
gzip  on;
gzip_buffers 32 4K;
gzip_comp_level 6;
gzip_min_length 200;
gzip_types text/xml text/css application/javascript image/jpeg;
# 保存配置退出,重启nginx服务
[root@localhost conf]# ../sbin/nginx -s reload

After opening gzip, the data length of the transmission is only 1/10 of the original size.


8448742-a5d864077fbfa39b.png
After the transmission data length Open gzip

3. Notes

  • Under normal circumstances, it is not recommended for pictures, mp3 and other compressed binary file configuration, because the compression rate of binary files is relatively small, but the cost is very high cpu resources
  • Not too small file compression

Two, expires cache

Reproduced in: https: //www.jianshu.com/p/6609829e68bf

Guess you like

Origin blog.csdn.net/weixin_34281537/article/details/91053008