在Ubuntu 18 04上为nginx添加Brotli支持
Tianye
・10 分钟阅读
Brotli(br)是一种新的开源压缩算法,由Google开发,可替代Gzip,zopfli和Deflate。它在因特网工程任务(IETF )中正式定义为RFC 7932 ,Google对Brotli的案例研究显示,压缩比要比当前的方法小26%,CPU使用率更低。
nginx没有官方支持,但是,有一个由谷歌开发的第三方模块,名为ngx brotli git 。
本指南将向你展示,如何在新的Ubuntu 18.04 LTS实例上添加对Nginx的Brotli支持。
注意:本指南将使用johndoe作为例子用户和example.com作为例子域名,使用你的名字替换它们。
要求
- Ubuntu 18.04服务器
- Nginx版本1.11.5或更高版本
- 设置了
A
/AAAA
记录的域名 - TLS证书
开始之前
检查Ubuntu版本。
lsb_release -ds
# Ubuntu 18.04 LTS
使用sudo
访问创建一个新的non-root
用户帐户,并且切换到它。
adduser johndoe --gecos"John Doe"
usermod -aG sudo johndoe
su - johndoe
注:将johndoe
替换为你的用户名。
更新你的系统操作系统。
sudo apt update && sudo apt upgrade -y
设置时区。
sudo dpkg-reconfigure tzdata
安装必需的生成工具和包。
sudo apt install -y build-essential git apt-transport-https socat
步骤1安装acme sh,并且从let'sencrypt获取TLS证书
Brotli要求你设置并使用HTTPS ,在本部分中,我们将从let's Encrypt获得可信证书。
下载并安装Acme.sh 。
sudo mkdir /etc/letsencrypt
git clone https://github.com/Neilpang/acme.sh.git
cd acme.sh
sudo ./acme.sh --install --home /etc/letsencrypt --accountemail your_email@example.com
cd ~
source ~/.bashrc
检查版本。
acme.sh --version
# v2.8.0
获取example.com
的RSA和ECDSA证书。
# RSA 2048
sudo /etc/letsencrypt/acme.sh --issue --standalone --home /etc/letsencrypt -d example.com --accountemail your_email@example.com --ocsp-must-staple --keylength 2048
# ECDSA/ECC P-256
sudo /etc/letsencrypt/acme.sh --issue --standalone --home /etc/letsencrypt -d example.com --accountemail your_email@example.com --ocsp-must-staple --keylength ec-256
在运行上命令后,证书和密钥将位于以下位置:
- RSA :
/etc/letsencrypt/example.com
- ecc/ecdsa :
/etc/letsencrypt/example.com_ecc
步骤2从官方nginx存储库安装nginx
下载并安装来自官方Nginx repo的最新主线Nginx 。
wget https://nginx.org/keys/nginx_signing.key
sudo apt-key add nginx_signing.key
rm nginx_signing.key
sudo -s
printf"deb https://nginx.org/packages/mainline/ubuntu/ `lsb_release -sc` nginx ndeb-src https://nginx.org/packages/mainline/ubuntu/ `lsb_release -sc` nginx n" >> /etc/apt/sources.list.d/nginx_mainline.list
exit
sudo apt update
sudo apt install -y nginx nginx-module-geoip nginx-module-image-filter nginx-module-njs nginx-module-perl nginx-module-xslt
检查版本。
sudo nginx -v
# nginx version: nginx/1.15.2
启用和启动Nginx 。
sudo systemctl enable nginx.service
sudo systemctl start nginx.service
步骤3下载并编译Brotli源代码
安装Nginx之后,我们需要将Brotli模块(ngx_brotli
)构建为动态Nginx模块,从Nginx版本1.11.5可以编译单个动态模块而不需要编译完整的Nginx软件,在接下来的几个步骤中,我们将构建动态Brotli模块,而不用编译完整的Nginx。
下载最新版本的主线Nginx源代码,并且提取它。
wget https://nginx.org/download/nginx-1.15.2.tar.gz && tar zxvf nginx-1.15.2.tar.gz
注:Nginx包和Nginx源代码匹配的版本号非常重要,如果你从官方Nginx存储库安装了Nginx 1.15.2,则必须下载相同版本的源代码1.15.2.
删除nginx-1.15.2.tar.gz
。
rm nginx-1.15.2.tar.gz
从GitHub克隆ngx_brotli
。
git clone https://github.com/eustas/ngx_brotli.git
cd ngx_brotli && git submodule update --init && cd ~
导航到Nginx源代码目录。
cd ~/nginx-1.15.2
下载所需的库。
sudo apt install -y libpcre3 libpcre3-dev zlib1g zlib1g-dev openssl libssl-dev
将ngx_brotli
编译为动态模块,并将它复制到Nginx模块(/etc/nginx/modules
)的标准目录。
./configure --with-compat --add-dynamic-module=../ngx_brotli
make modules
sudo cp objs/*.so /etc/nginx/modules
在/etc/nginx/modules
中列出文件,你将看到ngx_http_brotli_filter_module.so
和ngx_http_brotli_static_module.so
。
ls /etc/nginx/modules
将所有.so
文件的权限设置为644
。
sudo chmod 644 /etc/nginx/modules/*.so
步骤4配置nginx
我们准备在Nginx中配置Brotli支持。
运行sudo vim/etc/nginx/nginx.conf
,并且在文件顶部添加以下两个指令以加载新的Brotli模块。
load_module modules/ngx_http_brotli_filter_module.so;
load_module modules/ngx_http_brotli_static_module.so;
测试配置。
sudo nginx -t
为example.com
创建文档root目录,并在其中创建有某些内容的index.html
。
sudo mkdir -p /var/www/example.com
sudo -s
echo"Hello from example.com" >> /var/www/example.com/index.html
exit
为example.com
创建虚拟主机。
sudo vim /etc/nginx/conf.d/example.com.conf
使用以下配置填充它。
server {
listen 80;
server_name example.com; # Replace with your domain name
return 301 https://$server_name$request_uri;
}
server {
listen 443 ssl http2;
server_name example.com; # Replace with your domain name
root /var/www/example.com; # Replace with your document root
# RSA
ssl_certificate /etc/letsencrypt/example.com/fullchain.cer;
ssl_certificate_key /etc/letsencrypt/example.com/example.com.key;
# ECDSA
ssl_certificate /etc/letsencrypt/example.com_ecc/fullchain.cer;
ssl_certificate_key /etc/letsencrypt/example.com_ecc/example.com.key;
brotli on;
brotli_static on;
brotli_types text/plain text/css text/javascript application/javascript text/xml application/xml image/svg+xml application/json;
}
测试配置。
sudo nginx -t
重新加载Nginx 。
sudo systemctl reload nginx.service
访问站点浏览器中的站点,并打开开发人员工具的网络选项卡,你将在响应头中看到Content-Encoding: br
,这是Brotli压缩工作的指示器。
你已在Web服务器上启用了Brotli压缩功能。