你如何正确地观看和重新加载Nginx conf?

时间:2013-05-29 14:47:52

标签: nginx

我有两个问题:

  • nginx -s reloadpkill -HUP -F nginx.pid
  • 之间是否有区别?
  • 观看Nginx配置文件的最简单方法是什么,在更改时测试配置文件(nginx -t),以及它是否通过重新加载Nginx。可以使用runit还是像Supervisor这样的流程管理器来完成?

2 个答案:

答案 0 :(得分:3)

至少在Unix上,由于声明代码,“重载”动作和HUP信号都被视为一个

ngx_signal_t  signals[] = {
    { ngx_signal_value(NGX_RECONFIGURE_SIGNAL),
      "SIG" ngx_value(NGX_RECONFIGURE_SIGNAL),
      "reload",
      ngx_signal_handler },
src/os/unix/ngx_process.c中的

。在ngx_signal_handler()中使用相同的comnmon代码

    case ngx_signal_value(NGX_RECONFIGURE_SIGNAL):
        ngx_reconfigure = 1;
        action = ", reconfiguring";
        break;
执行

,准备进行常见的重新配置。

要在修改文件时触发操作,您可以制作crontab并确定检查周期,也可以使用inotifywait

要确定nginx -t是否出错,请检查bash文件中的返回代码$?

nginx -t
if [ $? -eq 0 ] then;
    nginx -s reload
fi

注意:您也可以使用service nginx reload

(参见返回代码检查示例here

答案 1 :(得分:3)

#!/bin/bash

# NGINX WATCH DAEMON
#
# Author: Devonte
#
# Place file in root of nginx folder: /etc/nginx
# This will test your nginx config on any change and
# if there are no problems it will reload your configuration
# USAGE: sh nginx-watch.sh

# Set NGINX directory
# tar command already has the leading /
dir='etc/nginx'

# Get initial checksum values
checksum_initial=$(tar --strip-components=2 -C / -cf - $dir | md5sum | awk '{print $1}')
checksum_now=$checksum_initial

# Start nginx
nginx

# Daemon that checks the md5 sum of the directory
# ff the sums are different ( a file changed / added / deleted)
# the nginx configuration is tested and reloaded on success
while true
do
    checksum_now=$(tar --strip-components=2 -C / -cf - $dir | md5sum | awk '{print $1}')

    if [ $checksum_initial != $checksum_now ]; then
        echo '[ NGINX ] A configuration file changed. Reloading...'
        nginx -t && nginx -s reload;
    fi

    checksum_initial=$checksum_now

    sleep 2
done