5

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    
Matrix
  • 2,399
  • 5
  • 28
  • 53

1 Answers1

11

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.

Farhad Farahi
  • 35,528
  • 7
  • 73
  • 70
  • Thanks for the reply. it helped. I'm wondering if you know what is the best way to have highly available Nginx container, an architecture for failover? I have asked it under this question but didn't get any reply yet. http://stackoverflow.com/questions/41379428/how-to-set-up-two-nginx-containers-as-a-reverse-proxy-in-an-active-passive-set-u – Matrix Mar 07 '17 at 08:39