122

I use nginx as a reverse proxy. Whenever I update the config for it using

sudo "cp -r #{nginx_config_path}* /etc/nginx/sites-enabled/"
sudo "kill -s HUP `cat /var/run/nginx.pid`"

I face a brief downtime. How can I avoid that?

  • 1
    Are those meant to be command line commands? I've never seen anyone wrap an entire sudo command in quotes like that, it may not be necessary. – brianmearns Apr 12 '16 at 12:59
  • 4
    Just a general comment: I think the standard/recommended practice is create a soft/symbolic link for your site configuration under sites-enabled, not copy it. Not related to your particular issue, but you may want to look into that. – brianmearns Apr 12 '16 at 13:01
  • 1
    You should not be facing a downtime. kill HUP is the way to do a graceful reload in nginx. – Jonathan Vanasco May 6 '16 at 16:40
181

Run service nginx reload or /etc/init.d/nginx reload

It will do a hot reload of the configuration without downtime. If you have pending requests, then there will be lingering nginx processes that will handle those connections before it dies, so it's an extremely graceful way to reload configs.

Sometimes you may want to prepend with sudo

  • 10
    Both of those should do exactly what the question states: send SIGHUP to the nginx master process. There should not be a difference. nginx.org/en/docs/control.html – Gnarfoz Jul 27 '12 at 12:36
  • When I issue the command on CentOS it keeps saying "Usage /etc/init.d/nginx (start..stop...restart..reload)" .. and that's exactly how I used it. Within the file /init.d/nginx I found kill -HUP cat $PIDFILE || echo -n " can't reload" – mashup Apr 13 '15 at 18:21
  • 1
    do you know what the difference is between service nginx reload and nginx -s reload? If I run the former, I get this output: Reloading nginx configuration: nginx., but my changes aren't updated. If I run the latter, I get no output, but my changes are reflected. – Ryan Quinn Apr 21 '17 at 3:46
  • I just tried this after adding a log_not_found directive but found that I had to actually do a restart to get it to work. I guess reloading does not work for all directives? – mydoghasworms Jun 22 '17 at 12:24
81

Run /usr/sbin/nginx -s reload

See http://wiki.nginx.org/CommandLine for more command line options.

  • Finally, a command that does work in Debian Jessie. – danger89 Jun 30 '17 at 11:49
  • 1
    This is a better way. Because your server doesn't down if your configurations have errors (just shows errors in this case). – Mir-Ismaili Sep 19 '18 at 14:51
  • if nginx default pid not in the default location , need '-p'. ie: ` /opt/gitlab/embedded/sbin/nginx -s reload -p /var/opt/gitlab/nginx` – qxo Jan 9 at 1:59
9

No, you are incorrect, you aren't supposed to be facing any downtime with the procedure you describe. (Nginx can do not only configuration reload on the fly without any downtime, but even the upgrade of the executable on the fly, still without any downtime.)

As per http://nginx.org/docs/control.html#reconfiguration, sending the HUP signal to nginx makes sure that it performs a graceful restart, and, if the configuration files are incorrect, the whole procedure is abandoned, and you're left with the nginx as before sending the HUP signal. At no point should any downtime be possible.

In order for nginx to re-read the configuration file, a HUP signal should be sent to the master process. The master process first checks the syntax validity, then tries to apply new configuration, that is, to open log files and new listen sockets. If this fails, it rolls back changes and continues to work with old configuration.

2

Usually, reloading configuration file of a service should not affect the running service. However, this depends on how the SIGHUP signal is processed.

If a specific service is experiencing a downtime during reload, this can be circumvented by running the same service on multiple servers preferably using a load balancer. In this case, you can take out one server at a time and reload/restart it. Then, it can be re-added after confirming it is OK.

  • While this does not directly answer the question, this is definitely a best-practice scenario that the OP would be smart to follow for avoiding downtime in general. – Andrew M. Apr 11 '12 at 17:29
  • 1
    Details on how nginx handles different signals: nginx.org/en/docs/control.html – Gnarfoz Jul 27 '12 at 12:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.