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

Conversation

robwatkiss
Copy link

This pull request adds an optional healthcheck endpoint, useful for environments such as Kubernetes where readiness needs to be probed and monitored.

Four new environment variables are introduced:

  • 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

If SERVER_HEALTHCHECK_ENABLED=1 then /etc/nginx/includes/healthcheck.conf is included into default.conf.

In order for location matching against /${SERVER_HEALTHCHECK_PATH} it was necessary to wrap existing redirects in

location / {
    ...
    # Existing redirect directives
    ...
}

Finally this pull request tidies up run.sh by declaring default ENV values at build time in the Dockerfile.

Comment on lines +3 to +11
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'
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.

Comment on lines -10 to +21
# 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};
}
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 / {
}

Comment on lines +29 to +30
# optionally enable the healthcheck endpoint
#- SERVER_HEALTHCHECK_ENABLED=1
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.

Comment on lines -23 to -42
# 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

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

@schmunk42 schmunk42 requested a review from handcode August 24, 2020 09:08
@schmunk42
Copy link
Owner

@robwatkiss Is this backward-compatible?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants