Nginx-平滑升级&开启新模块

今天想利用zabbix对nginx状态页面进行监控,在nginx.conf添加以下配置

location /nginx_status {
    stub_status;
}

 检测发现报错,凭经验判定是未开启模块导致的

[root@iZbp18re0hh4zzdtp6obrzZ sbin]# ./nginx  -t
nginx: [emerg] unknown directive "stub_status" in /usr/local/nginx/conf/nginx.conf:80
nginx: configuration file /usr/local/nginx/conf/nginx.conf test failed

 看一下都开了哪些模块,一个都没。。。

[root@iZbp18re0hh4zzdtp6obrzZ sbin]# ./nginx  -V
nginx version: nginx/1.20.1
built by gcc 8.5.0 20210514 (Red Hat 8.5.0-4) (GCC)
configure arguments:

 开启模块

这里有一个非常需要注意的点,如果之前的版本用户是nobody,那么这里指定了用户为nginx,就会出现权限不足,访问拒绝的问题。

解决也很简单:如果按以下这个方式重新编译指定nginx,那么需要在nginx.conf修改用户为nginx,然后chown重新授权目录权限为nginx。

]# ./configure --prefix=/usr/local/nginx  --user=nginx  --group=nginx  --with-http_ssl_module  --with-http_v2_module  --with-http_realip_module  --with-http_stub_status_module  --with-http_gzip_static_module  --with-pcre  --with-stream  --with-stream_ssl_module  --with-stream_realip_module  --with-http_sub_module

 接下来执行 make,千万不要make install~~~~

make

备份之前的nginx

]# mv /usr/local/nginx/sbin/nginx  /usr/local/nginx/sbin/nginx-old

 将新编译的nginx拷贝至工作目录

objs]# cp nginx /usr/local/nginx/sbin/
[root@iZbp18re0hh4zzdtp6obrzZ sbin]# ll
total 12604
-rwxr-xr-x 1 root root 7728600 Jun 26 15:01 nginx
-rwxr-xr-x 1 root root 5175136 Nov 19  2021 nginx-old

 再看一下

[root@iZbp18re0hh4zzdtp6obrzZ sbin]# ./nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: [emerg] getpwnam("nginx") failed
nginx: configuration file /usr/local/nginx/conf/nginx.conf test failed

 又报错了,这个是因为编译指定了nginx用户,然而系统并没有nginx用户

]# useradd  -s /sbin/nologin nginx

 检测一下

[root@iZbp18re0hh4zzdtp6obrzZ sbin]# ./nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

 重点

查看之前nginx进程

]# ps -ef | grep nginx
root        3038       1  0 May07 ?        00:00:00 nginx: master process ./nginx
nobody      3039    3038  0 May07 ?        00:17:03 nginx: worker process
root      382196  377642  0 15:01 pts/0    00:00:00 grep --color=auto nginx

 平滑升级

]# kill -USR2 3038

 再看一下

n]# ps -ef | grep nginx
root        3038       1  0 May07 ?        00:00:00 nginx: master process ./nginx
nobody      3039    3038  0 May07 ?        00:17:03 nginx: worker process
root      382285    3038  0 15:02 ?        00:00:00 nginx: master process ./nginx
nginx     382286  382285  0 15:02 ?        00:00:00 nginx: worker process
nginx     382287  382285  0 15:02 ?        00:00:00 nginx: worker process
nginx     382288  382285  0 15:02 ?        00:00:00 nginx: worker process
nginx     382289  382285  0 15:02 ?        00:00:00 nginx: worker process
root      382291  377642  0 15:02 pts/0    00:00:00 grep --color=auto nginx

 看一下模块

]# ./nginx -V
nginx version: nginx/1.20.1
built by gcc 8.5.0 20210514 (Red Hat 8.5.0-4) (GCC)
built with OpenSSL 1.1.1k  FIPS 25 Mar 2021
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre --with-stream --with-stream_ssl_module --with-stream_realip_module --with-http_sub_module

还遇到了一个问题

测试时一直报错404,如图

[root@iZbp18re0hh4zzdtp6obrzZ sbin]# curl http://localhost/nginx_status -i
HTTP/1.1 404 Not Found
Server: nginx/1.20.1
Date: Sun, 26 Jun 2022 07:42:29 GMT
Content-Type: application/json
Content-Length: 49
Connection: keep-alive

{"error":"Object Not Found","reason":"Not Found"}

这个报错是因为旧的nginx 进程还未退出

[root@iZbp18re0hh4zzdtp6obrzZ sbin]# ps -ef | grep nginx
root        3038       1  0 May07 ?        00:00:00 nginx: master process ./nginx
nobody      3039    3038  0 May07 ?        00:17:04 nginx: worker process
root      382285    3038  0 15:02 ?        00:00:00 nginx: master process ./nginx
nginx     388562  382285  0 15:42 ?        00:00:00 nginx: worker process
nginx     388563  382285  0 15:42 ?        00:00:00 nginx: worker process
nginx     388564  382285  0 15:42 ?        00:00:00 nginx: worker process
nginx     388565  382285  0 15:42 ?        00:00:00 nginx: worker process
root      388735  377642  0 15:44 pts/0    00:00:00 grep --color=auto nginx

 居然存在两个master进程,让它优雅地退出

[root@iZbp18re0hh4zzdtp6obrzZ sbin]# kill -QUIT 3038

 再看一下进程

[root@iZbp18re0hh4zzdtp6obrzZ sbin]# ps -ef | grep nginx
root      382285       1  0 15:02 ?        00:00:00 nginx: master process ./nginx
nginx     388562  382285  0 15:42 ?        00:00:00 nginx: worker process
nginx     388563  382285  0 15:42 ?        00:00:00 nginx: worker process
nginx     388564  382285  0 15:42 ?        00:00:00 nginx: worker process
nginx     388565  382285  0 15:42 ?        00:00:00 nginx: worker process
root      388968  377642  0 15:47 pts/0    00:00:00 grep --color=auto nginx

 没了,在测试一下

[root@iZbp18re0hh4zzdtp6obrzZ sbin]# curl http://localhost/nginx_status -i
HTTP/1.1 200 OK
Server: nginx/1.20.1
Date: Sun, 26 Jun 2022 07:47:20 GMT
Content-Type: text/plain
Content-Length: 104
Connection: keep-alive

Active connections: 5
server accepts handled requests
 153 153 1798
Reading: 0 Writing: 1 Waiting: 4

查看error.log,没任何异常表示升级成功。

 至此,Nginx就升级成功了。

 

posted @ 2022-06-26 15:24  不会跳舞的胖子  阅读(175)  评论(0编辑  收藏  举报