Skip to content

Commit 661f58f

Browse files
committed
platform/#1492: support host and path redirects.map and add new nginx image version
1 parent faf9ffe commit 661f58f

File tree

6 files changed

+121
-1
lines changed

6 files changed

+121
-1
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,13 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

8+
## 2022-09-16
9+
10+
### Added
11+
12+
- Add the support in the `redirects.map` file to use `$host$request_uri` as key (left-side) to manage multiple domains on the same nginx instance.
13+
- Add the new `1.23.1-alpine` image.
14+
815
## 2022-05-03
916

1017
### Added

Dockerfile-1.23.1-alpine

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
FROM nginx:1.23.1-alpine
2+
3+
# Pass inexistent UUID (e.g.: 1001) to enhance the container security
4+
ARG user=root
5+
6+
LABEL org.opencontainers.image.source https://github.com/sparkfabrik/docker-php-drupal-nginx/tree/feature/d8
7+
8+
COPY docker-entrypoint.sh /docker-entrypoint.sh
9+
COPY templates /templates
10+
COPY config/custom.conf /etc/nginx/conf.d/000-custom.conf
11+
12+
RUN chmod +x /docker-entrypoint.sh && \
13+
chmod 775 /etc/nginx && \
14+
chmod 775 /etc/nginx/conf.d && \
15+
chmod 664 /etc/nginx/conf.d/000-custom.conf && \
16+
mkdir -p /etc/nginx/conf.d/custom && \
17+
chmod 775 /etc/nginx/conf.d/custom && \
18+
mkdir -p /etc/nginx/conf.d/fragments && \
19+
chmod 775 /etc/nginx/conf.d/fragments && \
20+
chgrp root /var/cache/nginx && \
21+
chmod 775 /var/cache/nginx && \
22+
chmod 775 /var/log/nginx && \
23+
mkdir -p /var/run/nginx && \
24+
chmod 775 /var/run/nginx && \
25+
sed -i 's|/var/run/nginx.pid|/var/run/nginx/nginx.pid|g' /etc/nginx/nginx.conf && \
26+
chmod 775 /templates && \
27+
chmod 664 /etc/nginx/fastcgi.conf && \
28+
chmod 664 /etc/nginx/conf.d/default.conf
29+
30+
# Go to target user
31+
USER $user
32+
33+
ENTRYPOINT [ "/docker-entrypoint.sh" ]

Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,26 +9,30 @@ build:
99
docker build -f Dockerfile-1.17.6-alpine -t $(IMAGE_NAME):1.17.6-alpine.d8 --build-arg user=root .
1010
docker build -f Dockerfile-1.21.1-alpine -t $(IMAGE_NAME):1.21.1-alpine.d8 --build-arg user=root .
1111
docker build -f Dockerfile-1.21.6-alpine -t $(IMAGE_NAME):1.21.6-alpine.d8 --build-arg user=root .
12+
docker build -f Dockerfile-1.23.1-alpine -t $(IMAGE_NAME):1.23.1-alpine.d8 --build-arg user=root .
1213

1314
test:
1415
@chmod +x ./tests/tests.sh
1516
@IMAGE_NAME=$(IMAGE_NAME) IMAGE_TAG=1.13.6-alpine.d8 ./tests/tests.sh
1617
@IMAGE_NAME=$(IMAGE_NAME) IMAGE_TAG=1.17.6-alpine.d8 ./tests/tests.sh
1718
@IMAGE_NAME=$(IMAGE_NAME) IMAGE_TAG=1.21.1-alpine.d8 ./tests/tests.sh
1819
@IMAGE_NAME=$(IMAGE_NAME) IMAGE_TAG=1.21.6-alpine.d8 ./tests/tests.sh
20+
@IMAGE_NAME=$(IMAGE_NAME) IMAGE_TAG=1.23.1-alpine.d8 ./tests/tests.sh
1921

2022
build-rootless:
2123
docker build -f Dockerfile-1.13.6-alpine -t $(IMAGE_NAME):1.13.6-alpine.d8-rootless --build-arg user=1001 .
2224
docker build -f Dockerfile-1.17.6-alpine -t $(IMAGE_NAME):1.17.6-alpine.d8-rootless --build-arg user=1001 .
2325
docker build -f Dockerfile-1.21.1-alpine -t $(IMAGE_NAME):1.21.1-alpine.d8-rootless --build-arg user=1001 .
2426
docker build -f Dockerfile-1.21.6-alpine -t $(IMAGE_NAME):1.21.6-alpine.d8-rootless --build-arg user=1001 .
27+
docker build -f Dockerfile-1.23.1-alpine -t $(IMAGE_NAME):1.23.1-alpine.d8-rootless --build-arg user=1001 .
2528

2629
test-rootless:
2730
@chmod +x ./tests/tests.sh
2831
@IMAGE_NAME=$(IMAGE_NAME) IMAGE_TAG=1.13.6-alpine.d8-rootless IMAGE_USER="unknown uid 1001" BASE_TESTS_PORT="8080" ./tests/tests.sh
2932
@IMAGE_NAME=$(IMAGE_NAME) IMAGE_TAG=1.17.6-alpine.d8-rootless IMAGE_USER="unknown uid 1001" BASE_TESTS_PORT="8080" ./tests/tests.sh
3033
@IMAGE_NAME=$(IMAGE_NAME) IMAGE_TAG=1.21.1-alpine.d8-rootless IMAGE_USER="unknown uid 1001" BASE_TESTS_PORT="8080" ./tests/tests.sh
3134
@IMAGE_NAME=$(IMAGE_NAME) IMAGE_TAG=1.21.6-alpine.d8-rootless IMAGE_USER="unknown uid 1001" BASE_TESTS_PORT="8080" ./tests/tests.sh
35+
@IMAGE_NAME=$(IMAGE_NAME) IMAGE_TAG=1.23.1-alpine.d8-rootless IMAGE_USER="unknown uid 1001" BASE_TESTS_PORT="8080" ./tests/tests.sh
3236

