DEV Community

Cover image for Automate Nginx conf file validation using Docker
Davinder
Davinder

Posted on • Originally published at devkamboj.in on

Automate Nginx conf file validation using Docker

Nginx is an open-source web server, HTTP cache, reverse proxy server, and load balancer and is used by over 50% of the world’s busiest websites. And Docker with Nginx is a perfect combination to automate your app deployment.

Automated testing is an integral part of CI/CD pipelines. It would be nice to have one more check for the Nginx config file.

Automating Nginx configuration file validation using docker.

docker run -v $pwd/config:/data/ --name check-nginx-conf -t -a stdout nginx:latest nginx -t -c /data/nginx.conf

What’s happing here:

  • -v $pwd/config:/data/ : bind your $pwd/config to /data inside the container. Make sure you have nginx.conf inside config folder and other directories that your config file is using ext SSL folder.
  • —name check-nginx-conf : Name of the container.
  • -t -a stdout : Attach to standard stream stdout to get the logs.
  • nginx:latest : Nginx Image version.
  • nginx -t -c : -t to validate the config file and -c to use an alternative configuration file instead of a default file.

Your should see this output if your config is validnginx-conf-valid-output

Further, you could use grep command to check if test is successful

docker run -v $pwd/config:/data/ --name check-nginx-conf -t -a stdout nginx:latest nginx -t -c /data/nginx.conf | grep "test is successful" | wc -l

grep “test is successful” will return the line which contains test is successful and wc -l to count the number of lines.

if the syntax is valid, you will get 1 otherwise 0

Discussion (0)