From a65859e703a9db7cca47aa582d7a82a7d63c93c5 Mon Sep 17 00:00:00 2001 From: Rob Watkiss Date: Tue, 28 Jul 2020 17:11:06 +0100 Subject: [PATCH 1/2] Add optional healthcheck endpoint and define default environment variables in Dockerfile --- Dockerfile | 11 +++++++++++ default.conf | 19 +++++++++++-------- healthcheck.conf | 5 +++++ run.sh | 36 +++++++++++------------------------- 4 files changed, 38 insertions(+), 33 deletions(-) create mode 100644 healthcheck.conf diff --git a/Dockerfile b/Dockerfile index 8f9c64e..0bfe2c7 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,7 +1,18 @@ FROM nginx:alpine +ENV SERVER_NAME='localhost' \ + SERVER_REDIRECT_PATH='$request_uri' \ + SERVER_REDIRECT_SCHEME='$redirect_scheme' \ + SERVER_ACCESS_LOG='/dev/stdout' \ + SERVER_ERROR_LOG='/dev/stderr' \ + SERVER_HEALTHCHECK_ENABLED=0 \ + SERVER_HEALTHCHECK_RESPONSE_CODE=200 \ + SERVER_HEALTHCHECK_RESPONSE_BODY='alive' \ + SERVER_HEALTHCHECK_PATH='healthcheck' + ADD run.sh /run.sh ADD default.conf /etc/nginx/conf.d/default.conf +ADD healthcheck.conf /etc/nginx/includes/healthcheck.conf RUN chmod +x /run.sh diff --git a/default.conf b/default.conf index 3e15cbb..32f7b4e 100644 --- a/default.conf +++ b/default.conf @@ -7,16 +7,18 @@ server { listen 80; server_name ${SERVER_NAME}; - # cherry picked from https://github.com/schmunk42/docker-nginx-redirect/pull/8 - if ($request_method = POST) { - return ${SERVER_REDIRECT_POST_CODE} ${SERVER_REDIRECT_SCHEME}://${SERVER_REDIRECT}${SERVER_REDIRECT_PATH}; - } + location / { + # cherry picked from https://github.com/schmunk42/docker-nginx-redirect/pull/8 + if ($request_method = POST) { + return ${SERVER_REDIRECT_POST_CODE} ${SERVER_REDIRECT_SCHEME}://${SERVER_REDIRECT}${SERVER_REDIRECT_PATH}; + } - if ($request_method ~ PUT|PATCH|DELETE) { - return ${SERVER_REDIRECT_PUT_PATCH_DELETE_CODE} ${SERVER_REDIRECT_SCHEME}://${SERVER_REDIRECT}${SERVER_REDIRECT_PATH}; - } + if ($request_method ~ PUT|PATCH|DELETE) { + return ${SERVER_REDIRECT_PUT_PATCH_DELETE_CODE} ${SERVER_REDIRECT_SCHEME}://${SERVER_REDIRECT}${SERVER_REDIRECT_PATH}; + } - return ${SERVER_REDIRECT_CODE} ${SERVER_REDIRECT_SCHEME}://${SERVER_REDIRECT}${SERVER_REDIRECT_PATH}; + return ${SERVER_REDIRECT_CODE} ${SERVER_REDIRECT_SCHEME}://${SERVER_REDIRECT}${SERVER_REDIRECT_PATH}; + } # redirect server error pages to the static page /50x.html # @@ -25,4 +27,5 @@ server { root /usr/share/nginx/html; } + ${HEALTHCHECK_LOCATION_BLOCK} } diff --git a/healthcheck.conf b/healthcheck.conf new file mode 100644 index 0000000..78fba9f --- /dev/null +++ b/healthcheck.conf @@ -0,0 +1,5 @@ +# return status code 200 on healthcheck route +location = /${SERVER_HEALTHCHECK_PATH} { + return ${SERVER_HEALTHCHECK_RESPONSE_CODE} ${SERVER_HEALTHCHECK_RESPONSE_BODY}; + add_header Content-Type text/plain; +} \ No newline at end of file diff --git a/run.sh b/run.sh index ada7209..5610e25 100644 --- a/run.sh +++ b/run.sh @@ -5,11 +5,6 @@ if [ ! -n "$SERVER_REDIRECT" ] ; then exit 1 fi -# set server name from optional ENV var -if [ ! -n "$SERVER_NAME" ] ; then - SERVER_NAME='localhost' -fi - # set redirect code from optional ENV var # allowed Status Codes are: 301, 302, 303, 307, 308 expr match "$SERVER_REDIRECT_CODE" '30[12378]$' > /dev/null || SERVER_REDIRECT_CODE='301' @@ -20,26 +15,6 @@ expr match "$SERVER_REDIRECT_POST_CODE" '30[12378]$' > /dev/null || SERVER_REDIR # set redirect code from optional ENV var for PUT, PATCH and DELETE requests expr match "$SERVER_REDIRECT_PUT_PATCH_DELETE_CODE" '30[12378]$' > /dev/null || SERVER_REDIRECT_PUT_PATCH_DELETE_CODE=$SERVER_REDIRECT_CODE -# set redirect path from optional ENV var -if [ ! -n "$SERVER_REDIRECT_PATH" ] ; then - SERVER_REDIRECT_PATH='$request_uri' -fi - -# set redirect scheme from optional ENV var -if [ ! -n "$SERVER_REDIRECT_SCHEME" ] ; then - SERVER_REDIRECT_SCHEME='$redirect_scheme' -fi - -# set access log location from optional ENV var -if [ ! -n "$SERVER_ACCESS_LOG" ] ; then - SERVER_ACCESS_LOG='/dev/stdout' -fi - -# set error log location from optional ENV var -if [ ! -n "$SERVER_ERROR_LOG" ] ; then - SERVER_ERROR_LOG='/dev/stderr' -fi - sed -i "s|\${SERVER_REDIRECT}|${SERVER_REDIRECT}|" /etc/nginx/conf.d/default.conf sed -i "s|\${SERVER_NAME}|${SERVER_NAME}|" /etc/nginx/conf.d/default.conf sed -i "s|\${SERVER_REDIRECT_CODE}|${SERVER_REDIRECT_CODE}|" /etc/nginx/conf.d/default.conf @@ -48,6 +23,17 @@ sed -i "s|\${SERVER_REDIRECT_PUT_PATCH_DELETE_CODE}|${SERVER_REDIRECT_PUT_PATCH_ sed -i "s|\${SERVER_REDIRECT_PATH}|${SERVER_REDIRECT_PATH}|" /etc/nginx/conf.d/default.conf sed -i "s|\${SERVER_REDIRECT_SCHEME}|${SERVER_REDIRECT_SCHEME}|" /etc/nginx/conf.d/default.conf +# optionally add healthcheck endpoint +if [ $SERVER_HEALTHCHECK_ENABLED = 1 ]; then + sed -i "s|\${HEALTHCHECK_LOCATION_BLOCK}|include includes/healthcheck.conf;|" /etc/nginx/conf.d/default.conf + + sed -i "s|\${SERVER_HEALTHCHECK_PATH}|${SERVER_HEALTHCHECK_PATH}|" /etc/nginx/includes/healthcheck.conf + sed -i "s|\${SERVER_HEALTHCHECK_RESPONSE_CODE}|${SERVER_HEALTHCHECK_RESPONSE_CODE}|" /etc/nginx/includes/healthcheck.conf + sed -i "s|\${SERVER_HEALTHCHECK_RESPONSE_BODY}|${SERVER_HEALTHCHECK_RESPONSE_BODY}|" /etc/nginx/includes/healthcheck.conf +else + sed -i "s|\${HEALTHCHECK_LOCATION_BLOCK}||" /etc/nginx/conf.d/default.conf +fi + ln -sfT "$SERVER_ACCESS_LOG" /var/log/nginx/access.log ln -sfT "$SERVER_ERROR_LOG" /var/log/nginx/error.log From ac26fa20368a455172b0541f98339e5326a4fc0d Mon Sep 17 00:00:00 2001 From: Rob Watkiss Date: Tue, 28 Jul 2020 17:20:55 +0100 Subject: [PATCH 2/2] Add healthcheck environment variable definitions to README.md --- README.md | 9 +++++++++ docker-compose.yml | 2 ++ 2 files changed, 11 insertions(+) diff --git a/README.md b/README.md index 0bbed12..a925126 100644 --- a/README.md +++ b/README.md @@ -33,6 +33,15 @@ A very simple container to redirect HTTP traffic to another server, based on `ng - if not set /dev/stdout is used - `SERVER_ERROR_LOG` - optionally define the location where nginx will write its error log - if not set /dev/stderr is used +- `SERVER_HEALTHCHECK_ENABLED` - optionally enable a static healthcheck endpoint by setting `SERVER_HEALTHCHECK_ENABLED=1` + - useful for deploying to Kubernetes or similar infrastructure where readiness needs to be confirmed and monitored + - disabled by default +- `SERVER_HEALTHCHECK_PATH` - optionally override the location of the healthcheck endpoint + - defaults to `/healthcheck` +- `SERVER_HEALTHCHECK_RESPONSE_CODE` - optionally override the status code of the healthcheck endpoint response + - defaults to `200` +- `SERVER_HEALTHCHECK_RESPONSE_BODY` - optionally override the body of the healthcheck endpoint response + - defaults to `alive` See also `docker-compose.yml` file. diff --git a/docker-compose.yml b/docker-compose.yml index 462f290..4262a78 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -26,3 +26,5 @@ to: # optionally define the location for the nginx error log # if not set /dev/stderr is used #- SERVER_ERROR_LOG=/dev/null + # optionally enable the healthcheck endpoint + #- SERVER_HEALTHCHECK_ENABLED=1 \ No newline at end of file