Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Update Nginx config file in a container with zero down time

We are using Nginx as a reverse proxy for docker-cloud services. A script is implemented to update the config file of Nginx whenever new service deploys on docker cloud or if service gets new url on docker-cloud.

The Nginx and the script have been run in a docker container separately. The Nginx config file is mounted in Host(ECS). After updating the config file using script, it needs to reload the Nginx in order to apply the changes.

First, I would like to know if this is the best way of updating Nginx config file and also what is the best way to reload the Nginx without any downtime?

Shall I recreate the Nginx container after each update? if so, how?

or it's fine to reload the Nginx from Host by monitoring the changes in the config file(using a script) and reload it with below command?

docker exec NginxcontainerID | nginx -s reload    
like image 710
Matrix Avatar asked Mar 04 '17 16:03

Matrix


1 Answers

Shall I recreate the Nginx container after each update? if so, how?

No, You just need to reload nginx service most of the time. You can use:

docker exec nginxcontainername/id nginx -s reload

or

docker kill -s HUP nginxcontainername/id 

Another option would be using a custom image and check nginx config checksum and reload nginx when ever it changes. Example script:

nginx "$@"
oldcksum=`cksum /etc/nginx/conf.d/default.conf`

inotifywait -e modify,move,create,delete -mr --timefmt '%d/%m/%y %H:%M' --format '%T' \
/etc/nginx/conf.d/ | while read date time; do

    newcksum=`cksum /etc/nginx/conf.d/default.conf`
    if [ "$newcksum" != "$oldcksum" ]; then
        echo "At ${time} on ${date}, config file update detected."
        oldcksum=$newcksum
        nginx -s reload
    fi

done

You need to install inotifywait package.

like image 165
Farhad Farahi Avatar answered Sep 28 '22 04:09

Farhad Farahi