Nginx的啟動
Nginx的停止
Nginx支持以下幾種信號控制:
- TERM, INT 快速關閉
- QUIT 從容關閉
- HUP 平滑重啟
- USR1 重新打開日志文件,在切割文件時用處大
- USR2 平滑升級
- WINCH 從容關閉工作進程
我們可以通過信號停止Nginx主進程,首先,我們需要通過ps -ef|grep命令獲得master進程的PID,或者通過cat pid文件獲得主進程號。下面是幾個典型的停止語句:
#從容停止Nginx kill -QUIT master進程號 #快速停止Nginx kill -TERM master進程號 #強制停止Nginx kill -9 master進程號
Nginx的重加載
如果改變了配置文件,想重啟讓其生效,同樣可以通過發送系統信號給Nginx主進程,不過,在重啟之前,要確認配置文件的語法是正確的,否則將不會加載新的配置項。
通過以下語句測試配置文件語法是否正確:
然后,通過以下命令重加載Nginx配置:
kill -HUP master進程號
Nginx的啟動腳本
#!/bin/sh # chkconfig: 2345 85 15 # description:Nginx Server NGINX_HOME=/usr/local/nginx NGINX_SBIN=$NGINX_HOME/sbin/nginx NGINX_CONF=$NGINX_HOME/conf/nginx.conf NGINX_PID=$NGINX_HOME/logs/nginx.pid NGINX_NAME="Nginx" . /etc/rc.d/init.d/functions if [ ! -f $NGINX_SBIN ] then echo "$NGINX_NAME startup: $NGINX_SBIN not exists! " exit fi start() { $NGINX_SBIN -c $NGINX_CONF ret=$? if [ $ret -eq 0 ]; then action $"Starting $NGINX_NAME: " /bin/true else action $"Starting $NGINX_NAME: " /bin/false fi } stop() { kill `cat $NGINX_PID` ret=$? if [ $ret -eq 0 ]; then action $"Stopping $NGINX_NAME: " /bin/true else action $"Stopping $NGINX_NAME: " /bin/false fi } restart() { stop start } check() { $NGINX_SBIN -c $NGINX_CONF -t } reload() { kill -HUP `cat $NGINX_PID` && echo "reload success!" } relog() { kill -USR1 `cat $NGINX_PID` && echo "relog success!" } case "$1" in start) start ;; stop) stop ;; restart) restart ;; check|chk) check ;; status) status -p $NGINX_PID ;; reload) reload ;; relog) relog ;; *) echo $"Usage: $0 {start|stop|restart|reload|status|check|relog}" exit 1 esac
除了上面介紹的直接發信號給Nginx主進程的方法之外,我們還可以通過nginx -s命令:
stop
— fast shutdownquit
— graceful shutdownreload
— reloading the configuration filereopen
— reopening the log files