2.Nginx日常维护技巧
Nginx日常维护技巧
- Nginx配置正确性检查
nginx提供了配置文件调试功能,可以快速定义配置文件存在的问题。执行如下命令检测配置文件的正确性:
[root@localhost 桌面]# which nginx
/usr/local/nginx/sbin/nginx
我把nginx命令的路径写入到PATH里面,所以在执行nginx这条命令的时候我就不需要写绝对路径:
[root@localhost 桌面]# nginx –t -t 检查配置文件的正确性
nginx: the configuration file /usr/local/nginx//conf/nginx.conf syntax is ok
nginx: [warn] 65535 worker_connections exceed open file resource limit: 32768
nginx: configuration file /usr/local/nginx//conf/nginx.conf test is successful
[root@localhost 桌面]# nginx -t -c /usr/local/nginx/conf/nginx.conf -c 制定nginx配置文件的路径,默认为/usr/local/nginx/conf/nginx.conf
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: [warn] 65535 worker_connections exceed open file resource limit: 32768
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
另外,nginx也提供了查看版本信息和相关编译信息的功能:
[root@localhost 桌面]# nginx -v
nginx version: nginx/1.2.1
[root@localhost 桌面]# nginx -V
nginx version: nginx/1.2.1
configure arguments: --prefix=/usr/local/nginx/ --with-http_stub_status_module --with-http_gzip_static_module --user=www --group=www
- Nginx的启动,关闭,和重启
nginx对进程的控制能力非常强大,可以通过信号命令控制进程。常用的信号有:
QUIT:表示处理完当前请求后关闭
HUP:表示重新加载配置,即关闭原有的进程开启新的工作进程。此操作不会用端用户的访问请求,因此,可以通过此信号平滑的重启nginx。
USR1:用于nginx的日志切换,也就是重新打开一个日志文件。例如:每天要生成一个新的日志文件的时候,可以使用这个信号老控制。
USR2:用于平滑升级可执行程序。
WINCH:从容关闭工作进程。
nginx的启动非常简单:
/usr/local/nginx/sbin/nginx
即可完成对nginx的启动。在nginx启动以后,我们可以查看到nginx的工作进程。
[root@localhost 桌面]# cat /usr/local/nginx/logs/nginx.pid
6597
如果我们要不间断的重启nginx服务,我们需要
kill –xxx pid #xxx就是信号名
如:
[root@localhost 桌面]# kill -HUP `cat /usr/local/nginx/logs/nginx.pid`
在实际生产中,我们需要为nginx写服务启动脚本:
#!/bin/bash
# nginx Startup script for the Nginx HTTP Server
# it is v.0.0.2 version.
# chkconfig: - 85 15
# description: Nginx is a high-performance web and proxy server.
# It has a lot of features, but it's not for everyone.
# processname: nginx
# pidfile: /var/run/nginx.pid
# config: /usr/local/nginx/conf/nginx.conf
nginxd=/usr/local/nginx/sbin/nginx
nginx_config=/usr/local/nginx/conf/nginx.conf
nginx_pid=/usr/local/nginx/logs/nginx.pid
RETVAL=0
prog="nginx"
# 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 $nginxd ] || exit 0
# Start nginx daemons functions.
start() {
if [ -e $nginx_pid ];then
echo "nginx already running...."
exit 1
fi
echo -n $"Starting $prog: "
daemon $nginxd -c ${nginx_config}
RETVAL=$?
echo
[ $RETVAL = 0 ] && touch /var/lock/subsys/nginx
return $RETVAL
}
# Stop nginx daemons functions.
stop() {
echo -n $"Stopping $prog: "
killproc $nginxd
RETVAL=$?
echo
[ $RETVAL = 0 ] && rm -f /var/lock/subsys/nginx /usr/local/nginx/logs/nginx.pid
}
reload() {
echo -n $"Reloading $prog: "
#kill -HUP `cat ${nginx_pid}`
killproc $nginxd -HUP
RETVAL=$?
echo
}
# See how we were called.
case "$1" in
start)
start
;;
stop)
stop
;;
reload)
reload
;;
restart)
stop
start
;;
status)
status $prog
RETVAL=$?
;;
*)
echo $"Usage: $prog {start|stop|restart|reload|status|help}"
exit 1
esac
exit $RETVAL