---
title: "Nginx 重启 nginx 停止 修改配置后生效"
author: "夜鸣猪"
date: "2009-12-29T10:48:22Z"
tags: []
categories: ["编程语言"]
slug: "nginx-nginx"
draft: false
lastmod: "2010-01-06T15:49:01Z"
id: 558904
url: "https://hlee.iteye.com/blog/558904"
---

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

> **某网民** writes:
> 
> ps aux| grep nginx找到nginx的进程号
> 
> kill -HUP xxxx(进程号)


于是，我就想那就这样呗：

ps ax |grep mongrel | awk '{print $1}' |xargs kill -HUP

其中<span style="color:olive">HUP是重新载入配置</span>

后来，又看到一些

简单说就是

```bash
killall -HUP nginx
```


nginx是超级稳定的服务器，一般不会因为超载问题而需要重启，重启的目的一般都是修改配置文件后需要加载一下。

最开始的时候，我是用最直接的重启方式


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

```java

kill -HUP $pid($pid就是nginx master进程的进程号)
```


我一般这样用


```java
kill -HUP `cat /data/nginx/logs/nginx.pid`
```


这种方式的好处是实现“平滑重启”，在ps -aux中可以看到，nginx首先启动新进程，旧的进程仍然提供服务，在一段时间后，旧的进程服务结束就自动关闭，剩下新进程继续服务。但是这种方式也是有缺点的，如果配置文件有误，或者资源冲突，则重启失效，但nginx并没有任何的提示！这就会时常发现改动的配置文件没有生效，又比较难找到问题。

所以，最后杂和了一下问题，弄了一个nginx.sh，这个版本的nginx.sh还是没有解决kill -HUP的资源冲突的问题，但解决了配置文件的问题。资源冲突的比如80端口被占用、日志文件目录没有创建这种的，我再想想办法。


```java
#!/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

---------------------------------------------


```

```java
kill -HUP `cat /data/nginx/logs/nginx.pid`
```


这句话比较长，不容易记住，现在发现一种更为简洁的办法：


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

```java
kill -USR1
kill -QUIT
......
```


都可以用killall简化执行。


```ruby

#
# 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

```