67

When is it necessary to restart nginx and reload will not suffice?
Does it make a difference if an extension like passenger is used?

Should the service be restarted if it consumes too much memory. Any other reasons for restarting Nginx, particularly after a configuration change either in an extension or a Nginx core config?

After making a configuration change, one can either restart or reload nginx, via the binary itself or the init.d script "/etc/init.d/nginx -h" on Ubuntu. Which method should be preferred?

Ztyx
  • 14,100
  • 15
  • 78
  • 114
deepak
  • 7,230
  • 5
  • 24
  • 26

3 Answers3

69

Reloading nginx is safer than restarting because before old process will be terminated, new configuration file is parsed and whole process is aborted if there are any problems with it.

On the other hand when you restart nginx you might encounter situation in which nginx will stop, and won't start back again, because of syntax error.

Reloading terminates the old process, so any memory leaks should be cleared anyway.

samuil
  • 5,001
  • 1
  • 37
  • 44
  • will a ngixn extension like http://www.modrails.com/documentation/Users%20guide%20Nginx.html make any difference ? – deepak Nov 27 '12 at 09:23
  • Well, when using passenger it is usually wiser to restart application server instead of web server (in this case, by touching `tmp/restart.txt` file), but apart from that it should work exactly the same. – samuil Nov 27 '12 at 09:40
  • 4
    What if I `nginx -t` before to check syntax? What's the difference then? – Nick T Dec 21 '15 at 16:06
  • 4
    From nginx guide: Once the master process receives the signal to reload configuration, it checks the syntax validity of the new configuration file and tries to apply the configuration provided in it. If this is a success, the master process starts new worker processes and sends messages to old worker processes, requesting them to shut down. Otherwise, the master process rolls back the changes and continues to work with the old configuration. – samuil Dec 22 '15 at 08:02
33

I've experienced a case where I needed to restart nginx to have listen directives for a new IP adress kick in.

Ztyx
  • 14,100
  • 15
  • 78
  • 114
  • 2
    I had the same experience just now... I was going crazy until I figured this out!! – Muc Oct 23 '19 at 14:21
  • Ditto!, I spent days figuring out why docker containers can't resolve hostnames, and by simply restarting nginx instead of reloading, fixed it. – The.Wolfgang.Grimmer Jul 30 '22 at 11:53
15

Ztyx is right - restarting when changing a listen directive is required in some cases.

With 1.6.x, you can reload when changing the listen IP address, but you need to restart when listening from "*:80" to an IP address "x.x.x.x:80".

I have only confirmed this with IPv4, behavior for IPv6 may be similar.

Andy
  • 159
  • 1
  • 4