您的位置:首页 > 运维架构 > Nginx

nginx升级及遇到的问题

2013-11-21 15:45 295 查看
Nginx于11月20日通过官方邮件列表更新了一个安全漏洞,该漏洞影响Nginx 0.8.41 至 1.5.6版本,攻击者可以通过该漏洞绕过Nginx的部分访问策略控制或通过上传功能向服务器植入后门。

漏洞描述

当攻击通过例如下列方式进行 URL 访问限制的时候,如果攻击者使用一些没经过转义的空格字符(无效的 HTTP 协议,但从 Nginx 0.8.41 开始因为考虑兼容性的问题予以支持)那么这个限制可能无效:

location /protected/ {
deny all;
}

当请求是 "/foo /../protected/file" 这样的 URL (静态文件,但 foo 后面有一个空格结尾) 或者是如下的配置时:

location ~ \.php$ {
fastcgi_pass ...
}

当攻击者请求 "/file \0.php" 时就会绕过限制。

该问题已经在 Nginx 1.5.7 和 1.4.4 版本中修复。

修复方式
1:源码方式: Nginx官方已经更新该漏洞的源码补丁,请通过更新源码来修复。补丁地址:http://nginx.org/download/patch.2013.space.txt
2:升级Nginx:将Nginx升级到1.5.7和1.4.4版本
3:临时补丁:在配置中添加以下规则,来禁止带有空格字符的访问。
if ($request_uri ~ " ") {
return 444;
}

--------------------------------------------------
安全漏洞处理指南:常见漏洞处理指南应用安全漏洞检查项及常见漏洞修复方案常见漏洞培训视频
安全配置及开发指南:http://security.sohu-inc.com/zhidu.php

平滑升级完全参照张宴的nginx书操作,之前需要查看现有nginx版本、编译安装的参数、进程数据等;
查看版本、编译参数:
[root@test shell]# /usr/local/nginx/sbin/nginx -V
nginx version: nginx/1.2.7
built by gcc 4.1.2 20080704 (Red Hat 4.1.2-54)
TLS SNI support disabled
configure arguments: --with-http_stub_status_module --with-http_ssl_module --with-http_realip_module

进程数据:
[root@test shell]# ps -ef |egrep -egrep|egrep nginx
root 14859 1 0 Oct22 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
appusr 16499 14859 0 Oct22 ? 00:00:00 nginx: worker process
appusr 16500 14859 0 Oct22 ? 00:00:00 nginx: worker process
appusr 16501 14859 0 Oct22 ? 00:00:00 nginx: worker process
appusr 16502 14859 0 Oct22 ? 00:00:00 nginx: worker process

下面进行平滑升级操作:
1、下载想要升级nginx版本,然后编译覆盖安装(默认新的nginx将继续安装在旧的nginx目录下)。为了体现出升级的不同我在编译时还增加了关于user、group的定义。

wget http://nginx.org/download/nginx-1.5.6.tar.gz tar -zvxf nginx-1.5.6.tar.gz
cd nginx-1.5.6
./configure --user=appusr --group=appusr --with-http_stub_status_module --with-http_ssl_module --with-http_realip_module && make && make install

2、执行
[root@test nginx-1.5.6]# kill -USR2 14859 【老版本的Nginx主进程号】

3、旧版本 nginx 的主进程将重命名它的 pid 文件例如 .oldbin(如:/usr/local/nginx/logs/nginx.pid.oldbin)

4、此时,新旧两个版本的nginx进程都在运行中(默认,新版本的nginx在安装后就自动运行。)此时运行 kill -WINCH 【老版本的Nginx主进程号】 使老版本的nginx 的worker process 逐步结束;
[root@test shell]# kill -WINCH 14859
[root@test shell]# ps -ef |egrep nginx
root 4906 14859 0 11:19 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
appusr 4907 4906 6 11:19 ? 00:00:04 nginx: worker process
appusr 4908 4906 6 11:19 ? 00:00:04 nginx: worker process
appusr 4909 4906 3 11:19 ? 00:00:02 nginx: worker process
appusr 4910 4906 6 11:19 ? 00:00:04 nginx: worker process
root 4978 29465 0 11:20 pts/0 00:00:00 egrep nginx
root 14859 1 0 Oct22 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
appusr 16499 14859 0 Oct22 ? 00:00:25 nginx: worker process is shutting down
appusr 16500 14859 0 Oct22 ? 00:00:27 nginx: worker process is shutting down
appusr 16502 14859 0 Oct22 ? 00:00:27 nginx: worker process is shutting down

可以看到nginx: worker process isshutting down,说明老版本的nginx worker process正在逐步关闭。

批量升级时,也可以制作成rpm包,放到yum源,附脚本
for i in `cat host.gti`;do ssh ${i} bash < nginx_bak.sh;done #!/bin/bash
source /etc/profile
DATE=`date "+%Y%m%d-%H%M%S"`;
BACKUP_DIR="/opt/backup/nginx/${DATE}"
PID=`ps -ef | grep nginx | grep root | grep -v grep | awk '{print $2}'`
TEMP_DIR=`mktemp -u`

mkdir -p ${BACKUP_DIR}
/bin/cp -r -p /usr/local/nginx ${BACKUP_DIR}

mkdir ${TEMP_DIR}
/bin/cp -r -p /usr/local/nginx/conf ${TEMP_DIR}/
yum clean all
yum install nginx-1.4.4-2 -y
/bin/cp -r -p ${TEMP_DIR}/conf /usr/local/nginx/
echo ${PID}
kill -USR2 ${PID} && sleep 3 && kill -WINCH ${PID}
ps -ef | grep nginx | grep -v 'grep'

#kill -QUIT ${PID}

下面是我遇到的问题:
1、configure时没有报错,make时碰到如下错误
error: ‘ngx_connection_t’ has no member named ‘single_connection’
/opt/soft/wiznmp/src

aoslawful-lua-nginx-module-7ee528b/src

x_http_lua_socket_tcp.c: In function ‘ngx_http_lua_socket_tcp_connect’:
/opt/soft/wiznmp/src

aoslawful-lua-nginx-module-7ee528b/src

x_http_lua_socket_tcp.c:445: error: ‘ngx_connection_t’ has no member named ‘single_connection’
make[1]: *** [objs/addon/src

x_http_lua_socket_tcp.o] Error 1
make[1]: Leaving directory `/root

inx-1.4.4'
make: *** [build] Error 2

原因:是因为我的lua模块版本较旧,下载新版本即可,下载最新的 lua-nginx-module(在这个页面找:https://github.com/chaoslawful/lua-nginx-module/tags)
2、启动nginx时,报如下错误
unknown directive "set_real_ip_from" in /usr/local/nginx/conf/nginx.conf:5
原因:由于我以前添加过新的参数,而/usr/local/nginx/sbin/nginx -V没有显示我后来添加的模块,所以我升级时是按照查询出来的参数直接配置的,忘记了后来添加的参数,从新配置添加上 --with-http_realip_module
本文出自 “我的博客” 博客,请务必保留此出处http://lschao.blog.51cto.com/7814743/1329607
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: