What is the method for smooth upgrade of Nginx production environment?

WBOY
Release: 2023-05-13 17:58:06
forward
1280 people have browsed it

1. Background

Recently I encountered a rather embarrassing and practical problem, that is, the Nginx used in our production environment is an old antique of Centos6. Business requirements need to be implemented by loading a module of Nginx, but the version is too old and needs Nginx1.18 or later to support it, and ours is Nginx1.12. Then upgrading Nginx is what we have to do. But in the production environment, you have to consider many things. Unlike the test server, Nginx stops the service, recompiles the new version, and then starts it again. Our online services need to be provided uninterrupted, otherwise it will cause economic losses to the business. So is there any plan to smoothly upgrade the Nginx version?

2. Upgrade plan

In fact, the official has already announced We have done a lot of work on the smooth upgrade of Nginx. The basic principle is to start a new Nginx (master worker) process, and then send the -USER2 command to the old master process, so that the new and old versions of the process can receive processing requests at the same time. After that, we send -WINCH to the old process to stop the working service (close all old worker processes, but the old master process is not closed to prevent you from encountering problems later). If you confirm that there is no problem with the new Nginx, then manually Kill the old master process to complete the smooth upgrade.

3. Operation process

1. View-Old version [nginx 1.12.2] process information

[root@k8s-master nginx-1.12.2]# ps aux | grep 'nginx' | grep -v '7月' | grep -v 'grep' root 15180 0.0 0.0 46136 920 ? Ss 17:22 0:00 nginx: master process ./nginx-1.12.2/sbin/nginx nobody 15181 0.0 0.1 46584 4344 ? S 17:22 0:00 nginx: worker process
Copy after login

Main process pid: 15180 Worker process 15181

2. nginx -V View the compilation parameters of the old version of nginx

[root@k8s-master nginx-1.12.2]# nginx-1.12.2/sbin/nginx.old.1.12 -V nginx version: nginx/1.12.2 built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC) built with OpenSSL 1.0.2k-fips 26 Jan 2017 TLS SNI support enabled configure arguments: --prefix=nginx-1.12.2 --with-pcre=/root/nginx-test/pcre-8.45/ --with-http_stub_status_module --with-http_gzip_static_module --with-http_ssl_module --with-stream [root@k8s-master nginx-1.12.2]#
Copy after login

If make or ./configure reports an error , you can try to install: yum install -y gcc-c

3. Back up the old version binary nginx program, mv nginx nginx.old

mv nginx nginx.old
Copy after login

4. At this time we download [nginx1.20.2] Recompile the new version and follow the compilation parameters of the old version (or add a new compilation module yourself)

5. Copy the newly generated binary nginx and move it to the nginx path of the previous old version to overwrite it.

./configure --prefix=/usr/local/ --with-stream xxxx[模块列表] make &make install
Copy after login

6. Send nginx -USR2 15180 (old nginx main process) for replacement. At this time, there are 4 processes. 2 old nginx processes 2 new nginx processes

What is the method for smooth upgrade of Nginx production environment?

This When the request is tested, it is load balanced to these two masters.

What is the method for smooth upgrade of Nginx production environment?

The request is made 2 times, the first time it is loaded to the new master, and the second time it is loaded to the old master .

7. Confirm that the upgrade is successful, -WINCH signal stops the old version master from receiving new requests (at this time, the old version nginx master process is not dead, but just stops receiving new requests)

kill -winch 15180
Copy after login

What is the method for smooth upgrade of Nginx production environment?

The old version of nginx only has the master process left, and the child process no longer exists.

At this time, if the URL is accessed multiple times, load balancing will not occur.

What is the method for smooth upgrade of Nginx production environment?

Hello world does not appear again

8. To roll back the version is also very simple, just send kill -HUP 15180 (old nginx master process)

What is the method for smooth upgrade of Nginx production environment?

What is the method for smooth upgrade of Nginx production environment?

At this time, both the old version and the new version of nginx are load balancing. Just kill the new version of nginx master.

At this time, the new version of nginx has been killed Just -QUIT.

The above is the detailed content of What is the method for smooth upgrade of Nginx production environment?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!