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 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.
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 syntax is ok you will get 1 otherwise 0
I Hope this Post will help you.