Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to implement smooth upgrade and rollback of Nginx version

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)05/31 Report--

This article introduces the knowledge of "how to achieve smooth upgrade and rollback of Nginx version". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

1. Environmental introduction

The two versions of nginx 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 new and old 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-1 / 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 the 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 new version

A version upgrade is actually an upgrade for binaries, and 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 old version of nginx binaries [root@nginx sbin] # cp / usr/local/nginx-1.14.2/sbin/nginx. / # copy the new version of the binaries to the current directory

Next, perform a smooth upgrade operation.

[root@nginx ~] # ps-ef | grep nginx root 6324 10 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 work process also exists, so we use the following command to signal a smooth stop of the old work process, as follows:

[root@nginx ~] # kill-WINCH 6324 [root@nginx ~] # ps-ef | grep nginx root 6324 10 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 work process has stopped, so let's test whether it can be accessed properly:

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

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

Check that the version is also the version of *, and the upgrade is complete.

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

Kill-QUIT old_master_PID

5. Version rollback

For upgrading, the most difficult thing is not the upgrade, but the rollback, because there is a chance of rollback in the actual production environment, for example, the new version is incompatible with existing applications due to some unknown bug, or the operation is unstable.

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 exists all the time, and it will not shut down by itself until it is shut down manually. This design is beneficial because after upgrading the new version, if there is a problem, it can be rolled back to the last stable version in time and quickly.

[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 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 10 09:06? 00: 00:00 nginx: master process / usr/local/nginx-1.12.2/sbin/nginx root 6340 6324 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, it is found that the previous version has been rolled back smoothly, and then test whether it can be accessed properly:

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

This is the end of the content of "how to achieve smooth upgrade and rollback of Nginx version". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report