Home  >  Article  >  Operation and Maintenance  >  How to upgrade nginx version smoothly and safely

How to upgrade nginx version smoothly and safely

王林
王林forward
2021-02-03 10:52:113189browse

How to upgrade nginx version smoothly and safely

Let’s first introduce several signals supported by nginx. Listed below are several signals that the main process can receive.

Note: The worker process can also receive some signals, but its signal processing mechanism is somewhat different from the main process, and the signals supported by the main process may not be supported by the worker process.

How to upgrade nginx version smoothly and safely

The behavior of graceful stop is: (1) the process no longer listens and accepts new requests; (2) the process continues to process the request being processed, but is destroyed after the processing is completed .

1. Upgrade

If you want to upgrade a running nginx instance, or replace an old version because you have recompiled a version, you can consider following the following series of processes to stabilize and Upgrade safely. Of course, stopping the service directly will not have much impact. It is more convenient and simple to stop and then start the new version of the nginx instance.

1. Replace the old nginx command with the new version of the nginx command path.

Usually, for compiled and installed nginx, it is more convenient to use soft links. For example, if the installation path of the old version is /usr/local/nginx-1.12.0, create a soft link /usr/local/nginx for it. If there is a new version /usr/local/nginx-1.12.1, you only need to modify the soft link. The link /usr/local/nginx points to /usr/local/nginx-1.12.1. In this way, /usr/local/nginx/sbin/nginx will point to the new nginx program as the soft link changes.

2. Send the USR2 signal to the main process of the old nginx instance.

kill -USR2 `cat /var/run/nginx/nginx.pid`

This signal prompts the old main process of nginx to be upgraded and execute the new nginx program. For example, in step 1, the old nginx main process is /usr/local/nginx/sbin/nginx, but it points to /usr/local/nginx-1.12.0/sbin/nginx. After sending the signal, / will still be executed. usr/local/nginx/sbin/nginx, but because the soft link target has changed, the nginx started at this time is already the /usr/local/nginx-1.12.1/sbin/nginx program.

How to upgrade nginx version smoothly and safely

In addition, after sending this signal, the pid file will be switched. The old pid file is renamed to nginx.pid.oldbin, and the old nginx main process pid is recorded. value, the new pid file is nginx.pid, which records the pid value of the newly started nginx main process.

[root@xuexi ~]# ls /var/run/nginx*     
/var/run/nginx.pid  /var/run/nginx.pid.oldbin

3.graceful stop the old main process number. kill -QUIT `cat /var/run/nginx/nginx.pid.oldbin`

Sends a QUIT signal to the old main process number, which will cause the main process to shut down in a graceful manner. This will cause the old main process and the old worker process to no longer accept any new requests, but will complete the processing of the requests being processed and then be destroyed and exit.

4. A more reliable way is to gracefully stop the worker process first, and then gracefully stop the old main process after the new version of the nginx instance runs for a short period of time if it works normally.

kill -WINCH `cat /var/run/nginx/nginx.pid.oldbin`
# a period of time goes, graceful stop old master nginx
kill -QUIT `cat /var/run/nginx/nginx.pid.oldbin`

After sending the WINCH signal to the old main process, the old worker process will gradually exit, but the old main process will remain without exiting.

How to upgrade nginx version smoothly and safely

If you find that the new version of the nginx instance is not satisfied, you can directly send a HUP signal to the old main process number, so that the old main process will re-read the configuration file and fork The new worker process can be restored to the old version of nginx instance by killing the new main process number (graceful stop can be used).

2. Downgrade

Step 4 above is actually the safest way to downgrade. That is:

kill -HUP `cat /var/run/nginx/nginx.pid.oldbin`
kill -QUIT `cat /var/run/nginx/nginx.pid`

But if the old main process number has been killed and only the new version of the nginx instance is currently running, then you only need to downgrade it in the same upgrade steps. That is:

kill -USR2 `cat /var/run/nginx/nginx.pid`
kill -QUIT `cat /var/run/nginx/nginx.pid.oldbin`

3. One-click upgrade script

The following is the upgrade script.

How to upgrade nginx version smoothly and safely

Related recommendations: nginx tutorial

The above is the detailed content of How to upgrade nginx version smoothly and safely. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:cnblogs.com. If there is any infringement, please contact admin@php.cn delete
Previous article:nginx quick startNext article:nginx quick start

Related articles

See more