首先安装必备的模块
默认安装的 Nginx 在大部分情况下都是足够使用的,但有的时候我们还需要其他一些高级的东西,这个时候就需要编译安装啦。这个部分比较繁琐,纯小白不看也没关系。
东西比较多,我们创建一个文件夹单独存放,所有的操作都在该文件夹下操作。
apt update && apt full-upgrade -y && apt install cmake build-essential autoconf libtool automake libpcre3-dev libssl-dev zlib1g-dev libxml2-dev libbz2-dev libxslt1-dev libgd-dev libcurl4-openssl-dev libperl-dev libatomic-ops-dev libreadline-dev libncurses5-dev flex bison doxygen libffi-dev python-dev python-pip libfuzzy-dev libyajl-dev dh-autoreconf libpcre++-dev uuid-dev liblua5.3-dev ssl-cert libluajit-5.1-dev dpkg-dev debhelper po-debconf libexpat1-dev libgeoip-dev libhiredis-dev libmhash-dev libpam0g-dev git-buildpackage apg quilt -y
OPENSSL 库的选择
选用 OpenSSL 1.1.1 rfc 版本
OpenSSL 1.1.1
rfc 版已经支持TLSv1.3-rfc
,目前正式发布。
git clone https://github.com/S8Cloud/sslpatch.git
git clone -b OpenSSL_1_1_1-stable https://github.com/openssl/openssl && cd openssl
patch -p1 < ../sslpatch/OpenSSL1.1.1-tls1.3.patch
Patch 的作用为:在移动设备上优先使用 Chacha 20
系列加密, 添加 TLS 1.3 draft
版本,让 Nginx 可以指定 TLS 1.3 cipher
在 Nginx 配置中类似如下的写法:
ssl_protocols TLSv1.3 TLSv1.2;
ssl_ciphers TLS_AES_128_GCM_SHA256:TLS_CHACHA20_POLY1305_SHA256:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-ECDSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-RSA-CHACHA20-POLY1305:ECDHE-RSA-AES128-SHA256:ECDHE-RSA-AES128-SHA;
ssl_ecdh_curve X25519:P-256;
ssl_prefer_server_ciphers on;
选用 Patched OpenSSL 1.0.2(不再推荐使用)
git clone https://github.com/S8Cloud/sslpatch.git
git clone -b OpenSSL_1_0_2-stable https://github.com/openssl/openssl && cd openssl
patch -p1 < ../sslpatch/OpenSSL1.0.2o-chacha20-poly1305-draft.patch
./config && cd ..
这边 patch 的作用是添加了 draft-chacha20
加密方式,因为主流的 ssl 库都取消了 draft-chacha20
的支持,然而很多安卓浏览器(版本低于 Android 7)都只支持 draft-chacha20
加密,为了兼容这类浏览器,建议使用此 patch。
必须要注意的是,openssl 1.0.2p
不支持 TLSv1.3
与 x25519
,但是仍然保留了对于老旧加密方式较好的支持,适用于兼容性较强的场合。可以在 Nginx 配置中类似如下的写法:
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers 'ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-ECDSA-CHACHA20-draft-POLY1305:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA:ECDHE-RSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-draft-POLY1305:
ECDHE-RSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-SHA256:ECDHE-RSA-AES128-SHA:DHE-RSA-AES128-SHA:AES128-SHA';
ssl_ecdh_curve prime256v1:secp384r1;
ssl_prefer_server_ciphers on;
选用 Patched OpenSSL 1.1.0(不再推荐使用)
git clone https://github.com/S8Cloud/sslpatch.git
git clone -b OpenSSL_1_1_0-stable https://github.com/openssl/openssl && cd openssl
patch -p1 < ../sslpatch/OpenSSL1.1.0h-improve-ECDSA-sign-30-40%25.patch
这边打的 patch
作用是加快 ECDSA
解码的速度。实际上 OpenSSL 1.1.0
版本是一个妥协的版本,支持了一部分新特性如 X25519
之类的。但是仍然缺乏更多新的特性,1.1.1
才是一个长期的稳定版本。推荐的加密方式如下:
ssl_protocols TLSv1.1 TLSv1.2;
ssl_ciphers 'ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA:ECDHE-RSA-CHACHA20-POLY1305:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-SHA256:ECDHE-RSA-AES128-SHA:DHE-RSA-AES128-SHA:AES128-SHA';
ssl_ecdh_curve X25519:P-256:P-384;
ssl_prefer_server_ciphers on;
Nginx 的一些模块
Zlib 库
- 这里选择了 Cloudflare 魔改版的 zlib,显著提高了性能,仅仅适用于 Nginx/PHP 相关的应用,不适合替换系统的 zlib 库
git clone https://github.com/cloudflare/zlib && cd zlib && ./configure --64 && cd ..
Pcre 库
wget -c https://ftp.pcre.org/pub/pcre/pcre-8.42.zip && unzip pcre-8.42.zip && rm pcre-8.42.zip && cd pcre-8.42
./configure --enable-utf8 --enable-unicode-properties --enable-pcre16 --enable-pcre32 --enable-pcregrep-libz --enable-pcregrep-libbz2 --enable-pcretest-libreadline --enable-jit
make && make install
Brotli 模块
- Google 开发的网页压缩模块,相比 Gzip 压缩率提升显著,选取 Google 员工维护的最新版
git clone https://github.com/eustas/ngx_brotli.git
cd ngx_brotli && git submodule update --init --recursive && cd ..
Pagespeed 模块
- Google 推出的网站加速模块,主要做了 html、js、css 合并压缩,图片压缩懒加载等等功能,如果你纯单机部署不对静态文件使用 cdn 等话用处还是蛮大的。
git clone https://github.com/apache/incubator-pagespeed-ngx.git pagespeed && cd incubator-pagespeed-ngx
wget -c https://dl.google.com/dl/page-speed/psol/1.13.35.2-x64.tar.gz && tar -xzvf 1.13.35.2-x64.tar.gz && rm 1.13.35.2-x64.tar.gz && cd ..
Jemalloc 内存压缩模块
- 选取
5.1.0
最新版本显著增强性能
wget -c https://github.com/jemalloc/jemalloc/releases/download/5.1.0/jemalloc-5.1.0.tar.bz2 && tar xjf jemalloc-5.1.0.tar.bz2 && rm jemalloc-5.1.0.tar.bz2 && cd jemalloc-5.1.0 && ./configure
make -j2 && make install
echo '/usr/local/lib' > /etc/ld.so.conf.d/local.conf
ldconfig && cd ../
CT 模块
如果你的证书支持 Certificate Transparency
那么就不需要编译该模块,目前绝大多数证书都自带该特性,如果没有特殊需求,不需要再编译
- 选用
master
最新版以支持 TLSv1.3
git clone https://github.com/grahamedgecombe/nginx-ct.git
Devel Kit 模块
- 扩展了 nginx 的模块引擎,很多模块基于此开发
git clone https://github.com/simplresty/ngx_devel_kit.git
Headers More 模块
- 用于隐藏和修改各种服务端标识,推荐安装
git clone https://github.com/openresty/headers-more-nginx-module.git
Cache Purge 模块
- 清理服务端缓存,原版已经很久没有更新,这里选择
nginx-modules
接手的版本
git clone https://github.com/nginx-modules/ngx_cache_purge.git
Substitutions Filter 模块
- 反代网站必备
git clone https://github.com/yaoweibin/ngx_http_substitutions_filter_module.git
ModSecurity 模块
- WAF 防火墙模块,编译时间很长,运行也会消耗一些性能。
- 更新到 V3 正式版,安装方法和 V2 版有所不同。
pip install pip -U && pip install ssdeep
git clone -b v3/twaf/v3.0.2 --recursive https://github.com/SpiderLabs/ModSecurity.git
git clone https://github.com/SpiderLabs/ModSecurity-nginx.git
cd ModSecurity && git submodule update --init --recursive
./build.sh && ./configure
make && make install
NJS 模块
- Nginx 开发的基于
ECMAScript 5.1
的 JavaScript 脚本模块
git clone https://github.com/nginx/njs.git
LUA 模块
用 lua 脚本扩展 nginx,非常强大的模块,如果重度依赖 lua-nginx 不妨使用openresty
wget https://github.com/openresty/luajit2/archive/v2.1-20181029.tar.gz && tar zxf v2.1-20181029.tar.gz && cd luajit2-2.1-*
make && make install
cat > /etc/profile.d/luajit.sh << EOF
export LUAJIT_LIB=/usr/local/lib
export LUAJIT_INC=/usr/local/include/luajit-2.1/
EOF
source /etc/profile && cd ..
wget https://github.com/openresty/lua-nginx-module/archive/v0.10.14rc1.tar.gz && tar zxf v0.10.14rc1.tar.gz && rm *.tar.gz
Echo 模块
- 用于在网页输出一些简单信息,调试比较实用
git clone https://github.com/openresty/echo-nginx-module.git
GeoIP2 模块
- Nginx 包中自带了比较老的
GeoIP
模块,在编译参数中添加--with-http_geoip_module --with-stream_geoip_module
即可。 - 但是这个老的
MaxMind GeoIP
库即将停止维护,新的库对应的模块即为GeoIP2
add-apt-repository ppa:maxmind/ppa
apt update && apt install -y libmaxminddb0 libmaxminddb-dev mmdb-bin
git clone https://github.com/leev/ngx_http_geoip2_module.git
Auth PAM 模块
- 将 PAM 用做 nginx
simple http authentication
增强安全性
git clone https://github.com/sto/ngx_http_auth_pam_module.git
VTS 模块
- 更加详细的 nginx 统计与分析模块
git clone https://github.com/vozlt/nginx-module-vts.git
Upstream Fair 模块
- 非常古老的模块,用于负载均衡的,现在基本上用处不大了,不建议安装
git clone https://github.com/nginx-modules/nginx-upstream-fair.git
Upload Progress 模块
- 用于标示与 Upstrem post 上传状态的模块
git clone https://github.com/masterzen/nginx-upload-progress-module.git
DavExt 模块
- 用于强化 nginx 自带
WebDAV
组件的模块
git clone https://github.com/arut/nginx-dav-ext-module.git
RTMP 模块
- 做流媒体播放必备模块
git clone https://github.com/arut/nginx-rtmp-module.git
FancyIndex 模块
- 实现比较好看的目录索引功能,实际上也不算多好看
git clone https://github.com/aperezdc/ngx-fancyindex.git
编译安装 Nginx
下载 Nginx 包并打上 Cloudflare 的 patch
git clone https://github.com/nginx/nginx.git&& cd nginx && cp auto/configure .
patch -p1 < ../sslpatch/nginx_dynamic_tls_records_and_http2_hpack.patch
根据需求修改 Nginx 编译参数,然后编译安装
这边将大部分模块安装成 dynamic module
的形式,在调用的时候在 nginx.conf
中添加 load_module modules/xxx-module.so
即可
./configure --build=nginx-dcc --with-cc-opt='-g -O3 -m64 -march=native -ffast-math -DTCP_FASTOPEN=23 -fstack-protector-strong -flto -ffat-lto-objects -fuse-ld=gold --param=ssp-buffer-size=4 -Wformat -Werror=format-security -Wno-unused-parameter -fno-strict-aliasing -fPIC -D_FORTIFY_SOURCE=2 -gsplit-dwarf' --with-ld-opt='-lrt -ljemalloc -Wl,-Bsymbolic-functions -Wl,-z,relro -Wl,-z,now -fPIC' \
--prefix=/usr/share/nginx --sbin-path=/usr/sbin/nginx --conf-path=/etc/nginx/nginx.conf --http-log-path=/var/log/nginx/access.log --error-log-path=/var/log/nginx/error.log --lock-path=/var/lock/nginx.lock --pid-path=/run/nginx.pid --modules-path=/usr/lib/nginx/modules --http-client-body-temp-path=/var/lib/nginx/body --http-fastcgi-temp-path=/var/lib/nginx/fastcgi --http-proxy-temp-path=/var/lib/nginx/proxy --http-scgi-temp-path=/var/lib/nginx/scgi --http-uwsgi-temp-path=/var/lib/nginx/uwsgi \
--user=www-data --group=www-data \
--with-compat --with-file-aio --with-threads --with-poll_module --with-select_module --with-http_ssl_module --with-http_v2_module --with-http_v2_hpack_enc \
--with-http_realip_module --with-http_addition_module --with-http_xslt_module --with-http_image_filter_module --with-http_sub_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_auth_request_module --with-http_degradation_module --with-http_secure_link_module --with-http_slice_module --with-http_stub_status_module --with-http_dav_module --with-http_mp4_module --with-http_random_index_module \
--with-stream --with-stream_ssl_module --with-stream_realip_module --with-stream_ssl_preread_module \
--without-mail_imap_module --without-mail_pop3_module --without-mail_smtp_module \
--with-libatomic --with-zlib=../zlib --with-pcre --with-pcre-jit \
--with-openssl=../openssl --with-openssl-opt='enable-ec_nistp_64_gcc_128 zlib enable-egd threads -march=native -ljemalloc -Wl,-flto' --add-module=../ngx_brotli --add-module=../njs/nginx --add-module=../ngx_cache_purge --add-module=../headers-more-nginx-module --add-module=../ModSecurity-nginx --add-module=../pagespeed --add-module=../ngx_http_substitutions_filter_module
make -j $(nproc) && make install
查看 Nginx 版本和参数和安装目录
nginx -V
whereis nginx
创建 Nginx 系统服务
mkdir -p /var/lib/nginx/body
vi /lib/systemd/system/nginx.service
如下填写:
[Unit]
Description=A high performance web server and a reverse proxy server
After=syslog.target network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
PIDFile=/run/nginx.pid
ExecStartPre=/usr/sbin/nginx -t
ExecStart=/usr/sbin/nginx
ExecReload=/usr/sbin/nginx -s reload
ExecStop=/bin/kill -s QUIT $MAINPID
TimeoutStopSec=5
PrivateTmp=true
KillMode=mixed
[Install]
WantedBy=multi-user.target
开启 Nginx 服务
sudo systemctl enable nginx.service
sudo systemctl start nginx.service