本文记录了我在应用 Nginx 的过程中总结到的经验,将不定期更新。
- 编译时关闭 debug,能大大减小编译出的文件大小,前提是确保编译环境不会出错。
修改源码中的 gcc 文件
将文件最后的 debug 选项注释掉重新编译,看是不是文件变小了?
# CFLAGS="$CFLAGS -g"
604K nginx
3.7M nginx.old
- 启动脚本,放到 /etc/init.d 下,这样能方便不少
For CentOS:
# v.0.0.1
# create by jackbillow at 2007.10.15
# nginx - This shell script takes care of starting and stopping nginx.
#
# chkconfig: - 60 50
# description: nginx [engine x] is light http web/proxy server
# that answers incoming ftp service requests.
# processname: nginx
# config: /usr/local/nginx/conf/nginx.conf
nginx_path="/usr/local/nginx"
nginx_pid="/var/run/nginx/nginx.pid"
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
# Check that networking is up.
[ ${NETWORKING} = "no" ] && exit 0
[ -x $nginx_path/sbin/nginx ] || exit 0
RETVAL=0
prog="nginx"
start() {
# Start daemons.
ulimit -n 65535
if [ -e $nginx_pid -a ! -z $nginx_pid ];then
echo "nginx already running...."
exit 1
fi
if [ -e $nginx_path/conf/nginx.conf ];then
echo -n $"Starting $prog: "
$nginx_path/sbin/nginx -c $nginx_path/conf/nginx.conf &
RETVAL=$?
[ $RETVAL -eq 0 ] && {
touch /var/lock/subsys/$prog
success $"$prog"
}
echo
else
RETVAL=1
fi
return $RETVAL
}
# Stop daemons.
stop() {
echo -n $"Stopping $prog: "
killproc -d 10 $nigx_path/sbin/nginx
RETVAL=$?
echo
[ $RETVAL = 0 ] && rm -f $nginx_pid /var/lock/subsys/$prog
}
# See how we were called.
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
stop
start
;;
reload)
kill -HUP `cat $nginx_pid`
;;
status)
status $prog
RETVAL=$?
;;
*)
echo $"Usage: $0 {start|stop|reconfigure|status}"
exit 1
esac
exit $RETVAL
For Ubuntu:
### BEGIN INIT INFO
# Provides: nginx
# Required-Start: $all
# Required-Stop: $all
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: starts the nginx web server
# Description: starts nginx using start-stop-daemon
### END INIT INFO
PATH=/usr/local/nginx/sbin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=/usr/local/nginx/sbin/nginx
NAME=nginx
DESC=nginx
PID=/var/run/nginx.pid
test -x $DAEMON || exit 0
# Include nginx defaults if available
if [ -f /etc/default/nginx ] ; then
. /etc/default/nginx
fi
set -e
case "$1" in
start)
echo -n "Starting $DESC: "
ulimit -SHn 51200
start-stop-daemon --start --quiet --exec $DAEMON -- $DAEMON_OPTS > /dev/null 2>/dev/null
echo "$NAME."
;;
stop)
echo -n "Stopping $DESC: "
kill `cat $PID`
echo "$NAME."
;;
restart|force-reload)
echo -n "Restarting $DESC: "
kill `cat $PID`
sleep 1
start-stop-daemon --start --quiet --exec $DAEMON -- $DAEMON_OPTS > /dev/null 2>/dev/null
echo "$NAME."
;;
reload)
echo -n "Reloading $DESC configuration: "
kill -HUP `cat $PID`
echo "$NAME."
;;
*)
N=/etc/init.d/$NAME
echo "Usage: $N {start|stop|restart|reload|force-reload}" >&2
exit 1
;;
esac
exit 0
- 检查配置文件错误
一切 ok 的话就说明没问题
- Nginx 的控制信号
不管是什么信号,都是要发给 Nginx 的 Master 进程的,所以需要首先获得 Nginx Master 的 PID:
app 2683 0.0 0.0 4220 752 pts/10 R+ 09:11 0:00 grep nginx
root 7491 0.0 0.0 28892 2084 ? S 06:08 0:00 nginx: master process /usr/local/nginx/sbin/nginx
www-data 10732 0.1 0.0 48504 2068 ? S 06:26 0:11 nginx: worker process
www-data 10733 0.1 0.0 48504 2304 ? S 06:26 0:11 nginx: worker process
这个例子中,7491 就是 Nginx Master 的 PID
kill -HUP PID: 平滑重启 Nginx,相当于 reload config
kill -QUIT PID: 处理完当前请求后结束进程
kill -TERM PID: 立即结束进程
- 在生产环境中对 Nginx 进行平滑升级
Nginx 支持在生产环境中进行版本升级或者增删模块,之间不会中止 Web 服务,用户不会感受到任何影响。
升级步骤:
1. 备份旧版本并记下当前运行的 Nginx Master 进程的 PID
ps aux|grep nginx
2. 下载需要的源码包,进行模块配置,然后 make && make install 装到默认路径就可以了
3. 启动新版本的 Nginx, 发送 USR2 信号到旧版本的 PID,这样做会启动新版本,但是旧版本也不会退出, 两个版本会同时运行
4.如果没有什么问题的话可以停掉所有的旧版本的 worker 了,发送 WINCH 信号到旧版本 PID,此时用 ps aux|grep nginx 可以看到旧版本 Nginx 的 worker 会逐渐退出,最后所有请求都会由新版本的 Nginx 处理
5. 最后测试新版本没有问题后可以彻底停掉旧版本,发送 QUIT 信号到旧版本的 PID
如果测试后想恢复到旧版本的话,发送 HUP 信号到旧版 PID
关闭新版本,发送 QUIT 到新版本 PID,如果着急,也可以用 TERM
最后把备份的 Nginx 替换回去
mv /usr/local/nginx/sbin/nginx.old /usr/local/nginx/sbin/nginx
» 多级 Nginx 传递客户端 IP SoniTech wrote:
[...] 后端的 Nginx 需要安装一个 Module: NginxHttpRealIpModule,编译的时候默认不包含此 Module,需要重新编译安装 Nginx,configure 的时候加上 –with-http_realip_module,Nginx 升级或者添加/删除 Module 时支持热切换,可以避免中断服务。 [...]
Link | May 21st, 2010 at 10:15