Nginx 重启 nginx 停止 修改配置后生效

[url=http://dl.iteye.com/topics/download/cd74b46b-be55-3145-8674-d9c2cc128845]Nginx的rpm包自己打的[/url]
搜到别人说

[quote="某网民"]ps aux| grep nginx找到nginx的进程号

kill -HUP xxxx(进程号)[/quote]

于是,我就想那就这样呗:

ps ax |grep mongrel | awk '{print $1}' |xargs kill -HUP[/quote]

其中[color=olive]HUP是重新载入配置[/color]

后来,又看到一些

简单说就是
[code="bash"]killall -HUP nginx


nginx是超级稳定的服务器,一般不会因为超载问题而需要重启,重启的目的一般都是修改配置文件后需要加载一下。

最开始的时候,我是用最直接的重启方式

killall -9 nginx;/data/nginx/sbin/nginx


如果机器比较慢,kill进程时一瞬间杀不完,再执行一次即可。这种重启方式不是特别安全,如果配置有误,则会重启失败,需要重新修改配置文件然后再启动,期间会消耗一点时间。不过对于目前普遍还是不怎么严格的http界而言,这点时间还不至于产生太大损失,只要不是在关键时刻搞出来就好。如果希望沿用这种重启办法,我提议还是先好好测试吧。

后来我在nginx.net上看到了一种更奇妙的重启

kill -HUP $pid($pid就是nginx master进程的进程号)


我一般这样用

kill -HUP `cat /data/nginx/logs/nginx.pid`


这种方式的好处是实现“平滑重启”,在ps -aux中可以看到,nginx首先启动新进程,旧的进程仍然提供服务,在一段时间后,旧的进程服务结束就自动关闭,剩下新进程继续服务。但是这种方式也是有缺点的,如果配置文件有误,或者资源冲突,则重启失效,但nginx并没有任何的提示!这就会时常发现改动的配置文件没有生效,又比较难找到问题。

所以,最后杂和了一下问题,弄了一个nginx.sh,这个版本的nginx.sh还是没有解决kill -HUP的资源冲突的问题,但解决了配置文件的问题。资源冲突的比如80端口被占用、日志文件目录没有创建这种的,我再想想办法。

#!/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
fi

else

echo "######## error: ########"
cat ${BASE_DIR}nginx/logs/nginx.start

fi

---------------------------------------------

kill -HUP `cat /data/nginx/logs/nginx.pid`


这句话比较长,不容易记住,现在发现一种更为简洁的办法:

killall -HUP nginx


killall会将HUP指令传到所有的nginx进程里,包括master和worker,但是worker进程并不认识HUP指令,所以事实上只有master进程获取了HUP,并执行重启。

于是乎,nginx的kill指令:

kill -USR1
kill -QUIT
......


都可以用killall简化执行。


#
# Spec File for Nginx
#

# Setup Subversion
%define _svn_root %_topdir/SOURCES/
%define _svn_src 'https://10.0.0.33/svn/realview/trunk/'
%define _unpackaged_files_terminate_build 0


Summary: RealWorx software application
Name: nginx
Version: 0.7.64
Release: 1
License: commercial
Group: Applications/Communications
Distribution:
Vendor:
Packager:
BuildRoot: %{_topdir}/tmp/%{name}-%{version}-%{release}

Source0: nginx-0.7.64.tar.gz
Source1: ngx_http_auth_pam_module-1.1.tar.gz
BuildArch: noarch

#Requires:

%description

%prep
#realview dose not start with user realwork, so it doest check the user and usergroup.
#if ! /usr/bin/id realworx &>/dev/null; then
# /usr/sbin/useradd -s /bin/bash -c "Realworx application user" -M realworx || \
# %logmsg "Unexpected error adding user \"realworx\"."
#fi
#
#if ! /usr/sbin/groupmod realworx &>/dev/null; then
# /usr/sbin/groupadd realworx
# /usr/sbin/usermod -g realworx realworx || \
# %logmsg "Failed to change default group for user \"realworx\" to group \"realworx\"."
#fi


#cd %{_svn_root}
echo "prep"
#%setup -q
%setup -q -b 0 -b 1
#make soft ln to source code under build path.
#mkdir -p %{_topdir}/BUILD/%{name}-%{version}
#lndir -silent %{_svn_root} %{_topdir}/BUILD/%{name}-%{version}
#mkdir -p %{_topdir}/BUILD/ngx_http_auth_pam_module-1.1
#lndir -silent %_topdir/SOURCES/ngx_http_auth_pam_module-1.1 %{_topdir}/BUILD/ngx_http_auth_pam_module-1.1

%build
echo Building %{name}-%{version}-%{release}
./configure --add-module=../ngx_http_auth_pam_module-1.1
#./configure --prefix=%{buildroot} --bindir=%{buildroot}/sunrise/bin
make

%install
make install DESTDIR=%{buildroot}

%files
%dir %{_prefix}/local/nginx
%config(noreplace) %{_prefix}/local/nginx/conf/nginx.conf
%{_prefix}/local/nginx/conf/fastcgi_params
%{_prefix}/local/nginx/conf/fastcgi_params.default
%{_prefix}/local/nginx/conf/koi-utf
%{_prefix}/local/nginx/conf/koi-win
%{_prefix}/local/nginx/conf/mime.types
%{_prefix}/local/nginx/conf/mime.types.default
%{_prefix}/local/nginx/conf/nginx.conf.default
%{_prefix}/local/nginx/conf/win-utf
%{_prefix}/local/nginx/html/50x.html
%{_prefix}/local/nginx/html/index.html
%{_prefix}/local/nginx/sbin/nginx
%{_prefix}/local/nginx/sbin/nginx.old
%exclude %{_prefix}/src/*
%exclude %{_prefix}/lib/*

%changelog
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值