nginx is a super stable server. Generally, it will not need to be restarted due to overload problems. The purpose of restarting is generally to load after modifying the configuration file.
At the beginning, I used the most direct way to restart
killall -9 nginx;/data/nginx/sbin/nginx
If the machine is relatively slow, the kill process cannot be killed in an instant, just execute it again. This restart method is not particularly safe. If the configuration is wrong, the restart will fail, and the configuration file needs to be modified before restarting, which will take a little time. However, for the currently generally not very strict http industry, this time will not cause too much loss, as long as it is not done at a critical moment. If you want to continue to use this restart method, I suggest that you should test it first.
Later I saw a more fantastic restart on nginx.net
kill -HUP $pid ($pid is the process number of the nginx master process)
I usually use it like this
kill -HUP `cat /data/nginx/logs/nginx.pid`
The advantage of this method is to achieve “smooth restart”, in ps
In -aux, it can be seen that nginx starts a new process first, and the old process still provides services. After a period of time, the old process will automatically close after the service ends, and the new process will continue to serve. But this method also has disadvantages. If the configuration file is wrong or there is a resource conflict, the restart will fail, but nginx does not have any prompts! This will often find that the modified configuration file does not take effect, and it is more difficult to find the problem.
So, in the end, I messed up the problem and made a nginx.sh. This version of nginx.sh still didn’t solve the kill
-HUP’s resource conflict problem, but solved the configuration file problem. Resource conflicts such as port 80 being occupied, log file directory not created, etc., I will think of a solution.
#!/bin/sh
BASE_DIR=’/data/’
${BASE_DIR}nginx/sbin/nginx -t -c
${BASE_DIR}nginx/conf/nginx.conf >&
${BASE_DIR}nginx/logs/nginx.start
info=`cat ${BASE_DIR}nginx/logs/nginx.start`
if [ `echo $info | grep -c “syntax is ok” ` -eq 1 ]; then
if [ `ps aux|grep “nginx”|grep -c “master”` == 1 ]; then
kill -HUP `cat ${BASE_DIR}nginx/logs/nginx.pid`
echo “ok”
else
killall -9 nginx
sleep 1
${BASE_DIR}nginx/sbin/nginx
the fi
else
echo “######## error: ########”
cat ${BASE_DIR}nginx/logs/nginx.start
the fi
———————————————
kill -HUP `cat /data/nginx/logs/nginx.pid`
This sentence is relatively long and not easy to remember. Now I find a more concise way:
killall -HUP nginx
killall will pass the HUP command to all nginx processes, including master and worker, but the worker process does not recognize the HUP command, so in fact only the master process gets the HUP and executes the restart.
Ever since, nginx’s kill command:
kill -USR1
kill -QUIT
…
You can use killall to simplify execution.