kn007的个人博客
♥ You are here: Home > 软件与网络 > VPS > 我的Nginx编译之旅

我的Nginx编译之旅

by | 57 Comments

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

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

还是有一些人好奇我的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编译之旅

donate
有所帮助?

Comments

57 Comments立即评论
  1. 厉害呀!这么多的模块呀?我现在都不敢折腾Nginx的模块了!

    1. MOD回复

      @明月登楼: 我这很少了,按需编译。

  2. 膜拜大神了! :x

  3. LV2回复

    默默地看了下我服务器上的内核版本,低的让人不忍心看 :sad:

    1. MOD回复

      @公子: :grin: 没事,稳定也是一种需求

  4. nginx-ct真的还必要吗……证书不内嵌? :grin:

    1. MOD回复

      @云间守望: 内嵌了啊。但是喜欢再补充一个sct :oops:

  5. 居然编译了那么多东西进去 ...

    1. MOD回复

      @老谢: 还好吧,都是需要的东西。

    2. @kn007: 研究大佬的编译参数中... :!: :!: :!:

    3. MOD回复

      @老谢: :oops: 欢迎~

  6. 回复

    大神你好
    请帮忙解答一个小小的问题.我在编译nginx时同时把"--with-ld-opt="和"--with-cc-opt=" 参数添加后,Nginx编辑会报 "--with-ld-opt=" 参数找不到.谢谢!

    1. MOD回复

      @Maniac: 更新GCC版本。

  7. LV2回复

    :oops:

  8. 回复

    ./configure --sbin-path=/usr/local/sbin --conf-path=/etc/nginx/nginx.conf --pid-path=/var/run/nginx.pid --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --http-client-body-temp-path=/var/cache/nginx/nginx_client --http-proxy-temp-path=/var/cache/nginx/nginx_proxy --http-fastcgi-temp-path=/var/cache/nginx/nginx_fastcgi --with-pcre=/usr/local/src/pcre-8.42 --with-openssl=/usr/local/src/openssl --add-module=/usr/local/src/headers-more-nginx-module --add-module=/usr/local/src/ngx_brotli --with-zlib=/usr/local/src/zlib-cf --add-module=/usr/local/src/ngx_devel_kit --without-http_ssi_module --without-http_scgi_module --without-http_uwsgi_module --with-http_ssl_module --with-http_v2_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-http_v2_hpack_enc --with-file-aio --with-threads --with-pcre-jit --with-http_gzip_static_module --with-http_stub_status_module --with-http_realip_module --with-stream_realip_module --with-http_spdy_module --with-stream --with-libatomic --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 -gsplit-dwarf' --with-openssl-opt='zlib enable-tls1_3 enable-weak-ssl-ciphers enable-ec_nistp_64_gcc_128 -march=native -ljemalloc -Wl,-flto'

    ive got error like this :
    checking for OS
    + Linux 3.10.0-957.1.3.el7.x86_64 x86_64
    checking for C compiler ... found
    + using GNU C compiler
    + gcc version: 4.8.5 20150623 (Red Hat 4.8.5-36) (GCC)
    checking for gcc -pipe switch ... found
    checking for --with-ld-opt="-Wl,-z,relro -Wl,-z,now -fPIC -ljemalloc -lrt" ... not found
    ./configure: error: the invalid value in --with-ld-opt="-Wl,-z,relro -Wl,-z,now -fPIC -ljemalloc -lrt"

    1. MOD回复

      @karen: using the latest GCC version

    2. 回复

      @kn007: 升级到gcc 9了,还是报错。然后百度又谷歌,最后yum 安装jemalloc却编译通过。请问这是什么情况呢?另外怎么升级jemalloc?因为yum安装的是3.6版的jemalloc :|

    3. MOD回复

      @山: 编译

    4. 回复

      @kn007: 找到原因了,configure没找到libjemalloc.so.2, 要创建软链接ln -s /usr/local/lib/libjemalloc.so.2 /usr/lib64/libjemalloc.so :cry:

    5. 回复

      @kn007: 另外,请问这里采用了cf的zlib,如果后面装了php和memcached,也要安装zlib,请问两个zlib是否会冲突? :|

    6. MOD回复

      @山: zlib是引用到nginx,不是覆盖系统。
      与其他软件无关

  9. 回复

    大佬,问个问题,怎么能让nginx设置限制IP请求每小时200次,rate=200r/h,官方只提供到m分钟

    1. MOD回复

      @sora: 可以通过patch来支持,你可以了解下,
      https://www.getpagespeed.com/nginx-mod-a-better-faster-nginx-build

    2. 回复

      @kn007: 我也有找到这个链接。yum源找不到文件
      No Match for argument: getpagespeed-release
      No package getpagespeed-release available.
      No packages marked for update

    3. MOD回复

      @sora: 你上nginx官方的反馈节点,我记得有个patch,但是肯定patch位置不对了,改改就好了。

    4. MOD回复

      @sora: 官方好像当时因为连接数上限判断原因,跟平台有关系。所以没搞。。不大记得了。
      你可以搜一下。

    5. MOD回复

      @sora: 另外你需要先添加源,才能引用源。

    6. 回复

      @kn007: 就是因为没找到需要添加的源,刚接触nginx没多久,不太懂大佬说的patch在哪。。

    7. MOD回复

      @sora: 你google一下。。。

    8. 回复

      @kn007: 感谢!我研究下

    9. 回复

      @sora: 大佬,问个问题,是不是就算按照小时,也是会被算成秒为单位,相当于限制200请求每小时,换成每18秒处理一个请求,我测试的感觉是这样,能不能实现一个小时内的计数限制

    10. MOD回复

      @sora: 好像从代码来是这样算的。
      可以,但是不要用nginx实现。其实nginx本身就不是做长时间limit的。
      你可以通过防火墙来实现

    11. 回复

      @kn007: 您好,我最近遇到个问题,就是我现在有一台A服务器能访问B服务器oracle数据库,但是还有好几台服务器访问不了B,我如何让几台服务器通过A服务器去访问B,有什么实现方法吗,我试过ngxin的端口转发,但是B不知道怎么设置的,用自己机器测试都可以实现访问,但是就是访问不了B,就是想问下您除了端口转发还有其他方式可以实现类似的功能吗?

    12. MOD回复

      @sora: iptables,或者一切可以redir的程序

    13. 回复

      @kn007: oracle做了IP限制,我现在有一台可以连上的ip,怎么利用这台服务器,我用nginx转发时,会把实际ip给转发出去。。。
      events {
      worker_connections 1024;
      }

      stream {
      upstream oracle{
      server 10.1.101.3:1521;
      }

      server {
      listen 1521;
      proxy_pass oracle;
      }
      }

    14. 回复

      @kn007: 想请教的就是编译的jemalloc没法通过nginx的编译 ,该咋办:cry:

    15. MOD回复

      @山: 指定位置,或者覆盖系统的jemalloc

icon_wink.gificon_neutral.gificon_mad.gificon_twisted.gificon_smile.gificon_eek.gificon_sad.gificon_rolleyes.gificon_razz.gificon_redface.gificon_surprised.gificon_mrgreen.gificon_lol.gificon_idea.gificon_biggrin.gificon_evil.gificon_cry.gificon_cool.gificon_arrow.gificon_confused.gificon_question.gificon_exclaim.gif