Skip to content

Commit

Permalink
feat: add NGINX environment variable to add additional headers
Browse files Browse the repository at this point in the history
  • Loading branch information
Eisie96 authored and shauke committed Nov 29, 2023
1 parent e266e28 commit f2b664a
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 1 deletion.
1 change: 1 addition & 0 deletions .vscode/intershop.txt
Expand Up @@ -79,6 +79,7 @@ keyvalue
kubernetes
lessthan
logformat
loglevel
maxlength
mergeable
minibasket
Expand Down
3 changes: 3 additions & 0 deletions docker-compose.yml
Expand Up @@ -109,6 +109,9 @@ services:
- baseHref: /b2c
channel: default
theme: b2c
# ADDITIONAL_HEADERS: |
# headers:
# - X-Frame-Options: 'SAMEORIGIN'

# Logging to an External Device (see logging.md)
# volumes:
Expand Down
20 changes: 20 additions & 0 deletions docs/guides/nginx-startup.md
Expand Up @@ -136,6 +136,26 @@ Alternatively, the source can be supplied by setting `OVERRIDE_IDENTITY_PROVIDER

If no environment variable is set, this feature is disabled.

### Add additional headers

For some security or functional reasons it is necessary to add additional headers to page responses.
To make such headers configurable, the environment variable `ADDITIONAL_HEADERS` is introduced.

```yaml
nginx:
environment:
ADDITIONAL_HEADERS: |
headers:
- header-a: 'value-a'
- header-b: 'value-b'
```

Alternatively, the source can be supplied by setting `ADDITIONAL_HEADERS_SOURCE` in any [supported format by gomplate](https://docs.gomplate.ca/datasources/).

For every entry nginx will add this header to every possible response.

To make the additional headers available during build-time, the value for the environment variable `ADDITIONAL_HEADERS` can be put into the [additional-headers.yaml](../../nginx/additional-headers.yaml) file.

### Other

Built-in features can be enabled and disabled:
Expand Down
1 change: 1 addition & 0 deletions nginx/additional-headers.yaml
@@ -0,0 +1 @@
headers:
12 changes: 11 additions & 1 deletion nginx/docker-entrypoint.d/40-gomplate.sh
Expand Up @@ -29,4 +29,14 @@ then
fi
fi

/gomplate -d "domains=$MULTI_CHANNEL_SOURCE" -d "overrideIdentityProviders=$OVERRIDE_IDENTITY_PROVIDERS_SOURCE" -d "cachingIgnoreParams=$CACHING_IGNORE_PARAMS_SOURCE" -d 'ipwhitelist=env:///BASIC_AUTH_IP_WHITELIST?type=application/yaml' --input-dir="/etc/nginx/templates" --output-map='/etc/nginx/conf.d/{{ .in | strings.ReplaceAll ".conf.tmpl" ".conf" }}'
if [ -z "$ADDITIONAL_HEADERS_SOURCE" ]
then
if [ -z "$ADDITIONAL_HEADERS" ]
then
ADDITIONAL_HEADERS_SOURCE="./additional-headers.yaml"
else
ADDITIONAL_HEADERS_SOURCE="env:///ADDITIONAL_HEADERS?type=application/yaml"
fi
fi

/gomplate -d "domains=$MULTI_CHANNEL_SOURCE" -d "overrideIdentityProviders=$OVERRIDE_IDENTITY_PROVIDERS_SOURCE" -d "cachingIgnoreParams=$CACHING_IGNORE_PARAMS_SOURCE" -d "additionalHeaders=$ADDITIONAL_HEADERS_SOURCE" -d 'ipwhitelist=env:///BASIC_AUTH_IP_WHITELIST?type=application/yaml' --input-dir="/etc/nginx/templates" --output-map='/etc/nginx/conf.d/{{ .in | strings.ReplaceAll ".conf.tmpl" ".conf" }}'
7 changes: 7 additions & 0 deletions nginx/templates/add-header.conf.tmpl
@@ -0,0 +1,7 @@
{{ $headers := (datasource "additionalHeaders").headers -}}

{{- range $headers }}
{{- range $key, $val := (.) }}
add_header {{ $key }} "{{ $val }}";
{{- end}}
{{- end }}
2 changes: 2 additions & 0 deletions nginx/templates/multi-channel.conf.tmpl
Expand Up @@ -161,6 +161,7 @@ server {
{{ if (has $mapping "channel") }}
location / {
{{- tmpl.Exec "LOCATION_TEMPLATE" $mapping }}
include /etc/nginx/conf.d/add-header.conf;
}
location ^~ /sitemap_ {
{{- tmpl.Exec "LOCATION_TEMPLATE_FOR_SITEMAP" $mapping }}
Expand All @@ -169,6 +170,7 @@ server {
{{ range $mapping }}
location {{ .baseHref }} {
{{- tmpl.Exec "LOCATION_TEMPLATE" . }}
include /etc/nginx/conf.d/add-header.conf;
}
location ^~ {{ .baseHref }}{{if not ( .baseHref | strings.HasSuffix "/")}}/{{end}}sitemap_ {
{{- tmpl.Exec "LOCATION_TEMPLATE_FOR_SITEMAP" . }}
Expand Down

0 comments on commit f2b664a

Please sign in to comment.