Skip to content

Instantly share code, notes, and snippets.

@OleksandrKucherenko
Created August 30, 2022 09:59
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save OleksandrKucherenko/33ebf2f85a00b96e3dcbdfb3af7042a6 to your computer and use it in GitHub Desktop.
MACOSX Script to Verify NGINX server configuration on local computer before deployment. Lines 62-63 expects special location of the NGINX configuration files.
#!/usr/bin/env bash
# shellcheck disable=SC2086
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
# include other scripts
# shellcheck disable=SC1090 source=dependencies.sh
source "$SCRIPT_DIR/dependencies.sh"
# shellcheck disable=SC1090 source=commons.sh
source "$SCRIPT_DIR/commons.sh"
dependency docker "20.*.*" "brew install docker"
#NGINX_IMAGE="nginx:latest"
NGINX_IMAGE="nginx:alpine"
docker_status=$(docker ps -q)$?
if [[ $docker_status -ne 0 ]]; then
open --background -a Docker &&
while ! docker system info >/dev/null 2>&1; do sleep 1; done &&
echo "Docker is ready for running the commands"
fi
# refs:
# - https://docs.rackspace.com/support/how-to/basic-nginx-troubleshooting/
function commands() {
cat <<EOF
nginx -v \
&& echo "${cl_green}Prepare NGINX folders${cl_reset}" \
&& touch /usr/share/nginx/html/index.html \
&& mkdir -p /usr/share/nginx/html/management \
&& echo "{healthy}" >/usr/share/nginx/html/management/health \
&& echo "{alive}" >/usr/share/nginx/html/management/liveness \
&& (nginx -g 'daemon off;' 2>/dev/null >&2 &) \
&& echo "${cl_yellow}/etc/nginx/conf.d/default.conf${cl_reset}" \
&& cat -n /etc/nginx/conf.d/default.conf \
&& echo "${cl_yellow}/etc/nginx/nginx.conf${cl_reset}" \
&& cat -n "/etc/nginx/nginx.conf" \
&& nginx -c "/etc/nginx/nginx.conf" -t \
&& echo "${cl_green}Verify rewrite rules${cl_reset}" \
&& (wget -q --spider -O - http://127.0.0.1:8080/hvc-csp || echo "error") \
&& echo "${cl_yellow}http://127.0.0.1:8080/hvc-csp${cl_reset}" \
&& (tail -n 3 "/var/log/nginx/server-error.log" | cat -n) \
&& (wget -q --spider -O - http://localhost:8080/hvc-csp/?1=1 || echo "error") \
&& echo "${cl_yellow}http://localhost:8080/hvc-csp/?1=1${cl_reset}" \
&& (tail -n 3 "/var/log/nginx/server-error.log" | cat -n) \
&& (wget -q --spider -O - http://localhost:8080/hvc-csp/login?user=test || echo "error") \
&& echo "${cl_yellow}http://localhost:8080/hvc-csp/login?user=test${cl_reset}" \
&& (tail -n 3 "/var/log/nginx/server-error.log" | cat -n) \
&& echo "${cl_green}Get server health status${cl_reset}" \
&& set -x \
&& (wget -q -S -O - http://localhost:8080/stub_status 2>&1 || echo "error") \
&& (wget -q -S -O - http://localhost:8080/management/health 2>&1 || echo "error") \
&& (wget -q -S -O - http://localhost:8080/management/liveness 2>&1 || echo "error")
EOF
}
result=$(docker run --rm \
--tty --attach stdout \
--name my-nginx \
--volume "${SCRIPT_DIR}/../config/nginx/nginx.conf:/etc/nginx/nginx.conf:ro" \
--volume "${SCRIPT_DIR}/../config/nginx/default.conf:/etc/nginx/conf.d/default.conf:ro" \
$NGINX_IMAGE \
/bin/sh -c "$(commands)" 2>&1)
# Look for the word successful and count the lines that have it
# This validation could be improved if needed
successful=$(echo "$result" | grep -c 'test is successful')
# display results
echo
echo "$result"
echo
# confirm status of execution
if [ $successful = 0 ]; then
echo "Validation [${cl_red}NO${cl_reset}]: FAILED!"
exit 1
else
echo "Validation [${cl_green}OK${cl_reset}]: PASSED!"
fi
PUBLIC_URL=$(grep PUBLIC_URL .env | awk -F '=' '{print $2}')
echo "Validate sub-folder path: ${cl_yellow}${PUBLIC_URL}${cl_reset}"
SUB_PATH_CHECK=$(ggrep -q "rewrite ^\\${PUBLIC_URL}.*last;" ./config/nginx/default.conf)$?
if [[ $SUB_PATH_CHECK -ne 0 ]]; then
echo "Validation [${cl_red}NO${cl_reset}]: FAILED! expected rewrite rule in ${cl_yellow}./config/nginx/default.conf${cl_reset}"
echo " | ${cl_grey}rewrite ^${PUBLIC_URL}(/?)(.*)$ /\$2 last;${cl_reset}"
exit 2
else
echo "Validation [${cl_green}OK${cl_reset}]: PASSED!"
fi
#
# Refs:
# - https://dev.to/simdrouin/validate-your-nginx-configuration-files-easily-with-docker-4ihi
#
@OleksandrKucherenko
Copy link
Author

What script do:

  • verify that docker installed on local computer
  • verify that docker is up and running on local computer, otherwise starts it
  • execute NGINX docker image "nginx:alpine" and use it for executing a set of commands
  • each command verifying a specific use case like "URL rewriting rule", "syntax of the NGINX configuration", "availability of specific files on NGINX server" (health checks)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment