Nginx返回响应码411

问题

最近在迁移即将到期的虚拟机,虚拟机上面跑着Nginx对外提供服务。在切换到新的虚拟机后,有业务方反映上传图片失败,Nginx返回响应码411.

第一次遇到这种问题,在网上搜索一番后发现是低版本Nginx存在的问题。新的虚拟机上面的Nginx是通过yum源安装的,版本为1.0.15. 该版本的Nginx在处理 POST 方式上传文件的时候会触发该问题,给客户端返回响应码411.

解决方案

OK,知道是Nginx版本太低导致的,那解决方案就是升级Nginx了。如何不影响线上服务平滑升级Nginx呢?

平滑升级Nginx

源码编译

Nginx官网下载最新稳定版:

wget http://nginx.org/download/nginx-1.8.0.tar.gz

解压缩并进入源码目录:

tar zxf nginx-1.8.0.tar.gz
cd nginx-1.8.0

查看老版本Nginx的编译配置:

nginx -V

复制上述命令输出的配置参数,加到 ./configure 后面,修改prefix参数为其它目录:

./configure --prefix=/home/ubuntu/nginx <其它复制过来的配置参数>

执行完上面的命令,你可能看到如下错误信息,提示rewrite模块需要PCRE库:

./configure: error: the HTTP rewrite module requires the PCRE library.
You can either disable the module by using --without-http_rewrite_module
option, or install the PCRE library into the system, or build the PCRE library
statically from the source with nginx by using --with-pcre=<path> option.

PCRE官方FTP服务器下载PCRE并解压缩:

wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.36.tar.gz
tar zxf pcre-8.36.tar.gz

在Nginx的源码目录再次执行配置命令,这次加上--with-pcre选项:

./configure --prefix=/home/ubuntu/nginx <其它复制过来的配置参数> --with-pcre=/home/ubuntu/pcre-8.36

如果仍然提示缺少一些依赖库,安装这些库:

# on ubuntu/debian system
sudo apt-get install libssl-dev libxml2-dev libxslt-dev libgd-dev libgeoip-dev

配置无误后,然后执行make命令。至此,编译好的nginx可执行文件在当前目录的子目录objs里面。

平滑升级

  1. 备份旧版本的nginx可执行文件:

    <pre class=”brush: bash; gutter: false;”>
    sudo mv /usr/sbin/nginx /usr/sbin/nginx.old
    </pre>

  2. 把编译好的nginx可执行文件复制过去:

    <pre class=”brush: bash; gutter: false;”>
    sudo cp objs/nginx /usr/sbin/nginx
    </pre>

  3. 测试Nginx配置是否正确:

    <pre class=”brush: bash; gutter: false;”>
    sudo /usr/sbin/nginx -t
    </pre>

    如果测试正确,可以进入下一步操作。

  4. 给旧版本的nginx主进程发送USR2信号:

    <pre class=”brush: bash; gutter: false;”>
    ubuntu@me:~/nginx-1.8.0$ ps -ef | grep nginx
    root 9962 1 0 2014 ? 00:00:00 nginx: master process /usr/sbin/nginx
    ubuntu 31321 9962 0 Apr23 ? 00:04:58 nginx: worker process
    ubuntu 31322 9962 0 Apr23 ? 00:05:20 nginx: worker process
    ubuntu 31323 9962 0 Apr23 ? 00:05:46 nginx: worker process
    ubuntu 31324 9962 0 Apr23 ? 00:00:18 nginx: worker process
    ubuntu@me:~/nginx-1.8.0$ sudo kill -USR2 9962
    ubuntu@me:~/nginx-1.8.0$ ps -ef | grep nginx
    root 9962 1 0 2014 ? 00:00:00 nginx: master process /usr/sbin/nginx
    root 29149 9962 0 04:47 ? 00:00:00 nginx: master process /usr/sbin/nginx
    ubuntu 29150 29149 0 04:47 ? 00:00:00 nginx: worker process
    ubuntu 29151 29149 0 04:47 ? 00:00:00 nginx: worker process
    ubuntu 29152 29149 0 04:47 ? 00:00:00 nginx: worker process
    ubuntu 29153 29149 0 04:47 ? 00:00:00 nginx: worker process
    ubuntu 31321 9962 0 Apr23 ? 00:04:58 nginx: worker process
    ubuntu 31322 9962 0 Apr23 ? 00:05:20 nginx: worker process
    ubuntu 31323 9962 0 Apr23 ? 00:05:46 nginx: worker process
    ubuntu 31324 9962 0 Apr23 ? 00:00:18 nginx: worker process
    </pre>

    可以看到,在发送USR2信号后,新的nginx master进程和worker进程开始运行。此时,新nginx和旧nginx同时在工作。

  5. 接下来,给旧版本nginx主进程发送WINCH信号:

    <pre class=”brush: bash; gutter: false;”>
    ubuntu@me:~/nginx-1.8.0$ sudo kill -WINCH 9962
    ubuntu@me:~/nginx-1.8.0$ ps -ef | grep nginx
    root 9962 1 0 2014 ? 00:00:00 nginx: master process /usr/sbin/nginx
    root 29149 9962 0 04:47 ? 00:00:00 nginx: master process /usr/sbin/nginx
    ubuntu 29150 29149 0 04:47 ? 00:00:00 nginx: worker process
    ubuntu 29151 29149 0 04:47 ? 00:00:00 nginx: worker process
    ubuntu 29152 29149 0 04:47 ? 00:00:00 nginx: worker process
    ubuntu 29153 29149 0 04:47 ? 00:00:00 nginx: worker process
    </pre>

    这时,可以看到旧nginx的worker进程退出了,不再处理新的请求,新的请求都由新nginx处理。

  6. 接下来是平滑升级的最后一步,也就是给旧版本nginx主进程发送QUIT信号:

    <pre class=”brush: bash; gutter: false;”>
    ubuntu@me:~/nginx-1.8.0$ sudo kill -QUIT 9962
    ubuntu@me:~/nginx-1.8.0$ ps -ef | grep nginx
    root 29149 1 0 04:47 ? 00:00:00 nginx: master process /usr/sbin/nginx
    ubuntu 29150 29149 0 04:47 ? 00:00:00 nginx: worker process
    ubuntu 29151 29149 0 04:47 ? 00:00:00 nginx: worker process
    ubuntu 29152 29149 0 04:47 ? 00:00:00 nginx: worker process
    ubuntu 29153 29149 0 04:47 ? 00:00:00 nginx: worker process
    </pre>

    好了,至此旧nginx的主进程也退出了。我们已成功平滑升级到新版nginx.

版本回退

如果在执行上面的平滑升级过程中反悔了想回退,该怎么办?

  1. 给旧版本nginx主进程发送HUP信号
  2. 给新版本nginx主进程发送QUIT信号
  3. 从备份中还原nginx可执行文件

升级过程中出问题

按照上面的升级步骤,笔者在ubuntu系统上成功将Nginx升级到1.8版本。但是在公司虚拟机CentOS 6.4上执行升级操作却没有成功。问题在执行第3步的时候,检查Nginx配置失败,提示nginx.pm文件版本号不匹配。

参考资料

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容