我的Nginx编译之旅

提醒:本文最后更新于 1549 天前,文中所描述的信息可能已发生改变,请仔细核实。

好久没写文章了,太忙了主要是。

还是有一些人好奇我的Nginx怎么编译安装,所以作为除草,简单写一下吧。

以下内容基于CentOS 7 x64,root权限。请先安装好基础的运行库,再尝试下一步。

老司机,请直接看编译完的参数,文章就没必要看了。

截止至文章发布前,Nginx -V输出:

nginx version: nginx/1.15.5
built by gcc 7.3.1 20180303 (Red Hat 7.3.1-5) (GCC)
built with OpenSSL 1.1.2-dev  xx XXX xxxx
TLS SNI support enabled
configure arguments: --user=www --group=www --prefix=/usr/local/nginx --http-client-body-temp-path=/tmp/nginx_client_body --http-proxy-temp-path=/tmp/nginx_proxy --http-fastcgi-temp-path=/tmp/nginx_fastcgi --without-http_ssi_module --without-http_scgi_module --without-http_uwsgi_module --with-http_stub_status_module --with-http_ssl_module --with-http_spdy_module --with-http_v2_module --with-http_v2_hpack_enc --with-http_gzip_static_module --with-http_realip_module --with-libatomic --with-pcre=/data/src/pcre-8.42 --with-pcre-jit --with-threads --with-file-aio --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-ld-opt='-Wl,-z,relro -Wl,-z,now -fPIC -ljemalloc -lrt' --with-cc-opt='-m64 -O3 -g -DTCP_FASTOPEN=23 -ffast-math -march=native -flto -fstack-protector-strong -fuse-ld=gold --param=ssp-buffer-size=4 -Wformat -Werror=format-security -fno-strict-aliasing -fPIC -Wdate-time -Wp,-D_FORTIFY_SOURCE=2' --with-zlib=/data/src/zlib-cf --add-module=/data/src/nginx-ct --add-module=/data/src/ngx_brotli --add-module=/data/src/ngx_devel_kit --add-module=/data/src/headers-more-nginx-module --add-module=/data/src/lua-nginx-module --with-openssl=/data/src/openssl --with-openssl-opt='zlib enable-tls1_3 enable-weak-ssl-ciphers enable-ec_nistp_64_gcc_128 -march=native -ljemalloc -Wl,-flto'

首先,请仔细阅读上面编译命令的每一个参数,每个参数都有它的意义。我是根据自己的需求来的,你也一样,需要这样做。

从模块选择上来说,不要照搬,选择符合自己需求的去编译。

再次重复,请按照自己实际需求的去编译安装。不然你编译安装干嘛,直接包管理安装就完事了。

我根据实际需求,选用了:

zlib (Cloudflare优化版本):

cd $cur_dir
git clone https://github.com/cloudflare/zlib.git zlib-cf
cd zlib-cf
make -f Makefile.in distclean

Certificate Transparency (CT)

cd $cur_dir
git clone https://github.com/grahamedgecombe/nginx-ct.git

Brotli (eustas版本):

cd $cur_dir
git clone https://github.com/eustas/ngx_brotli.git
cd ngx_brotli
git submodule update --init --recursive

Nginx Devel Kit (NDK)

cd $cur_dir
git clone https://github.com/simpl/ngx_devel_kit.git

Nginx Headers More

cd $cur_dir
git clone https://github.com/openresty/headers-more-nginx-module.git

Lua

cd $cur_dir
git clone https://github.com/openresty/lua-nginx-module.git

除此外,还使用了最新版本的PCRElibatomic_opsjemallocLuaJIT等,请自行下载并安装,详情不再表。

而对于OpenSSL,则选用了最新的开发版本(1.1.2-dev),并打上由hakasenyang制作的补丁。

补丁使得TLSv1.3能支持draft 23、draft 26、draft 27、draft 28和Final(RFC)版本,以及类似BoringSSL等价加密算法组支持,另外还有ChaCha20 Draft支持。详情可以查看他的Github(文章末尾)。

cd $cur_dir
rm -rf openssl
git clone https://github.com/openssl/openssl.git
cd openssl
git submodule update --init --recursive
curl https://raw.githubusercontent.com/hakasenyang/openssl-patch/master/openssl-equal-1.1.2-dev_ciphers.patch | patch -p1
curl https://raw.githubusercontent.com/hakasenyang/openssl-patch/master/openssl-1.1.1-chacha_draft.patch | patch -p1

推荐使用的Nginx等价加密算法为:

