admin avatar

重新编译nginx,以便支持最新的quic(http3)以及 brotli 压缩

🕠 by admin

重新编译nginx,以便支持最新的quic(http3)以及 brotli 压缩

目前nginx的管网主线版本以及包含了quic(http3)支持了,直接在线安装即可。。。

下面是安装nginx主线版本的方法,就不翻译了,大致上接触过Linux系统的应该看得懂

Install the prerequisites:

1
sudo apt install curl gnupg2 ca-certificates lsb-release debian-archive-keyring

Import an official nginx signing key so apt could verify the packages authenticity. Fetch the key:

1
2
curl https://nginx.org/keys/nginx_signing.key | gpg --dearmor \
     | sudo tee /usr/share/keyrings/nginx-archive-keyring.gpg /dev/null

Verify that the downloaded file contains the proper key:

1
gpg --dry-run --quiet --no-keyring --import --import-options import-show /usr/share/keyrings/nginx-archive-keyring.gpg

The output should contain the full fingerprint as follows: 573BFD6B3D8FBC641079A6ABABF5BD827BD9BF62

1
2
3
 pub   rsa2048 2011-08-19 [SC] [expires: 2024-06-14]
       573BFD6B3D8FBC641079A6ABABF5BD827BD9BF62
 uid                      nginx signing key <[email protected]

If the fingerprint is different, remove the file.

To set up the apt repository for stable nginx packages, run the following command:

1
2
3
echo "deb [signed-by=/usr/share/keyrings/nginx-archive-keyring.gpg] \
 http://nginx.org/packages/debian `lsb_release -cs` nginx" \
     | sudo tee /etc/apt/sources.list.d/nginx.list

上面的是稳定版本,需要使用下面的主线版本

If you would like to use mainline nginx packages, run the following command instead:

1
2
3
4
5
6
7
8
9
10
11
12
13
14

echo "deb [signed-by=/usr/share/keyrings/nginx-archive-keyring.gpg] \
 http://nginx.org/packages/mainline/debian `lsb_release -cs` nginx" \
     | sudo tee /etc/apt/sources.list.d/nginx.list
 ```


Set up repository pinning to prefer our packages over distribution-provided ones:
优先使用nginx的软件分发仓库安装,而不是从debian软件仓库安装。


 ```
echo -e "Package: *\nPin: origin nginx.org\nPin: release o=nginx\nPin-Priority: 900\n" \
     | sudo tee /etc/apt/preferences.d/99nginx

To install nginx, run the following commands:

1
2
sudo apt update
 sudo apt install nginx

参考: https://nginx.org/en/linux_packages.html

nginx 安装好了,但是不支持brotli 压缩,所以需要重新编译一遍nginx

编译安装nginx的必要环境

1
2
apt update
apt install build-essential ca-certificates zlib1g-dev libpcre3 libpcre3-dev tar unzip libssl-dev wget curl git cmake ninja-build mercurial libunwind-dev pkg-config gcc g++

支持brotli 压缩

1
2
3
4
git clone https://github.com/google/ngx_brotli.git
cd ngx_brotli
git submodule update --init
cd ..

下载nginx源代码,进入nginx源代码根目录,执行以下代码编译nginx,下面的编译参数实在虚拟机上Debian系统示例

请根据实际情况修改,执行nginx -V即可查看已安装nginx编译的参数

1
2
./configure --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/dev/shm/nginx.pid --lock-path=/dev/shm/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=www-data --group=www-data --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-http_v3_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --add-module=/root/nginx/ngx_brotli --with-cc-opt='-g -O2 -ffile-prefix-map=/data/builder/debuild/nginx-1.25.1/debian/debuild-base/nginx-1.25.1=. -fstack-protector-strong -Wformat -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -Wl,--as-needed -pie'

重点,重点,重点

执行make,而不是make install否则会覆盖系统已经安装的nginx包括参数等等。

执行make,没有报错的nginx就编译好了,保存在nginx根目录下的objs目录下

把objs目录下nginx替换到实际运行的nginx目录即可,

执行nginx -v如果出现下面代码即可

1
--add-module=/root/nginx/ngx_brotli

添加brotli 压缩代码,重载nginx没有报错即可

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
gzip on;
  gzip_buffers 16 8k;
  gzip_comp_level 6;
  gzip_http_version 1.1;
  gzip_min_length 256;
  gzip_proxied any;
  gzip_vary on;
  gzip_types
    text/xml application/xml application/atom+xml application/rss+xml application/xhtml+xml image/svg+xml
    text/javascript application/javascript application/x-javascript
    text/x-json application/json application/x-web-app-manifest+json
    text/css text/plain text/x-component
    font/opentype application/x-font-ttf application/vnd.ms-fontobject
    image/x-icon;
  gzip_disable "MSIE [1-6]\.(?!.*SV1)";

  ##Brotli Compression
  brotli on;
  brotli_comp_level 6;
  brotli_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript application/javascript image/svg+xml;

💘 相关文章

写一条评论