


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!

In Linux systems, you can use the sudo systemctl restart nginx command to restart Nginx. This command will stop the Nginx process and start a new process, ensuring that the command is run as root user or as a user with sudo permissions. If you are using an older version of Linux, you can use the /etc/init.d/nginx restart (Red Hat/CentOS) or the /usr/sbin/service nginx restart (Debian/Ubuntu) command.

How to restart the file using nginx? Reload the configuration file: nginx -t && nginx -s reload restart nginx process: nginx -s stop && nginx

The steps to restart Nginx service on a Windows system are as follows: Open the Services Manager, locate and right-click the "Nginx" service, and select "Restart". Wait for the service to restart. Right-click the "Nginx" service, select "Properties", and check whether the service status is "Running".

Load balancing distributes network traffic to multiple servers, improving the performance and reliability of your website or application. Nginx provides load balancing algorithms such as polling, minimal connection, weighted polling, and IP hashing. Load balancing can be configured by defining back-end servers and front-end servers in the Nginx configuration file. The load balancing algorithm assigns requests based on the number of server connections, weights, or client IP addresses. Nginx also provides health checks to monitor the availability of backend servers and remove unhealthy servers from the load balancer as appropriate.

To restart the Nginx service, you can use the following command: sudo service nginx restart. Other methods include using systemctl (sudo systemctl restart nginx) or Nginx binary (/usr/sbin/nginx -s reload). Before restarting the service, save all changes and check for errors in the Nginx configuration file. Restarting Nginx service will temporarily interrupt service.

The reasons why nginx configuration does not take effect after restart include: syntax errors, configuration file not loading, configuration not reloading, file permission issues, old process interference, cache issues, etc. Solutions include: checking syntax, ensuring that the configuration file is loaded, reloading the configuration, checking file permissions, forcibly terminate the old process, clearing the cache, checking log files, restarting the computer, upgrading nginx, and monitoring resource usage.

Monitoring Nginx is crucial to ensure that websites and applications work properly. You can monitor key metrics (request count, response time, concurrent connection count, error count, CPU/memory usage) using built-in status modules, third-party tools such as Prometheus, Nagios, New Relic, and set alerts to detect problems early. Check monitoring tools regularly to make sure everything works as expected.

Nginx monitoring tools include: Web-based tools: Nginx Amplify, Nginx Dashboard command line tools: Nginx Status, nginxtop third-party tools: Prometheus, Grafana, Elasticsearch Kibana, New Relic, Datadog


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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

SublimeText3 English version
Recommended: Win version, supports code prompts!

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SublimeText3 Chinese version
Chinese version, very easy to use

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function