Skip to content
Merged
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
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,9 @@ Variable | Example | Description
`SERVER_MAX_BODY_SIZE` | `SERVER_MAX_BODY_SIZE=4M` | Allows the downstream application to specify a non-default `client_max_body_size` configuration for the `server`-level directive in `/etc/nginx/sites-available/default`
`SERVER_INDEX` | `SERVER_INDEX index.html index.html index.php` | Changes the default pages to hit for folder and web roots
`SERVER_APP_NAME` | `APP_NAME='view'` | Sets a kv pair to be consumed by logging service for easy parsing and searching


### Runtime Commands

To inject things into the runtime process, add shell scripts (ending in .sh) into the
`/run.d` folder. These will be executed during container start.
27 changes: 27 additions & 0 deletions container/root/run.d/10-nginx.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/bin/bash
CONFIG_SITE=/etc/nginx/sites-available/default
CONFIG_SERVER=/etc/nginx/nginx.conf

echo '[nginx] setting sensible defaults'

# Configure nginx to use as many workers as there are cores for the running container
sed -i "s/worker_processes [0-9]\+/worker_processes $(nproc)/" $CONFIG_SERVER
sed -i "s/worker_connections [0-9]\+/worker_connections 1024/" $CONFIG_SERVER

echo '[nginx] piping logs to STDOUT'
# Ensure nginx is configured to write logs to STDOUT
# Also set log_format to output SERVER_APP_NAME placeholder
sed -i "s/access_log [a-z\/\.\;]\+/ log_format main \'\$remote_addr - \$remote_user [\$time_local] \"\$request\" \$status \$bytes_sent \"\$http_referer\" \"\$http_user_agent\" ${SERVER_APP_NAME}\';\n access_log \/dev\/stdout main;\n/" $CONFIG_SERVER
sed -i "s/error_log [a-z\/\.\ \;]\+/error_log \/dev\/stdout info;/" $CONFIG_SERVER

if [[ $SERVER_MAX_BODY_SIZE ]]
then
echo "[nginx] server client max body is ${SERVER_MAX_BODY_SIZE}"
sed -i "s/client_max_body_size 1m/client_max_body_size ${SERVER_MAX_BODY_SIZE}/" $CONFIG_SITE
fi

if [[ $SERVER_INDEX ]]
then
echo "[nginx] server index is ${SERVER_INDEX}"
sed -i "s/index index.html index.htm/index ${SERVER_INDEX}/" $CONFIG_SITE
fi
6 changes: 6 additions & 0 deletions container/root/run.d/README
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
### Runtime commands
---

Allows children to inject things into the run process.

Drop bash scripts in here to be executed by entrypoint during startup.
31 changes: 6 additions & 25 deletions container/root/run.sh
Original file line number Diff line number Diff line change
@@ -1,30 +1,11 @@
#!/bin/bash
CONFIG_SITE=/etc/nginx/sites-available/default
CONFIG_SERVER=/etc/nginx/nginx.conf
RUN_SCRIPTS=/run.d

Choose a reason for hiding this comment

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

🎉 Nice!


echo '[nginx] setting sensible defaults'

# Configure nginx to use as many workers as there are cores for the running container
sed -i "s/worker_processes [0-9]\+/worker_processes $(nproc)/" $CONFIG_SERVER
sed -i "s/worker_connections [0-9]\+/worker_connections 1024/" $CONFIG_SERVER

echo '[nginx] piping logs to STDOUT'
# Ensure nginx is configured to write logs to STDOUT
# Also set log_format to output SERVER_APP_NAME placeholder
sed -i "s/access_log [a-z\/\.\;]\+/ log_format main \'\$remote_addr - \$remote_user [\$time_local] \"\$request\" \$status \$bytes_sent \"\$http_referer\" \"\$http_user_agent\" ${SERVER_APP_NAME}\';\n access_log \/dev\/stdout main;\n/" $CONFIG_SERVER
sed -i "s/error_log [a-z\/\.\ \;]\+/error_log \/dev\/stdout info;/" $CONFIG_SERVER

if [[ $SERVER_MAX_BODY_SIZE ]]
then
echo "[nginx] server client max body is ${SERVER_MAX_BODY_SIZE}"
sed -i "s/client_max_body_size 1m/client_max_body_size ${SERVER_MAX_BODY_SIZE}/" $CONFIG_SITE
fi

if [[ $SERVER_INDEX ]]
then
echo "[nginx] server index is ${SERVER_INDEX}"
sed -i "s/index index.html index.htm/index ${SERVER_INDEX}/" $CONFIG_SITE
fi
# Run shell scripts (ending in .sh) in run.d directory, if any
for file in $RUN_SCRIPTS/*.sh; do
echo "[run.d] executing ${file}"
/bin/bash $file
done

echo "[nginx] starting (foreground)"
exec /usr/sbin/nginx -g "daemon off;"