Skip to content
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

feat: add posibility to deploy nginx with selected features #386

Merged
merged 1 commit into from
Sep 28, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ Dockerfile*
/tslint-rules/test
/schematics/src/**/files/**
/schematics/src/**/*.js
/nginx/*.conf
/nginx/*.tmpl
/nginx/**/*.conf
/nginx/**/*.tmpl
/.idea/
/charts/**/templates/*
*~
Expand Down
5 changes: 3 additions & 2 deletions nginx/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ RUN echo "intalling nginx with mod-pagespeed" && \

FROM scratch as configstep
COPY --from=nginx:mainline /etc/nginx /etc/nginx
COPY nginx.conf perf.conf pagespeed.conf device.conf /etc/nginx/
COPY nginx.conf /etc/nginx/
COPY features /etc/nginx/features/
COPY channel.conf.tmpl /etc/nginx/conf.d/
COPY entrypoint.sh /
COPY 50x.html /usr/share/nginx/html/
Expand All @@ -20,7 +21,7 @@ RUN apt-get update && \
rm -r /var/cache/apt /var/lib/apt/lists
COPY --from=buildstep /usr/local/nginx /usr/local/nginx
COPY --from=configstep / /
ENV NPSC_ENABLE_FILTERS=in_place_optimize_for_browser,prioritize_critical_css,inline_preview_images,lazyload_images,rewrite_images,rewrite_css,remove_comments,move_css_to_head,move_css_above_scripts,collapse_whitespace,combine_javascript,extend_cache NPSC_JsPreserveURLs=off NPSC_ImagePreserveURLs=on NPSC_ForceCaching=off
ENV NPSC_ENABLE_FILTERS=in_place_optimize_for_browser,prioritize_critical_css,inline_preview_images,lazyload_images,rewrite_images,rewrite_css,remove_comments,move_css_to_head,move_css_above_scripts,collapse_whitespace,combine_javascript,extend_cache NPSC_JsPreserveURLs=off NPSC_ImagePreserveURLs=on NPSC_ForceCaching=off CACHE=on COMPRESSION=on PAGESPEED=on DEVICE_DETECTION=on

EXPOSE 80 443

Expand Down
18 changes: 18 additions & 0 deletions nginx/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ This sub project provides a docker image of [nginx](https://www.nginx.com/) supp
- Enabled compression for downstream services
- Caching for PWA Universal responses
- Easy to configure multi site handling via sub domains with environment variables
- Device type detection for pre-rendering the page fitting to the incoming user agent.

## Build it!

Expand Down Expand Up @@ -36,6 +37,13 @@ Temper with the default Page Speed configuration:

- set the environment variable `NPSC_ENABLE_FILTERS` to a comma-separated list of active [Page Speed Filters](https://www.modpagespeed.com/examples/) to override our carefully chosen defaults. Do this at your own risk!

Enable and disable features:

- use `CACHE=off` to disable caching (default `on`)
- use `PAGESPEED=off` to disable pagespeed optimizations (default `on`)
- use `COMPRESSION=off` to disable compression (default `on`)
- use `DEVICE_DETECTION=off` to disable user-agent detection (default `on`)

## Example

```
Expand All @@ -44,6 +52,8 @@ docker run -d --name "my-awesome-nginx" \
--restart always \
-p 4199:80 \
-e UPSTREAM_PWA=http://192.168.0.10:4200 \
-e CACHE=off \
-e PAGESPEED=off \
-e PWA_1_TOPLEVELDOMAIN=net \
-e PWA_1_CHANNEL=inSPIRED-inTRONICS-Site \
-e PWA_1_LANG=en_US \
Expand All @@ -66,3 +76,11 @@ docker run -d --name "my-awesome-nginx" \
And then access the PWA with `http://b2b.<your-fully-qualified-machine-name>:4199`

If your DNS is not set up correctly, you have to use something like _dnsmasq_ (Linux) or _Acrylic DNS Proxy_ (Windows), or just ask your local network administrator.

## Features

Features can be supplied in the folder `nginx/features`. A file named `<feature>.conf` is included if the environment variable `<feature>` is set to `on`, `1`, `true` or `yes` (checked case in-sensitive). The feature is disabled otherwise and an optional file `<feature>-off.conf` is included in the configuration. The feature name must be all word-characters (letters, numbers and underscore).

### Disabling Cache

If the cache feature is switched off all caching for pre-rendered pages is disabled. If the cache should also be disabled for static resources, the pagespeed feature also has to be switched off as it caches optimized images individually.
11 changes: 5 additions & 6 deletions nginx/channel.conf.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ server {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
add_header X-Cache-Status IGNORE;

proxy_no_cache true;
proxy_cache_bypass true;
add_header X-Cache-Status $upstream_cache_status;

proxy_pass $UPSTREAM_PWA;
proxy_buffer_size 128k;
Expand All @@ -26,7 +29,6 @@ server {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
add_header X-Cache-Status INHERITED;

proxy_pass $UPSTREAM_PWA;
proxy_buffer_size 128k;
Expand All @@ -36,15 +38,12 @@ server {

# cache and rewriting for rendered pages
location / {
proxy_cache my_cache;
proxy_cache_use_stale error timeout http_404 http_500 http_502 http_503 http_504;

proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
add_header X-Cache-Status $upstream_cache_status;

add_header X-Cache-Status $upstream_cache_status;
proxy_ignore_headers Cache-Control;
proxy_cache_valid 200 302 10m;
proxy_cache_valid 404 1m;
Expand Down
9 changes: 7 additions & 2 deletions nginx/entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ fi

[ -f "/etc/nginx/conf.d/default.conf" ] && rm /etc/nginx/conf.d/default.conf

find /etc/nginx/features/*.conf | xargs -I{} echo {} | sed -e "s%.*\/\(\w*\).conf%\1%" | grep -E '^\w+$' | while read feature; do echo "# $feature" ; env | grep -iqE "^$feature=(on|1|true|yes)$" && echo "include /etc/nginx/features/${feature}.conf;" || echo "include /etc/nginx/features/${feature}-off[.]conf;" ; done >/etc/nginx/conf.d/features.conf

i=1
while true
do
Expand Down Expand Up @@ -66,9 +68,12 @@ env | grep NPSC_ | sed -e 's/^NPSC_//g' -e "s/\([A-Z_]*\)=/\L\1=/g" -e "s/_\([a-

env | grep NPSC_ | sed -e 's/^[^=]*=//' -e 's/$/;/' > /tmp/pagespeed-suffix.txt

paste -d" " /tmp/pagespeed-prefix.txt /tmp/pagespeed-suffix.txt >> /etc/nginx/pagespeed.conf
paste -d" " /tmp/pagespeed-prefix.txt /tmp/pagespeed-suffix.txt >> /etc/nginx/features/pagespeed.conf

[ ! -z "$DEBUG" ] && find /etc/nginx -name '*.conf' -print -exec cat '{}' \;
if env | grep -iqE "^DEBUG=(on|1|true|yes)$"
then
find /etc/nginx -name '*.conf' -print -exec cat '{}' \;
fi

if [ -z "$*" ]
then
Expand Down
3 changes: 3 additions & 0 deletions nginx/features/cache.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
proxy_cache_path /tmp/cache levels=1:2 keys_zone=my_cache:10m max_size=10g inactive=60m use_temp_path=off;
proxy_cache my_cache;
proxy_cache_use_stale error timeout http_404 http_500 http_502 http_503 http_504;
2 changes: 0 additions & 2 deletions nginx/perf.conf → nginx/features/compression.conf
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,3 @@ gzip_proxied any;
gzip_buffers 16 8k;
gzip_types *;
gzip_vary on;

proxy_cache_path /tmp/cache levels=1:2 keys_zone=my_cache:10m max_size=10g inactive=60m use_temp_path=off;
3 changes: 3 additions & 0 deletions nginx/features/device_detection-off.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
map $http_user_agent $ua_device {
default 'desktop';
}
File renamed without changes.
File renamed without changes.
4 changes: 1 addition & 3 deletions nginx/nginx.conf
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,7 @@ http {

keepalive_timeout 65;

include /etc/nginx/device.conf;
include /etc/nginx/perf.conf;
include /etc/nginx/pagespeed.conf;
include /etc/nginx/conf.d/features.conf;

include /etc/nginx/conf.d/channel*.conf;
}
15 changes: 11 additions & 4 deletions templates/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,19 @@ services:
environment:
- LOGGING=true
- ICM_BASE_URL
# - PROXY_ICM=true
- TRUST_ICM=true
nginx:
build: ../nginx
depends_on:
- pwa
environment:
UPSTREAM_PWA: 'http://pwa:4200'
# DEBUG: 1
DEBUG: 1
CACHE: 0
PAGESPEED: 0
# COMPRESSION: 0
# DEVICE_DETECTION: 0
PWA_1_DOMAIN: 'example.com'
# PWA_1_SUBDOMAIN: "www"
# PWA_1_TOPLEVELDOMAIN: "com"
Expand All @@ -30,8 +36,9 @@ services:
PWA_2_LANG: de_DE
PWA_2_THEME: blue
PWA_3_TOPLEVELDOMAIN: 'net'
PWA_3_APPLICATION: 'smb-responsive'
PWA_3_CHANNEL: 'inSPIRED-inTRONICS-Site'
PWA_3_LANG: fr_FR
PWA_3_APPLICATION: 'rest'
PWA_3_CHANNEL: 'inSPIRED-inTRONICS_Business-Site'
PWA_3_LANG: en_US
PWA_3_FEATURES: quoting
ports:
- '4200:80'