详细情况请参看:
Nginx敏感信息泄露漏洞 (CVE-2017-7529) 分析
目前可以修复的方案有三条
- 1.受影响的版本nginx 0.5.6 - 1.13.2全版本,所以升级到大于1.13.2 就可以解决敏感信息泄露的问题。
- 2.临时解决方案,在nginx.conf 中配置max_ranges 1 ; 对下载大文件,断点续传会有影响,请酌情使用。
- 3.使用第三方安全软件进行防御。
升级到Nginx 1.14.1 稳定版本
nginx 被爆出存在安全问题,有可能会致使 1400 多万台服务器易遭受 DoS 攻击(CVE-2018-16843,CVE-2018-16844)以及 MP4 模块(CVE-2018-16845)):
- Security: 在使用 HTTP/2 时可能导致客户端内存消耗过大 (CVE-2018-16843),CPU 使用率过高 (CVE-2018-16844);
- Security: 使用 ngx_http_mp4_module 处理特制的 mp4 文件可能导致工作进程内存泄露(CVE-2018-16845);
- Bugfix: 使用 gRPC 后端可能会导致内存过度消耗。
2018/11/06发布了新的版本,解决上面的问题,所以直接一步到位升级到最新的版本(Nginx 1.14.1 稳定版本)。
安装详情(lua-nginx-module )
安装模块
lua-nginx-module
nginx-module-vts
前期工作
安装依赖包,避免编译出错:
$ yum -y install zlib zlib-devel openssl openssl--devel pcre pcre-devel gcc g++ gcc-c++ gd-devel
因为现在我安装是nginx 1.14,所以下载的组件都比较新,旧版本好像有冲突。
下载模块(之前用到的lua依赖需要升级)
$ cd /usr/local/src/nginx114-module
$ wget http://luajit.org/download/LuaJIT-2.0.5.tar.gz #下载LuaJIT
$ wget https://github.com/simplresty/ngx_devel_kit/archive/v0.3.0.tar.gz #下载ngx_devle_kit
$ wget https://github.com/openresty/lua-nginx-module/archive/v0.10.13.tar.gz #下载lua_nginx_module
$ wget https://nginx.org/download/nginx-1.14.1.tar.gz
$ git clone https://github.com/vozlt/nginx-module-vts.git
$ git clone https://github.com/yaoweibin/nginx_upstream_check_module.git
解压并安装
$ tar xzf LuaJIT-2.0.5.tar.gz
$ tar xzf v0.3.0.tar.gz
$ tar xzf v0.10.13.tar.gz
$ tar -xzf nginx-1.14.1.tar.gz
$ tar -xzf openssl-1.0.2o.tar.gz
$ unzip nginx_upstream_check_module-master.zip
$ mv nginx_upstream_check_module-master nginx_upstream_check_module
$ cd LuaJIT-2.0.5
$ make && make install
$ export LUAJIT_LIB=/usr/local/lib
$ export LUAJIT_INC=/usr/local/include/luajit-2.0
添加后端应用健康检查模块
nginx_upstream_check_module为淘宝技术团队开发的nginx模快,用来检测后方server的健康状态,如果后端服务器不可用,则所以的请求不转发到这台服务器。
nginx是没有针对负载均衡后端节点的健康检查的,但是可以通过proxy_next_upstream来间接实现,但这个还是会把请求转发给故障服务器的,然后再转发给别的服务器,这样就浪费了一次转发。
项目git地址:https://github.com/yaoweibin/nginx_upstream_check_module
进入nginx源码目录,进行打该模块的补丁(这一步千万不能遗漏)
$ cd nginx-1.14.1
$ patch -p1 < ../nginx114-module/nginx_upstream_check_module/check_1.14.0+.patch
然后通过./configure --add-module来增加模块
./configure –add-module=../ nginx_upstream_check_module-master/
添加gzip_static模块
模块的作用就是在接到请求后,会到url相同的路径的文件系统去找扩展名为“.gz”的文件
--with-http_gzip_static_module
编译的时候直接添加即可
比如:http://www.iteye.com/stylesheets/homepage.css
nginx就会先查找 stylesheets/homepage.css.gz 这个文件,如果存在直接把它发送出去,如果不存在,再将stylesheets/homepage.css文件进行gzip压缩,再发送出去,这样可以避免重复的压缩无谓的消耗资源,这个模块不受gzip_types限制,会对所有请求有效。所以建议不要在全局上使用,因为一般来说大部分都是动态请求,是不会有.gz这个文件的,建议只在局部我们确认有.gz的目录中使用。
Nginx不会自动的将压缩结果写入文件系统,这点不同于lighttpd,所以如果想使用static_gzip模块,需要自己写脚本生成.gz文件。
而对于Rails3项目来说就很方便了,只需要针对assets目录启用gzip_static模块即可:
location ~ ^/(assets)/ {
root /path/to/public;
gzip_static on; # to serve pre-gzipped version
expires max;
add_header Cache-Control public;
}
编译安装nginx
$ cd nginx-1.14.1
$ make clean
$ ./configure --prefix=/usr/local/nginx --with-pcre --with-http_ssl_module --with-http_stub_status_module --with-http_gzip_static_module --with-http_realip_module --with-http_gzip_static_module --with-pcre-jit --with-ld-opt=-ljemalloc --with-ld-opt=-Wl,-rpath,/usr/local/luajit/lib --add-module=/usr/local/src/nginx114-module/lua-nginx-module-0.10.13 --add-module=/usr/local/src/nginx114-module/ngx_devel_kit-0.3.0 --add-module=/usr/local/src/nginx114-module/nginx-module-vts --add-module=/usr/local/src/nginx114-module/nginx_upstream_check_module --with-stream
$ make ##不要make install 不然会覆盖老版本执行程序
$ mv /usr/local/nginx/sbin/nginx{,_`date +%F`}
$ cp objs/nginx /usr/local/nginx/sbin
$ /usr/local/nginx/sbin/nginx -V ##如果报如下错误,可以手动链接lib库
/usr/local/nginx/sbin/nginx: error while loading shared libraries: libluajit-5.1.so.2: cannot open shared object file: No such file or directory
$ cat /etc/ld.so.conf.d/libc.conf ## 新建libc.conf 新增以下内容
/usr/local/lib
/usr/local/include/luajit-2.0
$ ldconfig
$ /usr/local/nginx/sbin/nginx -V
nginx version: nginx/1.14.1 ##升级成功啦
$ kill -USR2 $(cat /usr/local/nginx/logs/nginx.pid) ##使用新的二进制文件重新启动新的进程(此时新旧进程并存)
$ kill -WINCH $(cat /usr/local/nginx/logs/nginx.pid.oldbin ) ## 优雅退出
$ kill -QUIT $(cat /usr/local/nginx/logs/nginx.pid.oldbin) ##杀死旧的进程 kill使用参考https://blog.csdn.net/superhosts/article/details/8741227
然后在测试lua:
在/usr/local/nginx/conf/nginx.conf 加入
lua_shared_dict limit 20m;
lua_shared_dict jump 20m;
lua_code_cache on;
验证是否安装成功
/usr/local/nginx/conf/nginx.conf 加入以下代码
location /hello_lua {
default_type 'text/plain';
content_by_lua 'ngx.say("hello, lua")';
重启nginx(建议将进程杀死后重启:)
/usr/local/nginx/sbin/nginx -s reload
$ curl 10.xxx.xx.xx/hello_lua
hello, lua