Nginx tutorial 3: nginx restart script (smooth restart of nginx)

Source: Internet
Author: User

Nginx is a super stable server and generally does not need to be restarted due to overload. The purpose of the restart is to modify the configuration file and load it.

At the beginning, I used killall-9 nginx, the most direct restart method./data/nginx/sbin/nginx if the machine is slow, the kill process cannot be killed in an instant, run the command again. This restart method is not particularly secure. If the configuration is incorrect, the restart will fail. You need to modify the configuration file and start it again, which takes a little time. However, for HTTP, which is still not very strict at present, this time will not cause too much loss, as long as it is not made at a critical moment. If you want to use this restart method, I suggest you test it first.

Later, I saw a more amazing restart of kill-HUP $ PID on nginx.net ($ PID is the process NUMBER OF THE nginx master process) I usually use kill-HUP 'cat/data/nginx/logs/nginx. the advantage of this method is to implement "smooth restart". In PS-Aux, we can see that nginx starts a new process and the old process still provides services. After a while, the old process service is automatically shut down after it ends, and the new process continues to serve. However, this method also has some disadvantages. If the configuration file is incorrect or the resource conflict occurs, the restart fails, but nginx does not have any prompts! This will often find that the modified configuration file does not take effect, and it is difficult to find the problem.

So, I finally fixed the problem and made an nginx. sh, nginx of this version. sh still does not solve the problem of resource conflicts in kill-HUP, but solves the problem of configuration files. Resource conflicts, such as port 80 being occupied and the log file directory is not created, I will try again.

#! /Bin/shbase_dir = '/data /'

$ {Base_dir} nginx/sbin/nginx-T-C $ {base_dir} nginx/CONF/nginx. conf> & $ {base_dir} nginx/logs/nginx. startinfo = 'cat $ {base_dir} nginx/logs/nginx. start 'If ['echo $ info | grep-c "syntax is OK" '-EQ 1]; thenif ['ps aux | grep "nginx" | grep
-C "master" '= 1]; thenkill-HUP 'cat $ {base_dir} nginx/logs/nginx. PID 'echo "OK"

Elsekillall-9 nginxsleep 1 $ {base_dir} nginx/sbin/nginxfielseecho "######### error :########"

Cat $ {base_dir} nginx/logs/nginx. startfi ----- kill-HUP 'cat/data/nginx/logs/nginx. the PID statement is long and hard to remember. Now we find a more concise method:

Killall-hup nginxkillall will upload the HUP command to all nginx processes, including master and worker. However, the worker process does not know the HUP command, so in fact, only the master process obtains the hup, and restart the instance.

Therefore, the kill command of nginx is as follows:

Kill-usr1

Kill-quit

......

You can use killall to simplify the execution.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.