Skip to content

Commit

Permalink
Merge pull request #1979 from bomoko/feature/add_healthcheck_endpoint…
Browse files Browse the repository at this point in the history
…_to_nginx_php

Adds healthz-php to php fpm
  • Loading branch information
Schnitzel authored Jul 10, 2020
2 parents 2c4a274 + c738e38 commit 112a7e4
Show file tree
Hide file tree
Showing 9 changed files with 66 additions and 1 deletion.
1 change: 1 addition & 0 deletions docs/using_lagoon/docker_images/nginx.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,4 @@ Environment variables are meant to contain common information for the `Nginx` co
| `BASIC_AUTH` | `restricted` | By not setting `BASIC_AUTH` this will instruct Lagoon to automatically enable basic authentication if `BASIC_AUTH_USERNAME` and `BASIC_AUTH_PASSWORD` are set. To disable basic authentication even if `BASIC_AUTH_USERNAME` and `BASIC_AUTH_PASSWORD` are set, set `BASIC_AUTH` to `off`. |
| `BASIC_AUTH_USERNAME` | \(not set\) | Username for basic authentication |
| `BASIC_AUTH_PASSWORD` | \(not set\) | Password for basic authentication \(unencrypted\) |
| `FAST_HEALTH_CHECK` | \(not set\) | If set to `true` this will redirect GET requests from certain user agents (StatusCake, Pingdom, Site25x7, Uptime, nagios) to the lightweight Lagoon service healthcheck. |
1 change: 1 addition & 0 deletions images/nginx/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ COPY fastcgi.conf /etc/nginx/fastcgi_params
COPY helpers/ /etc/nginx/helpers/
COPY static-files.conf /etc/nginx/conf.d/app.conf
COPY redirects-map.conf /etc/nginx/redirects-map.conf
COPY healthcheck/healthz.locations healthcheck/healthz.locations.php.disable /etc/nginx/conf.d/

RUN mkdir -p /app \
&& rm -f /etc/nginx/conf.d/default.conf \
Expand Down
14 changes: 13 additions & 1 deletion images/nginx/docker-entrypoint
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,20 @@ ep /etc/nginx/*
# Find all folders within /etc/nginx/conf.d/
find /etc/nginx/conf.d/ -type d | while read DIR; do
# envplate if found folder is not empty
if find $DIR -mindepth 1 | read; then
if find $DIR -mindepth 1 | read; then
ep $DIR/*;
fi
done
ep /etc/nginx/helpers/*

# If PHP is enabled, we override the Luascript /healthz check
echo "Setting up Healthz routing"
if [ ! -z "$NGINX_FASTCGI_PASS" ]; then
echo "Healthz routing - using PHP"
cp /etc/nginx/conf.d/healthz.locations.php.disable /etc/nginx/conf.d/healthz.locations
fi

if [ "$FAST_HEALTH_CHECK" == "true" ]; then
echo "FAST HEALTH CHECK ENABLED"
cp /etc/nginx/helpers/90_healthz_fast_check.conf.disabled /etc/nginx/helpers/90_health_fast_check.conf
fi
10 changes: 10 additions & 0 deletions images/nginx/healthcheck/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Healthcheck

In this directory you'll find two files

- healthz.locations.php.disable
- healthz.locations

Both are designed to expose a `/.lagoonhealthz` location from the nginx service. The difference being that the `.php.disable` file is used to point to the [healthz-php](https://github.com/amazeeio/healthz-php) application _if_ there is a PHP service attached to this application.

The logic for which of the two files are enabled are contained in this image's `docker-entrypoint` file - there we check for the existence of the env var `NGINX_FASTCGI_PASS`, which indicates (or should indicate) the presence of a PHP-fpm service.
8 changes: 8 additions & 0 deletions images/nginx/healthcheck/healthz.locations
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
location /.lagoonhealthz {
content_by_lua_block {
ngx.status = ngx.HTTP_OK;
ngx.header.content_type = 'application/json';
ngx.say('{"check_nginx":"pass"}');
ngx.exit(ngx.OK);
}
}
10 changes: 10 additions & 0 deletions images/nginx/healthcheck/healthz.locations.php.disable
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
location /.lagoonhealthz {
rewrite ^/.lagoonhealthz(/.*)?$ /.lagoonhealthz/index.php;

location ~* \.php(/|$) {
include /etc/nginx/fastcgi.conf;
fastcgi_param SCRIPT_NAME /index.php;
fastcgi_param SCRIPT_FILENAME /healthz-php/index.php;
fastcgi_pass ${NGINX_FASTCGI_PASS:-php}:9000;
}
}
1 change: 1 addition & 0 deletions images/nginx/helpers/90_healthz.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
include /etc/nginx/conf.d/healthz.locations;
13 changes: 13 additions & 0 deletions images/nginx/helpers/90_healthz_fast_check.conf.disabled
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
set $fhcc none;

if ( $http_user_agent ~* "StatusCake|Pingdom|Site25x7|Uptime|nagios" ) {
set $fhcc "A";
}

if ( $request_method = 'GET' ) {
set $fhcc "$fhcc G";
}

if ( $fhcc = 'A G' ) {
rewrite ~* /.lagoonhealthz last;
}
9 changes: 9 additions & 0 deletions images/php/fpm/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ ARG PHP_IMAGE_VERSION
ARG ALPINE_VERSION
ARG IMAGE_REPO
FROM ${IMAGE_REPO:-lagoon}/commons as commons

FROM composer:latest as healthcheckbuilder

RUN composer create-project --no-dev amazeeio/healthz-php /healthz-php v0.0.3

FROM php:${PHP_IMAGE_VERSION}-fpm-alpine${ALPINE_VERSION}

LABEL maintainer="amazee.io"
Expand All @@ -17,6 +22,10 @@ COPY --from=commons /bin/fix-permissions /bin/ep /bin/docker-sleep /bin/
COPY --from=commons /sbin/tini /sbin/
COPY --from=commons /home /home

# Copy healthcheck files

COPY --from=healthcheckbuilder /healthz-php /healthz-php

RUN chmod g+w /etc/passwd \
&& mkdir -p /home

Expand Down

0 comments on commit 112a7e4

Please sign in to comment.