京东云服务器推荐配置、多种配置选择,满足不同业务需求!

热门推荐

⭐ 稳定入门选择

配置:2核2G/5M

适用场景:个人站点 / 轻量应用

特点:性价比高,稳定可靠

¥19/月
立即购买
流量友好

☁️ 轻量云主机

配置:2核4G 5M

适用场景:小型业务 / 博客 / 测试环境

特点:流量友好,性能均衡

¥149/年
立即购买
高性能

💻 云服务主机

配置:4核16G 8M

适用场景:中小型服务 / 多并发场景

特点:充足内存与带宽,性能强劲

¥750/年
立即购买
新人专享

🎁 新人福利

新人访问 → 点击 → 最新活动

可获取 3000元 礼券

立即领取
服务端 / Nginx 教程 / Nginx 高级功能
Nginx br 压缩
Nginx 压缩和解压 Nginx 安全控制

Nginx br 压缩

压缩

Brotli 是 Google 研发的一种新兴压缩算法,可以加快网页的载入速度。相较于 Gzip 压缩率更高、性能也非常好,再基于 Chrome 的市场支配地位,Brotli 便得到迅速普及。不过要注意,它仅适用于 HTTPS。

Can I use brotli.png

1) 下载 Brotli 的源码;

yum install git && cd /usr/local/src

git clone https://github.com/google/ngx_brotli.git
pushd ngx_brotli
git submodule update --init
popd

2) 执行命令 nginx -V,configure arguments 即为现有的参数;

nginx version: nginx/1.16.1
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-36) (GCC) 
built with OpenSSL 1.1.1c  28 May 2019
TLS SNI support enabled
configure arguments: --with-http_ssl_module --with-http_v2_module --with-http_sub_module --with-openssl=../openssl-1.1.1c

3) 追加参数 --add-module=../ngx_brotli,重新编译 Nginx。本系列文中 Nginx 的源码目录是 /usr/local/src/nginx-1.16.1;

cd nginx-1.16.1
./configure \
--with-http_ssl_module \
--with-http_v2_module \
--with-http_sub_module \
--with-openssl=../openssl-1.1.1c \
--add-module=../ngx_brotli
make && make install

如需执行平滑升级 (热部署),make 之后请不要 make install:

mv /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.old
cp objs/nginx /usr/local/nginx/sbin/nginx
make upgrade

4) 接着修改 nginx.conf (缺省值就够用,篇幅所限不显式设置。不妨再留意一下 gzip_types 和 brotli_types 指令,以允许压缩 text/html 以外的文件,MIME 类型列表见);

http {
    ...
    gzip on;
    brotli on;
    
    # gzip_types text/css text/javascript application/rss+xml;
    # brotli_types text/css text/javascript application/rss+xml;
    ...
}

5) 重载 Nginx,可借由 Brotli Test 等工具检验效果。

nginx -t && nginx -s reload
Nginx 压缩和解压 Nginx 安全控制
下一篇:Nginx 安全控制