Nginx 配置 Gzip 压缩
打开配置文件 /etc/nginx/nginx.conf
,取消掉以下的注释项:
#gzip on;
取消后:
gzip on;
在此配置后加上以下内容:
gzip on;
gzip_vary on;
gzip_min_length 10240;
gzip_proxied expired no-cache no-store private auth;
gzip_types text/plain text/css text/xml text/javascript application/x-javascript application/xml;
gzip_disable "MSIE [1-6]\.";
以上各项的详解:
gzip on;
- 启用gzip压缩;gzip_vary on;
- 告知代理缓存常规及压缩版本;gzip_min_length 1024;
- 告知 NGINX 不压缩小于指定大小的内容;gzip_proxied
- 即使客户端是通过代理连接也压缩文件 (此处配置为:当一个response头部包含"expired"、"no-cache"、 "no-store"、"private"或"Authorization"时启用压缩);gzip_types
- 能够被压缩的文件类型;gzip_disable "MSIE [1-6]\.";
- Internet Explorer 1-6 的版本禁用压缩。
修改完成后可以使用 nginx -t
测试修改的配置文章,确定无误后重启nginx服务即可,重启命令:
systemctl restart nginx
或者:
nginx -s reload
参考:https://www.techrepublic.com/article/how-to-configure-gzip-compression-with-nginx/