Blog

03/06/2019

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/nginx.pid )

    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
    OR
    # /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
    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

    Posted by: SXI ADMIN

    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.

    14/08/2019

    How to KVM, QEMU start or stop virtual machine from command line (CLI)

    KVM or Kernel Based Virtual Machine is a popular virtualization technology. It allows you to run virtual guest machines over a host machine. To start...
    14/08/2019

    How to Docker backup Saving and restoring your volumes

    Running a Docker volume backup First, we spin up a temporary container, and we mount the backup folder and the target Docker volume to this container....
    12/08/2019

    How to Start and Enable Firewalld on CentOS 7

    In this article, we discuss how to start and enable firewalld. It is highly recommended that you have a firewall protecting your server.Pre-Flight CheckThese...