<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Simon Drouin</title>
    <description>The latest articles on DEV Community by Simon Drouin (@simdrouin).</description>
    <link>https://dev.to/simdrouin</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F20064%2Fd0eaf939-bc60-4f3c-b5a2-a7b6cfa413e3.jpg</url>
      <title>DEV Community: Simon Drouin</title>
      <link>https://dev.to/simdrouin</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/simdrouin"/>
    <language>en</language>
    <item>
      <title>Validate your Nginx configuration files easily with Docker</title>
      <dc:creator>Simon Drouin</dc:creator>
      <pubDate>Sat, 14 Apr 2018 12:15:33 +0000</pubDate>
      <link>https://dev.to/simdrouin/validate-your-nginx-configuration-files-easily-with-docker-4ihi</link>
      <guid>https://dev.to/simdrouin/validate-your-nginx-configuration-files-easily-with-docker-4ihi</guid>
      <description>&lt;p&gt;&lt;em&gt;This is my first post, please be indulgent and let me know if anything could be improved.&lt;/em&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Situation
&lt;/h1&gt;

&lt;p&gt;Let's suppose you have your web servers running any version of Nginx, and you are set up for automatic deployment of the Nginx config files (through Jenkins, StriderCD, TeamCity, Travis CI, Strider CD, or any other CICD tool you like) from a Git repository or any other source.&lt;/p&gt;

&lt;p&gt;With that setup, everything runs smoothly until someone pushes a modification to the config files that has typos or errors in it, in which case you don't want these files to ever get to the Nginx servers.&lt;/p&gt;

&lt;p&gt;To avoid such a situation, you'd want your validations to occur as soon as possible in the update process.&lt;/p&gt;

&lt;p&gt;This happened to us a few times, so I decided to write a small script that would validate the &lt;code&gt;nginx.conf&lt;/code&gt; files prior to allowing the deployment.&lt;/p&gt;

&lt;h1&gt;
  
  
  First steps
&lt;/h1&gt;

&lt;p&gt;The first thing we wanted to do was to be able to validate the Nginx config files from a server with Nginx.  That was pretty easy to do with this command:&lt;br&gt;
&lt;code&gt;nginx -c /etc/nginx/nginx.conf -t&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;With valid config files, it returns this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With invalid config files, it returns something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;2018/04/13 21:26:15 [emerg] 1#1: unexpected ";" in /etc/nginx/nginx.conf:155
nginx: [emerg] unexpected ";" in /etc/nginx/nginx.conf:155
nginx: configuration file /etc/nginx/nginx.conf test failed
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  Dockerizing this validation
&lt;/h1&gt;

&lt;p&gt;However, this requires that the person editing the config file had Nginx installed on its machine. This is not always the case, and we didn't want to install Nginx on the build server either (there could be multiple versions on different servers too...), so we turned to Docker as we already use it a lot.&lt;/p&gt;

&lt;p&gt;After a few minutes, we were able to come up with a similar command that could run from every system that has Docker installed:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker run --rm -t -a stdout --name my-nginx -v $PWD/config/:/etc/nginx/:ro nginx:latest nginx -c /etc/nginx/nginx.conf -t
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Docker command explained briefly:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;--rm&lt;/code&gt; : Removes the container after execution completes&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;-t -a stdout&lt;/code&gt; : Outputs the container logs on the standard output&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;--name my-nginx&lt;/code&gt; : Name of the docker container&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;-v $PWD/config/:/etc/nginx/:ro&lt;/code&gt; : Maps the &lt;code&gt;config&lt;/code&gt; subfolder to Nginx config folder inside the container&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;nginx:latest&lt;/code&gt; : Nginx image version to use from hub.docker.com&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;nginx -c /etc/nginx/nginx.conf -t&lt;/code&gt; : Nginx command to validate nginx.conf file&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;em&gt;There might be some adjustments required for it to run on Docker for Windows&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;This assumes you have the following folder structure:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;./&lt;/li&gt;
&lt;li&gt;./config&lt;/li&gt;
&lt;li&gt;./config/nginx.conf&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;With that, everyone could run this command in the parent folder of the config directory and see if their &lt;code&gt;nginx.conf&lt;/code&gt; file is valid or not.&lt;/p&gt;

&lt;h1&gt;
  
  
  Making it simpler with a bash script
&lt;/h1&gt;

&lt;p&gt;Though that was already a good start, we wanted to make it so that we get a SUCCESS or a FAILED message, whether the config file was valid or not.  We also wanted to be able to specify a more dynamic version for the Nginx docker image and mostly, didn't want to remember all this command.&lt;/p&gt;

&lt;p&gt;Here is what we ended up with in a file called &lt;code&gt;checkNginx.sh&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#!/bin/bash
rootPath=$1
nginxVersion=1.13  # Could also be passed as an argument using $2

result=$(docker run --rm -t -a stdout --name my-nginx -v ${rootPath}/config/:/etc/nginx/:ro nginx:$nginxVersion nginx -c /etc/nginx/nginx.conf -t)

# Look for the word successful and count the lines that have it
# This validation could be improved if needed
successful=$(echo $result | grep successful | wc -l)

if [ $successful = 0 ]; then
    echo FAILED
    echo "$result"
    exit 1
else
    echo SUCCESS
fi
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This script can be called using the following command: &lt;br&gt;
&lt;code&gt;bash checkNginx.sh $PWD&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;It outputs &lt;code&gt;SUCCESS&lt;/code&gt; if everything is ok or the following if there is an error:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;FAILED
2018/04/13 21:26:15 [emerg] 1#1: unexpected ";" in /etc/nginx/nginx.conf:155
nginx: [emerg] unexpected ";" in /etc/nginx/nginx.conf:155
nginx: configuration file /etc/nginx/nginx.conf test failed
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then we were able to run this same command from the build server, at the Test phase of the deployment process and abort the deployment of the configs completely when a Nginx config file had a typo or an error in it.&lt;/p&gt;

&lt;p&gt;This could also have been added to a &lt;em&gt;pre-commit&lt;/em&gt; or &lt;em&gt;pre-push&lt;/em&gt; Git hook for the repository keeping track of these configs, but so far we didn't feel the need to do so.&lt;/p&gt;

&lt;p&gt;Thanks for reading, hope you appreciated :)&lt;/p&gt;

</description>
      <category>docker</category>
      <category>nginx</category>
      <category>devops</category>
    </item>
    <item>
      <title>Hi, I'm Simon Drouin</title>
      <dc:creator>Simon Drouin</dc:creator>
      <pubDate>Sun, 28 May 2017 11:38:27 +0000</pubDate>
      <link>https://dev.to/simdrouin/hi-im-simon-drouin</link>
      <guid>https://dev.to/simdrouin/hi-im-simon-drouin</guid>
      <description>&lt;p&gt;I have been coding for 20 years, started around 14 years old with some old Basic and VB3. &lt;/p&gt;

&lt;p&gt;I live in Quebec City and work for Bookenda, a company that aims to connect businesses with their customers. &lt;/p&gt;

&lt;p&gt;I mostly program in these languages: NodeJS, AngularJS, C# and also work as a DevOp with Docker Swarm, AWS and related technologies. &lt;/p&gt;

&lt;p&gt;I keep learning new technologies and methodologies to improve myself as a software developer and architect. &lt;/p&gt;

&lt;p&gt;Nice to meet you.&lt;/p&gt;

</description>
      <category>introduction</category>
    </item>
  </channel>
</rss>
