#!/bin/sh
#
-# nginx Nginx Web Server
+# nginx Nginx Web Server (@type@ version)
#
# chkconfig: 345 85 15
-# description: Nginx is a World Wide Web server. It is used to serve \
-# HTML files and CGI.
-# processname: nginx
-# pidfile: /var/run/nginx.pid
-# config: /etc/nginx/nginx.conf
-
+# description: Nginx is an HTTP(S) server, HTTP(S) reverse \
+# proxy and IMAP/POP3 proxy server
+# processname: nginx-@type@
+# pidfile: /var/run/nginx-@type@.pid
+# config: /etc/nginx/nginx-@type@.conf
# Source function library
. /etc/rc.d/init.d/functions
-# Get network config
+# Source networking configuration.
. /etc/sysconfig/network
+nginx="/usr/sbin/nginx-@type@"
+svname="nginx (@type@)"
+prog=${nginx##*/}
+
+sysconfig="/etc/sysconfig/$prog"
+lockfile="/var/lock/subsys/$prog"
+pidfile="/var/run/$prog.pid"
+
+NGINX_CONF_FILE="/etc/nginx/$prog.conf"
+
# Get service config
-[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx
+[ -f $sysconfig ] && . $sysconfig
# Check that networking is up.
if is_yes "${NETWORKING}"; then
if [ ! -f /var/lock/subsys/network -a "$1" != stop -a "$1" != status ]; then
- msg_network_down nginx
+ msg_network_down "$svname"
exit 1
fi
else
exit 0
fi
-if [ -d "${HTTPD_CONF:-'/etc/nginx/nginx.conf'}" ]; then
- CFG="-f ${HTTPD_CONF:-'/etc/nginx/nginx.conf'}"
-elif [ -n "$HTTPD_CONF" ]; then
- echo "error: HTTPD_CONF='$HTTPD_CONF': not a file, not a directory"
- exit 1
-else
- CFG=""
-fi
+# configtest itself
+# must return non-zero if check failed
+# output is discarded if checkconfig is ran without details
+configtest() {
+ $nginx -t -c $NGINX_CONF_FILE
+}
+
+# wrapper for configtest
+checkconfig() {
+ local details=${1:-0}
+
+ if [ $details = 1 ]; then
+ # run config test and display report (status action)
+ show "Checking %s configuration" "$svname"; busy
+ local out
+ out=$(configtest 2>&1)
+ RETVAL=$?
+ if [ $RETVAL = 0 ]; then
+ ok
+ else
+ fail
+ fi
+ [ "$out" ] && echo >&2 "$out"
+ else
+ # run config test and abort with nice message if failed
+ # (for actions checking status before action).
+ configtest >/dev/null 2>&1
+ RETVAL=$?
+ if [ $RETVAL != 0 ]; then
+ show "Checking %s configuration" "$svname"; fail
+ nls 'Configuration test failed. See details with %s "checkconfig"' $0
+ exit $RETVAL
+ fi
+ fi
+}
start() {
# Check if the service is already running?
- if [ ! -f /var/lock/subsys/nginx ]; then
- msg_starting nginx
- daemon /usr/sbin/nginx $CFG $HTTPD_OPTS
+ if [ ! -f $lockfile ]; then
+ checkconfig
+ msg_starting "$svname"
+ daemon $nginx -c $NGINX_CONF_FILE
RETVAL=$?
- [ $RETVAL -eq 0 ] && touch /var/lock/subsys/nginx
+ [ $RETVAL -eq 0 ] && touch $lockfile
else
- msg_already_running nginx
+ msg_already_running "$svname"
fi
}
stop() {
# Stop daemons.
- if [ -f /var/lock/subsys/nginx ]; then
- msg_stopping nginx
- killproc --waitforname nginx --waitfortime 60 nginx $CFG
- # Delete pidfile only when nginx was called successfully
- if [ $? -eq 0 ]; then
- rm -f /var/lock/subsys/nginx /var/run/nginx.pid >/dev/null 2>&1
- fi
+ if [ -f $lockfile ]; then
+ msg_stopping "$svname"
+ killproc -p $pidfile $prog
+ RETVAL=$?
+ rm -f $lockfile $pidfile >/dev/null 2>&1
+ else
+ msg_not_running "$svname"
+ fi
+}
+
+reload() {
+ if [ -f $lockfile ]; then
+ checkconfig
+ msg_reloading "$svname"
+ killproc -p $pidfile $prog -HUP
+ RETVAL=$?
+ else
+ msg_not_running "$svname"
+ RETVAL=7
+ fi
+}
+
+condrestart() {
+ if [ ! -f $lockfile ]; then
+ msg_not_running "$svname"
+ RETVAL=$1
+ return
+ fi
+
+ checkconfig
+ stop
+ start
+}
+
+# Upgrade the binary with no downtime.
+# http://nginx.org/en/docs/control.html#upgrade
+# TODO: handle revert back on failed upgrade
+upgrade() {
+ local oldbin_pidfile="${pidfile}.oldbin"
+
+ checkconfig
+ show "Upgrading $svname"
+ killproc -p $pidfile $prog -USR2
+ RETVAL=$?
+ sleep 1
+ if [ -f $oldbin_pidfile ] && [ -f $pidfile ]; then
+ show "Upgrade: stopping old process"
+ killproc -p $oldbin_pidfile $prog -QUIT
+ return 0
else
- msg_not_running nginx
+ return 1
fi
}
+# Tell nginx to reopen logs
+# http://nginx.org/en/docs/control.html#logs
+reopen_logs() {
+ show "Reopening $svname logs"
+ killproc -p $pidfile $prog -USR1
+}
+
RETVAL=0
# See how we were called.
case "$1" in
start)
- start
+ start
;;
stop)
- stop
- ;;
- status)
- status nginx
- RETVAL=$?
+ stop
;;
restart)
+ checkconfig
stop
start
;;
- reload|force-reload|graceful)
- if [ -f /var/lock/subsys/nginx ]; then
- msg_reloading nginx
- killproc nginx -HUP
- RETVAL=$?
- else
- msg_not_running nginx
- RETVAL=7
- fi
+ try-restart)
+ condrestart 0
+ ;;
+ reload|graceful)
+ reload
+ ;;
+ force-reload|upgrade)
+ upgrade
+ ;;
+ reopen-logs)
+ reopen_logs
+ ;;
+ checkconfig|configtest)
+ checkconfig 1
+ ;;
+ status)
+ status --pidfile $pidfile $prog
+ RETVAL=$?
;;
*)
- msg_usage "$0 {start|stop|restart|reload|force-reload|graceful|status}"
+ msg_usage "$0 {start|stop|restart|try-restart|reload|force-reload|graceful|reopen-logs|checkconfig|status}"
exit 3
;;
esac