Gracefully Restart Nginx Web Server After Changes Made In a Config File

last updated in Categories , ,

I know how to gracefully restart Apache web server under Unix like operating system. I made changes to nginx.conf. How do I gracefully restart Nginx web server? How do I make changes in a Nginx server config file to take effect without restarting the Nginx server itself without interrupting users’ current session?

Nginx is a free and open source web server that runs on Linux and Unix-like systems. Nginx is very fast and lightweight server for static and dynamic workloads. This page shows how to gracefully restart Nginx web server after config changes.
Gracefully Restart Nginx On Linux, BSD and Unix

ADVERTISEMENTS


How to gracefully restarts the Nginx service

The syntax is as follows for GNU/Linux with systemd:
sudo systemctl reload nginx
For older Linux distros with sysv init, try the service command:
sudo service nginx reload
When executed reload option the master Nginx process shuts down the child processes, loads the new configuration, and starts new child processes without disturbing operations. Hence, we prefer reload over restart. In other words, do not use any one of the following Linux commands for graceful restarts:
sudo systemctl restart nginx
OR
sudo service nginx restart

Gracefully Restart Nginx Web Server

The master Nginx process can handle the following signals:

  1. TERM, INT : Quick shutdown
  2. QUIT : Graceful shutdown
  3. KILL : Halts a stubborn process
  4. HUP : Configuration reload. Start the new worker processes with a new configuration. Gracefully shutdown the old worker processes
  5. USR1 : Reopen the log files
  6. USR2 : Upgrade Executable on the fly
  7. WINCH : Gracefully shutdown the worker processes

Universal syntax to gracefully restart Nginx on Linux or Unix-like systems

Try the following combination of the kill command and cat command as the root user:
sudo kill -HUP $( cat /path/to/nginx.pid )
One can find nginx pid with the pgrep commandor ps command as follows::

pgrep nginx
ps aux | grep [n]ginx

Sample outputs:

root      4333  0.0  0.4  70776  9352 ?        Ss   Nov24   0:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx     9921  1.0  0.5  70776  9888 ?        S    Dec05  19:24 nginx: worker process      
nginx     9922  1.0  0.5  70776 10240 ?        S    Dec05  19:42 nginx: worker process      
nginx     9923  0.0  0.4  70776  8724 ?        S    Dec05   0:00 nginx: cache manager process

Next, type the following command as root user:

kill -HUP 4333 
sudo kill -HUP 4333

If you are using Nginx version 0.7.53+

The latest version of Nginx makes it really easy to gracefully reload and restart web service on Unix and Linux. All you have to do is pass the -s reload option:# nginx -s reload
OR
# /usr/local/nginx/sbin -s reload

Debian / CentOS / RHEL / Fedora / Ubuntu Linux try

# /etc/init.d/nginx reload

FreeBSD user

# /usr/local/etc/rc.d/nginx reload

OpenBSD user

# /usr/sbin/nginx -s reload
OR
# /etc/rc.d/nginx reload

How do I reload / gracefully restart chrooted nginx server?

Type the following command:
# /usr/sbin/chroot /jail /usr/local/nginx/sbin/nginx -s reload

How to reload Nginx inside Docker or LXD container

The syntax is as follows for Docker:
docker container exec {container_name_here} nginx -s reload
OR
docker kill -s HUP {container_name_here}
LXD users can type the following command:
lxc exec {container_name_here} -- nginx -t reload

Conclusion

You learned how to gracefully reload the Nginx web server using various command-line methods. Please see Nginx docs here for more info.

Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.


ADVERTISEMENTS