How to smoothly upgrade and rollback Nginx version in 1 minute

How to smoothly upgrade and rollback Nginx version in 1 minute

Today, let's talk about a situation that is often encountered in the actual production environment of an enterprise, upgrading Nginx to a new version and how to roll back to the old version.

1. Environment Introduction

The two nginx versions prepared today are as follows:

[root@nginx ~]# cd /download/nginx/
[root@nginx nginx]# ll
total 1952
-rw-r--r-- 1 root root 981687 Oct 17 2017 nginx-1.12.2.tar.gz
-rw-r--r-- 1 root root 1015384 Dec 4 09:58 nginx-1.14.2.tar.gz

2. Compile and install the old and new versions

Compile and install nginx-1.12.2

[root@nginx nginx]# tar zxf nginx-1.12.2.tar.gz 
[root@nginx nginx]# cd nginx-1.12.2
[root@nginx nginx-1.12.2]# ./configure --prefix=/usr/local/nginx-1.12.2
[root@nginx nginx-1.12.2]# echo $?
0
[root@nginx nginx-1.12.2]# make && make install
[root@nginx nginx-1.12.2]# echo $?
0
[root@nginx nginx-1.12.2]# ll /usr/local/nginx-1.12.2/
total 0
drwxr-xr-x 2 root root 333 Mar 1 09:01 conf
drwxr-xr-x 2 root root 40 Mar 1 09:01 html
drwxr-xr-x 2 root root 6 Mar 1 09:01 logs
drwxr-xr-x 2 root root 19 Mar 1 09:01 sbin

Compile and install nginx-1.14.2

[root@nginx ~]# cd /download/nginx/
[root@nginx nginx]# tar zxf nginx-1.14.2.tar.gz 
[root@nginx nginx]# cd nginx-1.14.2
[root@nginx nginx-1.14.2]# ./configure --prefix=/usr/local/nginx-1.14.2
[root@nginx nginx-1.14.2]# echo $?
0
[root@nginx nginx-1.14.2]# make && make install
[root@nginx nginx-1.14.2]# echo $?
0
[root@nginx nginx-1.14.2]# ls -l /usr/local/nginx-1.14.2/
total 0
drwxr-xr-x 2 root root 333 Mar 1 09:03 conf
drwxr-xr-x 2 root root 40 Mar 1 09:03 html
drwxr-xr-x 2 root root 6 Mar 1 09:03 logs
drwxr-xr-x 2 root root 19 Mar 1 09:03 sbin

At this point, two versions of nginx software have been deployed.

3. Start the old version of nginx

[root@nginx ~]# /usr/local/nginx-1.12.2/sbin/nginx -t
nginx: the configuration file /usr/local/nginx-1.12.2/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx-1.12.2/conf/nginx.conf test is successful
[root@nginx ~]# /usr/local/nginx-1.12.2/sbin/nginx
[root@nginx ~]# ps -ef|grep nginx
root 6324 1 0 09:06 ? 00:00:00 nginx: master process /usr/local/nginx-1.12.2/sbin/nginx
nobody 6325 6324 0 09:06 ? 00:00:00 nginx: worker process
root 6327 1244 0 09:06 pts/0 00:00:00 grep --color=auto nginx
[root@nginx ~]# lsof -i :80
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
nginx 6324 root 6u IPv4 26324 0t0 TCP *:http (LISTEN)
nginx 6325 nobody 6u IPv4 26324 0t0 TCP *:http (LISTEN)

4. Upgrade to the latest version

Version upgrade is actually an upgrade for binary files. The process is as follows:

[root@nginx ~]# /usr/local/nginx-1.12.2/sbin/nginx -v
nginx version: nginx/1.12.2
[root@nginx ~]# cd /usr/local/nginx-1.12.2/sbin/
[root@nginx sbin]# mv nginx nginx-1.12.2
#First, back up the original old version of nginx binary file [root@nginx sbin]# cp /usr/local/nginx-1.14.2/sbin/nginx ./
#Copy the new version of the binary file to the current directory

Next, perform a smooth upgrade operation

[root@nginx ~]# ps -ef|grep nginx
root 6324 1 0 09:06 ? 00:00:00 nginx: master process /usr/local/nginx-1.12.2/sbin/nginx
nobody 6325 6324 0 09:06 ? 00:00:00 nginx: worker process
root 6338 1244 0 09:11 pts/0 00:00:00 grep --color=auto nginx
[root@nginx ~]# kill -USR2 6324
[root@nginx ~]# ps -ef|grep nginx
root 6324 1 0 09:06 ? 00:00:00 nginx: master process /usr/local/nginx-1.12.2/sbin/nginx
nobody 6325 6324 0 09:06 ? 00:00:00 nginx: worker process
root 6340 6324 0 09:12 ? 00:00:00 nginx: master process /usr/local/nginx-1.12.2/sbin/nginx
nobody 6341 6340 0 09:12 ? 00:00:00 nginx: worker process
root 6343 1244 0 09:12 pts/0 00:00:00 grep --color=auto nginx

