search
HomeBackend DevelopmentPHP TutorialLet's talk about nginx smooth restart and FPM smooth restart

Let's talk about nginx smooth restart and FPM smooth restart

Mar 10, 2022 am 11:30 AM
fpmnginxgraceful restart

This article will take you through smooth restart and introduce nginx smooth restart and FPM smooth restart in detail. I hope it can help you!

Smooth Restart

GR is the abbreviation of Graceful Restart (smooth restart), which is a method to ensure that forwarding services are not interrupted when the protocol is restarted. mechanism.
The core of the GR mechanism is that when a device restarts the protocol, it can notify its surrounding devices to maintain stable neighbor relationships and routes to the device within a certain period of time. After the protocol is restarted, peripheral devices assist it in synchronizing information (including various topology, routing and session information maintained by routing/MPLS-related protocols that support GR), restoring the device to the state before the restart in the shortest possible time. state. There will be no route flapping during the entire protocol restart process, and there will be no change in the packet forwarding path. The entire system can forward data without interruption. This process is called a smooth restart.

nginx smooth restart

The nginx process is divided into the master process and the worker process. The smooth restart of nginx is controlled by the signal HUB.

Lets talk about nginx smooth restart and FPM smooth restart

Note: On POSIX-compliant platforms, SIGUSR1 and SIGUSR2 are signals sent to a process that represent user-defined situations.

In order to analyze the smooth restart process of nginx in detail, we continue to monitor the nginx process changes.
Send HUP signal

kill -HUP `cat /home/git/nginx/logs/nginx.pid`

Lets talk about nginx smooth restart and FPM smooth restart

Lets talk about nginx smooth restart and FPM smooth restart

Lets talk about nginx smooth restart and FPM smooth restart

##Through observation, a rough smooth restart can be analyzed The process is:

1. The master uses the new configuration to fork out n-1 workers and a new master
2. The new worker handles the new request, and the old worker exits after execution
3. The master reloads the configuration, during Use the new master to take over the service
4. After the master is loaded and configured, the new master switches to the worker working mode
After the smooth restart, the master process number will not change.

nginx smooth upgrade

HUP is only used for smooth restart, loading configuration, etc. If you want to smoothly upgrade the nginx version, reload the compiled binary file requires the help of the USR2 signal.

1. Send USR2 signal

kill -USR2 `cat /home/git/nginx/logs/nginx.pid`

Lets talk about nginx smooth restart and FPM smooth restart

Lets talk about nginx smooth restart and FPM smooth restart##Observe the nginx process, fork out new master and worker, at this time The content of nginx.pid has changed, and the nginx.pid.oldbin file is generated in the logs directory to record the old master pid.

2. Send the WINCH signal to the old master, and nginx worker will stop the service gracefully. That is: stop receiving new requests, but will not terminate requests that are already being processed. After a period of time, all worker processes of the old nginx exit, leaving only the master process, and all user requests are processed by the new nginx process.

kill -WINCH `cat /home/git/nginx/logs/nginx.pid.oldbin`

Lets talk about nginx smooth restart and FPM smooth restart3. Send the QUIT signal to the old master, the old nginx process completely exits, and the smooth upgrade is completed.

kill -QUIT `cat /home/git/nginx/logs/nginx.pid.oldbin`

Lets talk about nginx smooth restart and FPM smooth restart

FPM Smooth RestartFPM (FastCGI Process Manager) is used to replace PHP FastCGI Most of the additional functions, FPM has been integrated since php5.3.3. PHP-FPM can be turned on by passing the –enable-fpm parameter in ./configure.

FPM’s smooth restart needs to be controlled by the USR2 signal, but it is quite different from nginx’s smooth restart process.

kill -USR2 `cat /home/git/php/var/run/php-fpm.pid`

Lets talk about nginx smooth restart and FPM smooth restart By continuing to observe the fpm process, we can see that FPM restarts smoothly. It needs to wait for the child process to completely exit before starting the new master and child processes, and then the old master quit.

Use strace for further analysis


Lets talk about nginx smooth restart and FPM smooth restartIt is found that the master notifies all child processes to exit, including the child process that is processing the request.

