Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add healthcheck endpoint, set default ENV in Dockerfile #16

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -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'
Comment on lines +3 to +11
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Define default environment variables at build time instead of checking and overriding in run.sh.


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

Expand Down
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
19 changes: 11 additions & 8 deletions default.conf
Original file line number Diff line number Diff line change
Expand Up @@ -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};
}
Comment on lines -10 to +21
Copy link
Author

@robwatkiss robwatkiss Jul 28, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Existing directives wrapped to allow healthcheck location to be added

location / {
}


# redirect server error pages to the static page /50x.html
#
Expand All @@ -25,4 +27,5 @@ server {
root /usr/share/nginx/html;
}

${HEALTHCHECK_LOCATION_BLOCK}
}
2 changes: 2 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Comment on lines +29 to +30
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I haven't added all new environment variables to this file to avoid cluttering it.

5 changes: 5 additions & 0 deletions healthcheck.conf
Original file line number Diff line number Diff line change
@@ -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;
}
36 changes: 11 additions & 25 deletions run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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

Comment on lines -23 to -42
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No longer required as defaults are set at buildtime then overridden at runtime

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
Expand All @@ -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

Expand Down