[TLS13+AESGCM+AES128|TLS13+AESGCM+AES256|TLS13+CHACHA20]:[EECDH+ECDSA+AESGCM+AES128|EECDH+ECDSA+CHACHA20]:EECDH+ECDSA+AESGCM+AES256:EECDH+ECDSA+AES128+SHA:EECDH+ECDSA+AES256+SHA:[EECDH+aRSA+AESGCM+AES128|EECDH+aRSA+CHACHA20]:EECDH+aRSA+AESGCM+AES256:EECDH+aRSA+AES128+SHA:EECDH+aRSA+AES256+SHA:RSA+AES128+SHA:RSA+AES256+SHA:RSA+3DES

接下来就是编译Nginx了,我使用了自己做的Patch,以及使用OpenSSL库代替原生的md5及sha1库。

编译流程大概如下:

cd $cur_dir
wget -c http://nginx.org/download/nginx-1.15.5.tar.gz
tar zxvf nginx-1.15.5.tar.gz && cd nginx-1.15.5/
curl https://raw.githubusercontent.com/kn007/patch/master/nginx.patch | patch -p1
curl https://gist.github.com/CarterLi/f6e21d4749984a255edc7b358b44bf58/raw/4a7ad66a9a29ffade34d824549ed663bc4b5ac98/use_openssl_md5_sha1.diff | patch -p1
./configure --user=www --group=www --prefix=/usr/local/nginx --http-client-body-temp-path=/tmp/nginx_client_body --http-proxy-temp-path=/tmp/nginx_proxy --http-fastcgi-temp-path=/tmp/nginx_fastcgi --without-http_ssi_module --without-http_scgi_module --without-http_uwsgi_module --with-http_stub_status_module --with-http_ssl_module --with-http_spdy_module --with-http_v2_module --with-http_v2_hpack_enc --with-http_gzip_static_module --with-http_realip_module --with-libatomic --with-pcre=$cur_dir/pcre-8.42 --with-pcre-jit --with-threads --with-file-aio --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-ld-opt='-Wl,-z,relro -Wl,-z,now -fPIC -ljemalloc -lrt' --with-cc-opt='-m64 -O3 -g -DTCP_FASTOPEN=23 -ffast-math -march=native -flto -fstack-protector-strong -fuse-ld=gold --param=ssp-buffer-size=4 -Wformat -Werror=format-security -fno-strict-aliasing -fPIC -Wdate-time -Wp,-D_FORTIFY_SOURCE=2' --with-zlib=$cur_dir/zlib-cf --add-module=$cur_dir/nginx-ct --add-module=$cur_dir/ngx_brotli --add-module=$cur_dir/ngx_devel_kit --add-module=$cur_dir/headers-more-nginx-module --add-module=$cur_dir/lua-nginx-module --with-openssl=$cur_dir/openssl --with-openssl-opt='zlib enable-tls1_3 enable-weak-ssl-ciphers enable-ec_nistp_64_gcc_128 -march=native -ljemalloc -Wl,-flto'
make -j$MakeThreads
make install

如果你是升级Nginx,那么可以在make install后,执行make upgrade来重启Nginx完成更新。

以上代码,提前定义cur_dir的位置,可以合理规划好安装文件存储位置;定义MakeThreads指定编译时线程数,大家按照实际情况定就好了。

顺带的,送一份Systemd的系统服务注册给大家。

cat > /usr/lib/systemd/system/nginx.service <<EOF
[Unit]
Description=A high performance web server and a reverse proxy server
After=syslog.target network.target network.service

[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s quit
Restart=on-abort

[Install]
WantedBy=multi-user.target
EOF

启用它,让Nginx可以开机自启动:

systemctl daemon-reload
systemctl enable nginx
systemctl start nginx

以上就是本次编译之旅的全部内容了。

本期内容涵盖的几个相关传送门:

1. 也聊博客TLS 1.3

2. kn007/patch

3. hakasenyang/openssl-patch

4. Backport some (should be safe) changes from google nginx


文章可能会不定期更新吧,可能吧。。我不确认。

再聊点别的吧,鉴于最近又拍云的节点十分不稳定,且图片处理功能经常爆炸。

目前博客已经全面从又拍云上退出,全部回源,暂时观望。

余额宁愿烂掉,也不用让人不放心的服务。

话说起来,Google Cloud Platform (GCP)被发现可以重复申请$300赠金(相同帐号,同一张信用卡)。实测确实如此,目前已经重新申请,但暂未创建实例。


根据与DCC的探讨,变更了编译函数。

其中Nginx的SSL_OP_PRIORITIZE_CHACHA优先补丁包没有意义,在hakasenyang的补丁中OpenSSL这一判断被删除了。来源传送门,所以删除这一Patch的修补。

转载请注明转自:kn007的个人博客的《我的Nginx编译之旅