In order to further verify this conclusion, write a server-side sleep script

<?php
exec("sleep 5");
echo &#39;done&#39;;

用浏览器请求这个地址,并在此期间平滑重启fpm,请求直接502了。
nginx错误日志:

[error] 29841#0: *1646 recv() failed (104: Connection reset by peer) while reading response header from upstream, client: 127.0.0.1, server: localhost, request: "GET /test.php HTTP/1.1", upstream: "fastcgi://127.0.0.1:9001", host: "localhost"

php bug#60961,也有对fpm无法优雅的实现平滑重启的说明。
难道FPM这么low?答案当时是no,实际上通过 process_control_timeout 参数可以实现我们的目标。

process_control_timeout

 设置子进程接受主进程复用信号的超时时间。可用单位:s(秒),m(分),h(小时)或者 d(天)。默认单位:s(秒)。默认值:0(关闭)。

 原则上,php-fpm会选择空闲的fastcgi进程去处理请求,在处理之前,php-fpm会给fastcgi发送信号,用来让fastcgi进程准备好接受请求处理。但是fastcgi进程并不总是能够处理请求,也就是不能总是响应该信号(比如出现假死的情况),这时候就需要设定php-fpm留给fastcgi进程响应信号的时间,如果超时了,php-fpm会想其他办法(例如选择其他fastcgi进程),这个就是process_control_timeout参数的作用。

 这个参数缺省是 0,也就是不生效,修改为10,重新验证,502已经不会再出现。

 结论:缺省情况下,PHP-FPM 无法保证平滑的执行 reload 操作,必须设置一个合理的 process_control_timeout 才行,同时需要注意的是其值不能设置的过大,否则系统可能出现严重的请求堵塞问题。

推荐学习:《PHP视频教程

The above is the detailed content of Let's talk about nginx smooth restart and FPM smooth restart. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:huaweicloud. If there is any infringement, please contact admin@php.cn delete
PHP in Action: Real-World Examples and ApplicationsPHP in Action: Real-World Examples and ApplicationsApr 14, 2025 am 12:19 AM

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP: Creating Interactive Web Content with EasePHP: Creating Interactive Web Content with EaseApr 14, 2025 am 12:15 AM

PHP makes it easy to create interactive web content. 1) Dynamically generate content by embedding HTML and display it in real time based on user input or database data. 2) Process form submission and generate dynamic output to ensure that htmlspecialchars is used to prevent XSS. 3) Use MySQL to create a user registration system, and use password_hash and preprocessing statements to enhance security. Mastering these techniques will improve the efficiency of web development.

PHP and Python: Comparing Two Popular Programming LanguagesPHP and Python: Comparing Two Popular Programming LanguagesApr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

The Enduring Relevance of PHP: Is It Still Alive?The Enduring Relevance of PHP: Is It Still Alive?Apr 14, 2025 am 12:12 AM

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP's Current Status: A Look at Web Development TrendsPHP's Current Status: A Look at Web Development TrendsApr 13, 2025 am 12:20 AM

PHP remains important in modern web development, especially in content management and e-commerce platforms. 1) PHP has a rich ecosystem and strong framework support, such as Laravel and Symfony. 2) Performance optimization can be achieved through OPcache and Nginx. 3) PHP8.0 introduces JIT compiler to improve performance. 4) Cloud-native applications are deployed through Docker and Kubernetes to improve flexibility and scalability.

PHP vs. Other Languages: A ComparisonPHP vs. Other Languages: A ComparisonApr 13, 2025 am 12:19 AM

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP vs. Python: Core Features and FunctionalityPHP vs. Python: Core Features and FunctionalityApr 13, 2025 am 12:16 AM

PHP and Python each have their own advantages and are suitable for different scenarios. 1.PHP is suitable for web development and provides built-in web servers and rich function libraries. 2. Python is suitable for data science and machine learning, with concise syntax and a powerful standard library. When choosing, it should be decided based on project requirements.

PHP: A Key Language for Web DevelopmentPHP: A Key Language for Web DevelopmentApr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

DVWA

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

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),