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

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?

From the nginx wiki pages

The master process can handle the following signals: TERM, INT Quick shutdown

  • QUIT Graceful shutdown
  • KILL Halts a stubborn process
  • HUP Configuration reload. Start the new worker processes with a new configuration. Gracefully shutdown the old worker processes
  • USR1 Reopen the log files
  • USR2 Upgrade Executable on the fly
  • WINCH Gracefully shutdown the worker processes
  • The syntax is as follows to

     kill -HUP $( cat /path/to/ )

    OR find nginx pid with the pgrep command or ps command:

    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

    Type the following command as root user:

    kill -HUP 4333

    If you are using Nginx version 0.7.53+

    Pass the -s reload option:
    # nginx -s reload
    # /usr/local/nginx/sbin -s reload

    If you are using Debian / CentOS / RHEL / Fedora / Ubuntu Linux try

    # /etc/init.d/nginx reload

    If you are using FreeBSD try

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

    If you are using OpenBSD try

    # /usr/sbin/nginx -s reload
    # /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

    Posted by: SXI ADMIN

    The author is the creator of SXI LLC 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.