-
Notifications
You must be signed in to change notification settings - Fork 96
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
base: master
Are you sure you want to change the base?
Conversation
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' |
There was a problem hiding this comment.
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
.
# 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}; | ||
} |
There was a problem hiding this comment.
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 / {
}
# optionally enable the healthcheck endpoint | ||
#- SERVER_HEALTHCHECK_ENABLED=1 |
There was a problem hiding this comment.
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.
# 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 | ||
|
There was a problem hiding this comment.
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
@robwatkiss Is this backward-compatible? |
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 settingSERVER_HEALTHCHECK_ENABLED=1
SERVER_HEALTHCHECK_PATH
- optionally override the location of the healthcheck endpoint/healthcheck
SERVER_HEALTHCHECK_RESPONSE_CODE
- optionally override the status code of the healthcheck endpoint response200
SERVER_HEALTHCHECK_RESPONSE_BODY
- optionally override the body of the healthcheck endpoint responsealive
If
SERVER_HEALTHCHECK_ENABLED=1
then/etc/nginx/includes/healthcheck.conf
is included intodefault.conf
.In order for location matching against
/${SERVER_HEALTHCHECK_PATH}
it was necessary to wrap existing redirects inFinally this pull request tidies up
run.sh
by declaring default ENV values at build time in the Dockerfile.