3337
shellcheck-build:
3438
@docker build -f shellcheck/Dockerfile -t sparkfabrik/shellchek shellcheck

README.md

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,3 +87,62 @@ If you provide a non-root user the container will drop its privileges targeting
8787
We have inserted the specific make targets with dedicated image suffix tags (`-rootless`) for these flavours.
8888

8989
You can find some more information [here](https://docs.bitnami.com/tutorials/work-with-non-root-containers/).
90+
91+
## redirects.map file
92+
93+
You can use the file `/etc/nginx/conf.d/redirects.map` to specify the static redirection that you want to manage. Each line of the file represents a key-value pair used to verify the current request and to send the 301 header and the corresponding new `Location` to the client. The file structure is:
94+
95+
```bash
96+
<key to match> <new localtion>;
97+
```
98+
99+
The key to match, or the left side of the file, could be the host and path (e.g.: `www.example.com/my-awesome-path`) or only the path (e.g.: `/my-awesome-path`). In the case of the same path, the more detailed rule will be applied (host and path), **ignoring the order in the file**. **ATTENTION**: as described, the left part of the file is treated as a key, so you must avoid any conflict.
100+
101+
### Example
102+
103+
Here you can find an example for the `redirects.map` file:
104+
105+
```bash
106+
# The line below will send a redirect only for .com TLD to a new path
107+
www.example.com/my-awesome-path https://www.example.com/my-new-awesome-path;
108+
# The line below will send a redirect only for .co.uk TLD to a new path
109+
www.example.co.uk/my-awesome-path https://www.example.co.uk/my-new-awesome-path;
110+
# The line below will send a redirect for all other domains to the .it TLD and to a new path
111+
/my-awesome-path https://www.example.it/another-awesome-path;
112+
# The line below will send a redirect only for .fr TLD to a new path
113+
www.example.fr/my-awesome-path https://www.example.fr/my-new-awesome-path;
114+
```
115+
116+
With this configuration, the nginx server will first test the host and path key, and if there is no match it will then also test the simple path. You can find some examples of response header below, obtained when using the previous `redirects.map` file:
117+
118+
```bash
119+
$ curl --head -H "Host: www.example.com" http://localhost/my-awesome-path
120+
HTTP/1.1 301 Moved Permanently
121+
Location: https://www.example.com/my-new-awesome-path
122+
```
123+
124+
```bash
125+
$ curl --head -H "Host: www.example.co.uk" http://localhost/my-awesome-path
126+
HTTP/1.1 301 Moved Permanently
127+
Location: https://www.example.co.uk/my-new-awesome-path
128+
```
129+
130+
```bash
131+
$ curl --head -H "Host: www.example.eu" http://localhost/my-awesome-path
132+
HTTP/1.1 301 Moved Permanently
133+
Location: https://www.example.it/my-new-awesome-path
134+
```
135+
136+
```bash
137+
$ curl --head -H "Host: www.example.fr" http://localhost/my-awesome-path
138+
HTTP/1.1 301 Moved Permanently
139+
Location: https://www.example.fr/my-new-awesome-path
140+
```
141+
142+
```bash
143+
$ curl --head -H "Host: www.example.de" http://localhost/my-awesome-path
144+
HTTP/1.1 301 Moved Permanently
145+
Location: https://www.example.it/my-new-awesome-path
146+
```
147+
148+
You can find the redirects.map test file in the `example` folder of this repo.

examples/redirects.map

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# The line below will send a redirect only for .com TLD to a new path
2+
www.example.com/my-awesome-path https://www.example.com/my-new-awesome-path;
3+
# The line below will send a redirect only for .co.uk TLD to a new path
4+
www.example.co.uk/my-awesome-path https://www.example.co.uk/my-new-awesome-path;
5+
# The line below will send a redirect for all other domains to the .it TLD and to a new path
6+
/my-awesome-path https://www.example.it/another-awesome-path;
7+
# The line below will send a redirect only for .fr TLD to a new path
8+
www.example.fr/my-awesome-path https://www.example.fr/my-new-awesome-path;

templates/default.conf

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,12 @@ map $http_x_forwarded_proto $fastcgi_https {
88
https on;
99
}
1010

11-
# We should use $request_uri to support redirect with query parameters.
11+
# We should use $host$request_uri to support redirects when nginx is serving multiple domains.
12+
map $host$request_uri $new_host_and_uri {
13+
include /etc/nginx/conf.d/redirects[.]map;
14+
}
15+
16+
# We should use $request_uri to support redirects with query parameters.
1217
map $request_uri $new_uri {
1318
include /etc/nginx/conf.d/redirects[.]map;
1419
}
@@ -26,6 +31,10 @@ server {
2631
# default, catch all server, regardless of server_name.
2732
listen ${NGINX_DEFAULT_SERVER_PORT} ${DEFAULT_SERVER};
2833

34+
if ($new_host_and_uri) {
35+
return 301 $new_host_and_uri;
36+
}
37+
2938
if ($new_uri) {
3039
return 301 $new_uri;
3140
}

0 commit comments

Comments
 (0)