Skip to content

Commit

Permalink
feat: improve structured log format configuring it in all server defi…
Browse files Browse the repository at this point in the history
…nition (#115)
  • Loading branch information
Monska85 committed May 23, 2024
1 parent f264f46 commit 52cb1b7
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 10 deletions.
18 changes: 9 additions & 9 deletions docker-entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ export PHP_PORT="${PHP_PORT:-9000}"
export NGINX_PHP_READ_TIMEOUT="${NGINX_PHP_READ_TIMEOUT:-900}"
export NGINX_CATCHALL_RETURN_CODE="${NGINX_CATCHALL_RETURN_CODE:-444}"

export NGINX_ACCESS_LOG_FORMAT="${NGINX_ACCESS_LOG_FORMAT:-main}"
# If the environment is not local, we enable structured logging.
if [ "${ENV:-}" != "loc" ]; then
export NGINX_ACCESS_LOG_FORMAT="structured"
fi

# If you use the rootless image the user directive is not needed
if [ "$(id -u)" -ne 0 ]; then
sed -i '/^user /d' /etc/nginx/nginx.conf
Expand Down Expand Up @@ -107,10 +113,9 @@ if [ "${NGINX_DEFAULT_SERVER_NAME}" = "_" ]; then
else
export DEFAULT_SERVER=""
# shellcheck disable=SC2016 # The envsubst command needs to be executed without variable expansion
envsubst '${PHP_HOST} ${PHP_PORT} ${NGINX_DEFAULT_SERVER_PORT} ${NGINX_DEFAULT_SERVER_NAME} ${NGINX_DEFAULT_ROOT} ${NGINX_SUBFOLDER} ${NGINX_SUBFOLDER_ESCAPED} ${NGINX_CATCHALL_RETURN_CODE} ${NGINX_HSTS_HEADER}' < /templates/catch-all-server.conf > /etc/nginx/conf.d/catch-all-server.conf
envsubst '${PHP_HOST} ${PHP_PORT} ${NGINX_ACCESS_LOG_FORMAT} ${NGINX_DEFAULT_SERVER_PORT} ${NGINX_DEFAULT_SERVER_NAME} ${NGINX_DEFAULT_ROOT} ${NGINX_SUBFOLDER} ${NGINX_SUBFOLDER_ESCAPED} ${NGINX_CATCHALL_RETURN_CODE} ${NGINX_HSTS_HEADER}' < /templates/catch-all-server.conf > /etc/nginx/conf.d/catch-all-server.conf
fi

export NGINX_ACCESS_LOG_FORMAT="${NGINX_ACCESS_LOG_FORMAT:-main}"
export NGINX_DEFAULT_ROOT="${NGINX_DEFAULT_ROOT:-/var/www/html}"
export NGINX_HTTPSREDIRECT="${NGINX_HTTPSREDIRECT:-0}"
export NGINX_SUBFOLDER="${NGINX_SUBFOLDER:-0}"
Expand Down Expand Up @@ -142,11 +147,6 @@ if [ "${NGINX_OSB_RESOLVER_ENFORCE_IPV6_OFF}" = "1" ] && ! echo "${NGINX_OSB_RES
export NGINX_OSB_RESOLVER="${NGINX_OSB_RESOLVER} ipv6=off"
fi

# If the environment is not local, we enable structured logging.
if [ "${ENV:-}" != "loc" ]; then
export NGINX_ACCESS_LOG_FORMAT="structured"
fi

# These lines of code have been added to provide a BC path for those
# environments that still have the NGINX_OSB_BUCKET variable filled with the
# old format that consist in having the value of NGINX_OSB_PUBLIC_PATH (usually
Expand Down Expand Up @@ -239,7 +239,7 @@ envsubst '${PHP_HOST} ${PHP_PORT} ${NGINX_ACCESS_LOG_FORMAT} ${NGINX_DEFAULT_SER

if [ "${NGINX_SUBFOLDER}" != 0 ]; then
# shellcheck disable=SC2016 # The envsubst command needs to be executed without variable expansion
envsubst '${PHP_HOST} ${PHP_PORT} ${NGINX_DEFAULT_SERVER_PORT} ${NGINX_DEFAULT_SERVER_NAME} ${NGINX_DEFAULT_ROOT} ${NGINX_SUBFOLDER} ${NGINX_SUBFOLDER_ESCAPED} ${NGINX_XFRAME_OPTION_VALUE} ${NGINX_HSTS_HEADER}' < /templates/subfolder.conf > /etc/nginx/conf.d/default.conf
envsubst '${PHP_HOST} ${PHP_PORT} ${NGINX_ACCESS_LOG_FORMAT} ${NGINX_DEFAULT_SERVER_PORT} ${NGINX_DEFAULT_SERVER_NAME} ${NGINX_DEFAULT_ROOT} ${NGINX_SUBFOLDER} ${NGINX_SUBFOLDER_ESCAPED} ${NGINX_XFRAME_OPTION_VALUE} ${NGINX_HSTS_HEADER}' < /templates/subfolder.conf > /etc/nginx/conf.d/default.conf
fi

# Handle robots.txt and sitemap directive
Expand Down Expand Up @@ -344,7 +344,7 @@ if [ "${NGINX_REDIRECT_FROM_TO_WWW}" -eq 1 ] && [ "${NGINX_DEFAULT_SERVER_NAME}"
print "/etc/nginx/conf.d/from-to-www.conf - Creating a redirect from ${DOMAIN_FROM} to ${DOMAIN_TO}"
# shellcheck disable=SC2016 # The envsubst command needs to be executed without variable expansion
DOMAIN_FROM=${DOMAIN_FROM} DOMAIN_TO=${DOMAIN_TO} \
envsubst '${DOMAIN_FROM} ${DOMAIN_TO} ${NGINX_DEFAULT_SERVER_PORT} ${DEFAULT_SERVER} ${NGINX_HSTS_HEADER}' < /templates/from-to-www.conf.tpl \
envsubst '${DOMAIN_FROM} ${DOMAIN_TO} ${NGINX_DEFAULT_SERVER_PORT} ${DEFAULT_SERVER} ${NGINX_ACCESS_LOG_FORMAT} ${NGINX_HSTS_HEADER}' < /templates/from-to-www.conf.tpl \
| tee -a /etc/nginx/conf.d/from-to-www.conf >/dev/null
else
print "/etc/nginx/conf.d/from-to-www.conf - Skipping redirect from ${DOMAIN_FROM} to ${DOMAIN_TO} because it already exists"
Expand Down
2 changes: 2 additions & 0 deletions templates/catch-all-server.conf
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ map $scheme://$host$request_uri $new_domain_uri {
}

server {
access_log /var/log/nginx/access.log ${NGINX_ACCESS_LOG_FORMAT};

listen ${NGINX_DEFAULT_SERVER_PORT} default_server;

# The _ it is just one of a myriad of invalid domain
Expand Down
4 changes: 4 additions & 0 deletions templates/from-to-www.conf.tpl
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
server {
access_log /var/log/nginx/access.log ${NGINX_ACCESS_LOG_FORMAT};

server_name ${DOMAIN_FROM};
listen ${NGINX_DEFAULT_SERVER_PORT} ${DEFAULT_SERVER};

#hstsheader
#httpsredirect

set $request_proto $scheme;
if ($http_x_forwarded_proto = "https") {
set $request_proto "https";
Expand Down
3 changes: 2 additions & 1 deletion templates/subfolder.conf
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ map $http_x_forwarded_proto $fastcgi_https {
}

server {
access_log /var/log/nginx/access.log ${NGINX_ACCESS_LOG_FORMAT};

server_name ${NGINX_DEFAULT_SERVER_NAME};
listen ${NGINX_DEFAULT_SERVER_PORT};

Expand All @@ -22,7 +24,6 @@ server {

include /etc/nginx/conf.d/fragments/*.conf;


# Just return for google gclb healthcheck.
if ($http_user_agent = 'GoogleHC/1.0') {
return 200;
Expand Down

0 comments on commit 52cb1b7

Please sign in to comment.