At this time, the new master process has been started normally, but the old worker process still exists, so we use the following command to signal the old worker process to stop smoothly, as follows:

[root@nginx ~]# kill -WINCH 6324
[root@nginx ~]# ps -ef|grep nginx
root 6324 1 0 09:06 ? 00:00:00 nginx: master process /usr/local/nginx-1.12.2/sbin/nginx
root 6340 6324 0 09:12 ? 00:00:00 nginx: master process /usr/local/nginx-1.12.2/sbin/nginx
nobody 6341 6340 0 09:12 ? 00:00:00 nginx: worker process
root 6346 1244 0 09:14 pts/0 00:00:00 grep --color=auto nginx

At this point, the old worker process has stopped. Next, we test whether it can be accessed normally:

It can be accessed normally. In fact, this smooth upgrade is completely imperceptible to the accessing users, so the nginx hot deployment has been completed.

[root@nginx ~]# /usr/local/nginx-1.12.2/sbin/nginx -v
nginx version: nginx/1.14.2

Check the version and it is the latest version, the upgrade is complete.

Note: If there are no problems after the version upgrade is completed and you need to shut down the old master process, you can use the following command:

kill -QUIT old_master_PID

5. Version rollback

The most difficult part of upgrading is not upgrading, but rolling back, because there is a possibility of rollback in the actual production environment. For example, the new version is incompatible with the existing application due to some unknown bugs, or the operation becomes unstable, etc.

Therefore, for operation and maintenance engineers, fault rollback is the key point.

In the above results, we can also see that the old master process always exists. It will not shut down by itself unless it is manually shut down. This design is beneficial. The benefit is that after upgrading the new version, if there is a problem, it can be rolled back to the previous stable version in a timely and quick manner.

[root@nginx ~]# ps -ef|grep nginx
root 6324 1 0 09:06 ? 00:00:00 nginx: master process /usr/local/nginx-1.12.2/sbin/nginx
root 6340 6324 0 09:12 ? 00:00:00 nginx: master process /usr/local/nginx-1.12.2/sbin/nginx
nobody 6341 6340 0 09:12 ? 00:00:00 nginx: worker process
root 6350 1244 0 09:23 pts/0 00:00:00 grep --color=auto nginx
[root@nginx ~]# cd /usr/local/nginx-1.12.2/sbin/
[root@nginx sbin]# mv nginx nginx-1.14.2
[root@nginx sbin]# mv nginx-1.12.2 nginx
[root@nginx sbin]# kill -USR1 6324
[root@nginx sbin]# ps -ef|grep nginx
root 6324 1 0 09:06 ? 00:00:00 nginx: master process /usr/local/nginx-1.12.2/sbin/nginx
root 6340 6324 0 09:12 ? 00:00:00 nginx: master process /usr/local/nginx-1.12.2/sbin/nginx
nobody 6341 6340 0 09:12 ? 00:00:00 nginx: worker process
root 6355 1244 0 09:24 pts/0 00:00:00 grep --color=auto nginx
[root@nginx sbin]# ./nginx -v
nginx version: nginx/1.12.2

From the above results, we can see that the previous version has been rolled back smoothly. Next, we will test whether it can be accessed normally:

It can still be accessed normally, so the rollback operation is also imperceptible to the user.

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • Example of how to upgrade nginx to support HTTP2.0
  • Detailed explanation of Nginx version smooth upgrade solution
  • How to upgrade nginx to support http2
  • Some suggestions for improving Nginx performance
  • Nginx service installation and software upgrade

<<:  Detailed explanation of the observer mode starting from the component value transfer of Vue

>>:  Example of using CASE WHEN in MySQL sorting

Recommend

vue dynamic component

Table of contents 1. Component 2. keep-alive 2.1 ...

Example of integrating Kafka with Nginx

background nginx-kafka-module is a plug-in for ng...

Detailed explanation of JS ES6 coding standards

Table of contents 1. Block scope 1.1. let replace...

In-depth study of JavaScript array deduplication problem

Table of contents Preface 👀 Start researching 🐱‍🏍...

Introduction to commonly used MySQL commands in Linux environment

Enter the mysql command: mysql -u+(user name) -p+...

Vue detailed introductory notes

Table of contents 1. Introduction 2. Initial Vue ...

How to write configuration files and use MyBatis simply

How to write configuration files and use MyBatis ...

Docker uses a single image to map to multiple ports

need: The official website's resource server ...

JavaScript prototype and prototype chain details

Table of contents 1. prototype (explicit prototyp...

JavaScript implements changing the color of a web page through a slider

Hello everyone, today when I was looking at the H...

HTML implements read-only text box and cannot modify the content

Without further ado, I will post the code for you...

Detailed explanation of various join summaries of SQL

SQL Left Join, Right Join, Inner Join, and Natura...

Examples of clearfix and clear

This article mainly explains how to use clearfix a...