Skip to content

Commit

Permalink
Refactor default server. Add default server template. Add default ser…
Browse files Browse the repository at this point in the history
…ver to every template
  • Loading branch information
MatthewPattell committed Jul 31, 2019
1 parent 996e682 commit c632f98
Show file tree
Hide file tree
Showing 12 changed files with 41 additions and 25 deletions.
3 changes: 3 additions & 0 deletions docker/.env-default
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ SERVER_REPOSITORY=matthewpatell/universal-docker-server:3.9-dev
#DOMAIN_COVERAGE=coverage.sampledomain.com
#DOMAIN_OPCACHE=opcache.sampledomain.com
#DOMAIN_COMMON=sampledomain.com
#DOMAIN_DEFAULT=

# SSL DOMAINS
#SSL_DOMAINS[0]="[email protected] :api.sampledomain.com coverage.sampledomain.com opcache.sampledomain.com sampledomain.com:"
Expand All @@ -77,6 +78,8 @@ GIT_AUTHTOKEN=

# HOST NGINX SUPPORT (for create proxies nginx configs on host)
HOST_NGINX_PROXIES=no
# enable default nginx host
NGINX_DEFAULT_HOST=yes
# Set yes for keep created conf file
HOST_NGINX_KEEP_CONF=no
HOST_NGINX_TEMPLATE_PATH=${VENDOR_DIR}/conf/nginx-proxy.conf
Expand Down
16 changes: 0 additions & 16 deletions docker/nginx/nginx.conf
Original file line number Diff line number Diff line change
Expand Up @@ -182,22 +182,6 @@ http {
resolver 8.8.8.8 8.8.4.4 valid=300s;
resolver_timeout 5s;

# Default host
server {
listen 80 default_server;
#listen 443; need ssl certificates

location = /health-check {
add_header Content-Type text/plain;

return 200;
}

location / {
return 404;
}
}

# Hosts
include /etc/nginx/conf-dynamic.d/*.conf;
include /etc/nginx/conf.d/*.conf;
Expand Down
3 changes: 2 additions & 1 deletion docker/nginx/templates/admin.conf
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ server {
charset utf-8;
client_max_body_size 128M;

listen 80;
listen 80$DEFAULT;

server_name $COMMON_DOMAIN;

root $ROOT_PATH;
Expand Down
2 changes: 1 addition & 1 deletion docker/nginx/templates/api-nolimit.conf
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ server {
charset utf-8;
client_max_body_size 50M;

listen 80;
listen 80$DEFAULT;

server_name $COMMON_DOMAIN;

Expand Down
2 changes: 1 addition & 1 deletion docker/nginx/templates/api.conf
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ server {
charset utf-8;
client_max_body_size 50M;

listen 80;
listen 80$DEFAULT;

server_name $COMMON_DOMAIN;

Expand Down
2 changes: 1 addition & 1 deletion docker/nginx/templates/coverage.conf
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ server {
charset utf-8;
client_max_body_size 50M;

listen 80;
listen 80$DEFAULT;

server_name $COMMON_DOMAIN;

Expand Down
15 changes: 15 additions & 0 deletions docker/nginx/templates/default.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Default host
server {
listen 80 default_server;
#listen 443; need ssl certificates

location = /health-check {
add_header Content-Type text/plain;

return 200;
}

location / {
return 404;
}
}
2 changes: 1 addition & 1 deletion docker/nginx/templates/domain-nolimit.conf
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ server {
charset utf-8;
client_max_body_size 128M;

listen 80;
listen 80$DEFAULT;

server_name $COMMON_DOMAIN;

Expand Down
2 changes: 1 addition & 1 deletion docker/nginx/templates/domain.conf
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ server {
charset utf-8;
client_max_body_size 128M;

listen 80;
listen 80$DEFAULT;

server_name $COMMON_DOMAIN;

Expand Down
2 changes: 1 addition & 1 deletion docker/nginx/templates/opcache.conf
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ server {
charset utf-8;
client_max_body_size 50M;

listen 80;
listen 80$DEFAULT;

server_name $COMMON_DOMAIN;

Expand Down
16 changes: 14 additions & 2 deletions docker/scripts/init-nginx.sh
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,16 @@ echo "${NGINX_TEMPLATE_CODE}" > /etc/nginx/nginx.conf
# delete previous dynamic configs
find /etc/nginx/conf-dynamic.d/ -name "*.conf" -type f -delete

# add default host
if [ "$NGINX_DEFAULT_HOST" = "yes" ]; then
cp /etc/nginx/templates/default.conf /etc/nginx/conf-dynamic.d/default.conf
fi

# create dynamic nginx configs
for TEMPLATE_NAME in ${PACKAGE_DOCKER_FOLDER_CONTAINER}/nginx/conf-dynamic.d/*.conf; do
TARGET_CONFIG_PATH=/etc/nginx/conf-dynamic.d/$(basename ${TEMPLATE_NAME})
# Remove old file
[[ -f file ]] && rm ${TARGET_CONFIG_PATH}
[[ -f file ]] && rm "${TARGET_CONFIG_PATH}"

# get template content
BASE_TEMPLATE_CODE=$(cat ${TEMPLATE_NAME})
Expand Down Expand Up @@ -82,6 +87,12 @@ for TEMPLATE_NAME in ${PACKAGE_DOCKER_FOLDER_CONTAINER}/nginx/conf-dynamic.d/*.c
DOMAIN_1LVL=$(echo "${DOMAIN}" | sed -n "s/\([^\.]*\)\.\([^\.]*\)/\2/p")
DOMAIN_2LVL=$(echo "${DOMAIN}" | sed -n "s/\([^\.]*\)\.\([^\.]*\)/\1/p")

if [ "$DOMAIN_DEFAULT" = "$DOMAIN" ]; then
DEFAULT_HOST="default_server"
else
DEFAULT_HOST=""
fi

TEMPLATE_CODE="${BASE_TEMPLATE_CODE}"
TEMPLATE_CODE="${TEMPLATE_CODE//\$COMMON_DOMAIN/$DOMAIN}"
TEMPLATE_CODE="${TEMPLATE_CODE//\$ENVIRONMENT/$ENVIRONMENT}"
Expand All @@ -90,6 +101,7 @@ for TEMPLATE_NAME in ${PACKAGE_DOCKER_FOLDER_CONTAINER}/nginx/conf-dynamic.d/*.c
TEMPLATE_CODE="${TEMPLATE_CODE//\$PARSED_DOMAINS/$ONLY_DOMAINS}"
TEMPLATE_CODE="${TEMPLATE_CODE//\$ROOT_PATH/$REPLACE_ROOT}"
TEMPLATE_CODE="${TEMPLATE_CODE//\$CUSTOM_SNIPPETS/$REPLACE_CUSTOM_SNIPPETS}"
TEMPLATE_CODE="${TEMPLATE_CODE//\$DEFAULT/$DEFAULT_HOST}"

for i in ${!SSL_DOMAINS[*]}; do
LIST_CERTIFICATE_DOMAINS=$(echo "${SSL_DOMAINS[$i]}" | cut -d ':' -f 2)
Expand Down Expand Up @@ -127,6 +139,6 @@ for TEMPLATE_NAME in ${PACKAGE_DOCKER_FOLDER_CONTAINER}/nginx/conf-dynamic.d/*.c

TEMPLATE_CODE="${TEMPLATE_CODE//\$SSL_INCLUDE/$SSL_DIRECTIVE}"

echo "${TEMPLATE_CODE}" >> ${TARGET_CONFIG_PATH}
echo "${TEMPLATE_CODE}" >> "${TARGET_CONFIG_PATH}"
done
done
1 change: 1 addition & 0 deletions docs/ENVIRONMENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ Property | Values | Description
`HOST_NGINX_TEMPLATE_PATH`| `(string)` `Default: ${VENDOR_DIR}/conf/nginx-proxy.conf` | Template nginx proxy conf
`HOST_NGINX_CONF_DIR`| `(string)` `Default: /usr/local/etc/nginx/servers` | Nginx configs dir on host machine
`HOST_NGINX_RESTART_COMMAND`| `(string)` `Default: "sudo brew services restart nginx"` | Restart nginx command on host machine
`NGINX_DEFAULT_HOST` | `yes`/`no` `Default: no` | Enable/disable default nginx host

## <a id="auto-update-hosts-envs"></a>Auto update hosts env (host machine)
Property | Values | Description
Expand Down

0 comments on commit c632f98

Please sign in to comment.