DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’» is a community of 967,911 amazing developers

We're a place where coders share, stay up-to-date and grow their careers.

Create account Log in
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

Top comments (0)

πŸ‘‹ Hey, my name is Noah and I’m the one who set up this ad. My job is to get you to join DEV, so if you fancy doing me a favor, I’d love for you to create an account.

If you found DEV from searching around, here are a couple of our most popular articles on DEV: