


What is the method for smooth upgrade of Nginx production environment?
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
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]#
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
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
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
This When the request is tested, it is load balanced to these two masters.
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
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.
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)
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

How to configure Nginx in Windows? Install Nginx and create a virtual host configuration. Modify the main configuration file and include the virtual host configuration. Start or reload Nginx. Test the configuration and view the website. Selectively enable SSL and configure SSL certificates. Selectively set the firewall to allow port 80 and 443 traffic.

You can query the Docker container name by following the steps: List all containers (docker ps). Filter the container list (using the grep command). Gets the container name (located in the "NAMES" column).

Docker container startup steps: Pull the container image: Run "docker pull [mirror name]". Create a container: Use "docker create [options] [mirror name] [commands and parameters]". Start the container: Execute "docker start [Container name or ID]". Check container status: Verify that the container is running with "docker ps".

How to confirm whether Nginx is started: 1. Use the command line: systemctl status nginx (Linux/Unix), netstat -ano | findstr 80 (Windows); 2. Check whether port 80 is open; 3. Check the Nginx startup message in the system log; 4. Use third-party tools, such as Nagios, Zabbix, and Icinga.

Create a container in Docker: 1. Pull the image: docker pull [mirror name] 2. Create a container: docker run [Options] [mirror name] [Command] 3. Start the container: docker start [Container name]

The methods that can query the Nginx version are: use the nginx -v command; view the version directive in the nginx.conf file; open the Nginx error page and view the page title.

How to configure an Nginx domain name on a cloud server: Create an A record pointing to the public IP address of the cloud server. Add virtual host blocks in the Nginx configuration file, specifying the listening port, domain name, and website root directory. Restart Nginx to apply the changes. Access the domain name test configuration. Other notes: Install the SSL certificate to enable HTTPS, ensure that the firewall allows port 80 traffic, and wait for DNS resolution to take effect.

Starting an Nginx server requires different steps according to different operating systems: Linux/Unix system: Install the Nginx package (for example, using apt-get or yum). Use systemctl to start an Nginx service (for example, sudo systemctl start nginx). Windows system: Download and install Windows binary files. Start Nginx using the nginx.exe executable (for example, nginx.exe -c conf\nginx.conf). No matter which operating system you use, you can access the server IP
