如何在Linux上启用GZIP和Brotli压缩Nginx

时间:2020-02-23 14:38:27  来源:igfitidea点击:

我刚刚在Linux系统上安装了Nginx Web服务器,如何为HTML/JS/CSS/JSON文件等静态资产启用GZIP和Brotli压缩?
更快的网页加载时间是每个Web开发人员和系统管理员的愿望。
压缩不仅可以改善Google页面排名,而且还有更好的用户体验和改进参与。

本文专注于引导我们通过在Linux中运行的nginx Web服务器上配置Gzip和Brotli压缩。
GZIP&Brotli是主要Web浏览器支持的最流行的压缩算法。

gzip压缩算法?

GZIP提供无损压缩,这意味着在解压缩时可以恢复原始数据。
它基于缩小算法,它是LZ77和霍夫曼编码的组合。

Brotli压缩算法?

就像GZIP一样,Brotli是一个广泛支持许多浏览器的无损压缩算法。
它由Google开发,最适合压缩基于文本的静态资源,如JSON,JS,CSS和HTML。
我们将在此设置中使用nginx模块进行brotli压缩。

为brotli安装ngx_brotli - nginx模块

ngx_brotli是nginx模块,它使用brotli进行压缩任务。
它是一组两个模块:NGX_BROTLI过滤器模块 - 用于压缩禁止的响应响应 - vly.ngx_brotli静态模块 - 用于服务预压缩文件。

在Ubuntu上安装NGX_BROTLI模块

下面的命令用于在Ubuntu系统上安装NGX_BROTLI模块。

sudo apt-add-repository -y ppa:hda-me/nginx-stable
sudo apt-get update
sudo apt-get install brotli nginx nginx-module-brotli

重新启动nginx以使用brotli。

sudo systemctl restart nginx

在CentOS 7上安装NGX_BROTLI模块

我们将使用GetPagespeed Extras yum存储库在CentOS 7服务器上安装Brotli的Nginx模块。

sudo yum -y install https://extras.getpagespeed.com/release-el7-latest.rpm
sudo yum -y install nginx nginx-module-nbr

安装后需要启用模块。
编辑nginx配置文件 - /etc/nginx/nginx.conf并在顶部附近添加这些行。

load_module modules/ngx_http_brotli_filter_module.so;
 load_module modules/ngx_http_brotli_static_module.so;

这是我的oniToroad nginx配置的屏幕截图。

配置nginx使用brotli/gzip压缩

现在我们已安装压缩模块。
为打开nginx配置文件并添加以下行。

# gzip
gzip on;
gzip_vary on;
gzip_proxied any;
gzip_comp_level 6;
gzip_types text/plain text/css text/xml application/json application/javascript application/xml+rss application/atom+xml image/svg+xml;
# brotli
brotli on;
brotli_comp_level 6;
brotli_types text/xml image/svg+xml application/x-font-ttf image/vnd.microsoft.icon application/x-font-opentype application/json font/eot application/vnd.ms-fontobject application/javascript font/otf application/xml application/xhtml+xml text/javascript  application/x-javascript text/plain application/x-font-truetype application/xml+rss image/x-icon font/opentype text/css image/x-win-bitmap;

Nginx Web服务器需要重新启动以进行静态内容要压缩。

sudo systemctl restart nginx

在上测试Brotli/Gzip压缩

支持Brotli的浏览器在接受编码请求标题中与"gzip"一起发送'br'。

$curl -IL https://theitroad.com -H "Accept-Encoding: br"
HTTP/2 200 
date: Sat, 20 Apr 2019 20:06:28 GMT
content-type: text/html; charset=UTF-8
vary: Accept-Encoding
...
x-frame-options: SAMEORIGIN
x-xss-protection: 1; mode=block
x-content-type-options: nosniff
expect-ct: max-age=604800, report-uri="https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct"
server: cloudflare
cf-ray: 4ca9bd24fdfecb79-MBA
content-encoding: br

检查GZIP编码用卷曲:

$curl -IL https://theitroad.com -H "Accept-Encoding: gzip"
HTTP/2 200 
date: Sat, 20 Apr 2019 20:06:28 GMT
content-type: text/html; charset=UTF-8
vary: Accept-Encoding
...
x-frame-options: SAMEORIGIN
x-xss-protection: 1; mode=block
x-content-type-options: nosniff
expect-ct: max-age=604800, report-uri="https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct"
server: cloudflare
cf-ray: 4ca9bd24fdfecb79-MBA
content-encoding: gzip

如果在Web服务器上启用了Brotli,则会在Brotli压缩格式中获得响应。
我们还可以使用Web浏览器来检查内容编码,如下:

Inspect > Network > Headers > "Response Headers" > "Content-Encoding" header