Skip to content

Commit 65effb9

Browse files
Merge pull request #68 from UKHomeOffice/custom-error-pages
Add support for individual custom error pages
2 parents 9923d43 + 0ed2713 commit 65effb9

File tree

9 files changed

+43
-9
lines changed

9 files changed

+43
-9
lines changed

README.md

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,9 @@ rules to be specified without downloading or mounting in a rule file.
3838
for easy tracking in down stream logs e.g. `nginxId=50c91049-667f-4286-c2f0-86b04b27d3f0`.
3939
If set to `HEADER` it will add `nginxId` to the headers, not append to the get params.
4040
* `CLIENT_CERT_REQUIRED` - if set to `TRUE`, will deny access at this location, see [Client Certs](#client-certs).
41-
* `ERROR_REDIRECT_CODES` - Can override when Nginx will redirect requests to the error page. Defaults to
42-
"`500 501 502 503 504`"
41+
* `ERROR_REDIRECT_CODES` - Can override when Nginx will redirect requests to its own error page. Defaults to
42+
"`500 501 502 503 504`". To support a new code, say `505`, an error page must be provided at
43+
`/usr/local/openresty/nginx/html/505.shtml`, see [Useful File Locations](#useful-file-locations).
4344
* `ADD_NGINX_LOCATION_CFG` - Arbitrary extra NGINX configuration to be added to the location context, see
4445
[Arbitrary Config](#arbitrary-config).
4546
* `PORT_IN_HOST_HEADER` - If FALSE will remove the port from the http `Host` header.
@@ -101,9 +102,11 @@ N.B. see HTTP(S)_LISTEN_PORT above
101102
See `CLIENT_CERT_REQUIRED` above in [Environment Variables](#environment-variables).
102103
* `/usr/local/openresty/naxsi/*.conf` - [Naxsi](https://github.com/nbs-system/naxsi) rules location in default
103104
nginx.conf.
104-
* `/usr/local/openresty/nginx/html/50x.html` - HTML displayed when a 500 error occurs. See ERROR_REDIRECT_CODES to
105-
change this.
106-
105+
* `/usr/local/openresty/nginx/html/$CODE.shtml` - HTML (with SSI support) displayed when a the status code $CODE
106+
is encountered upstream and the proxy is configured to intercept. See ERROR_REDIRECT_CODES to change this.
107+
* `/usr/local/openresty/nginx/html/418-request-denied.shtml` - HTML (with SSI support) displayed when NAXSI
108+
blocks a request.
109+
107110
### Examples
108111

109112
#### Self signed SSL Certificate

ci-build.sh

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,24 @@ else
269269
exit 1
270270
fi
271271

272+
start_test "Test custom error pages..." "${STD_CMD} \
273+
-e \"PROXY_SERVICE_HOST=http://mockserver\" \
274+
-e \"PROXY_SERVICE_PORT=8080\" \
275+
-e \"DNSMASK=TRUE\" \
276+
-e \"ENABLE_UUID_PARAM=FALSE\" \
277+
-e \"ERROR_REDIRECT_CODES=502 404 500\" \
278+
--link mockserver:mockserver "
279+
if curl -k https://${DOCKER_HOST_NAME}:${PORT}/not-found | grep "404 Not Found" ; then
280+
if curl -k https://${DOCKER_HOST_NAME}:${PORT}/api/dead | grep "An error occurred" ; then
281+
echo "Passed custom error pages with ERROR_REDIRECT_CODES"
282+
else
283+
echo "Failed custom error pages with ERROR_REDIRECT_CODES on code 500"
284+
exit 1
285+
fi
286+
else
287+
echo "Failed custom error pages with ERROR_REDIRECT_CODES on code 404"
288+
exit 1
289+
fi
272290

273291
start_test "Start with Custom upload size" "${STD_CMD} \
274292
-e \"PROXY_SERVICE_HOST=http://mockserver\" \
@@ -390,5 +408,5 @@ echo "Testing no logging of url params option works..."
390408
docker logs mockserver | grep 'Nginxid:'
391409
docker logs ${INSTANCE} | grep '"nginx_uuid": "'
392410

393-
echo "__________________________________"
394-
echo "We got here, ALL tests successfull"
411+
echo "_________________________________"
412+
echo "We got here, ALL tests successful"

enable_location.sh

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,9 +135,17 @@ else
135135
msg "Auto UUID request parameter enabled for location ${LOCATION_ID}."
136136
touch ${UUID_FILE}
137137
fi
138+
138139
if [ "${ERROR_REDIRECT_CODES}" == "" ]; then
139140
ERROR_REDIRECT_CODES="${DEFAULT_ERROR_CODES}"
140141
fi
142+
ERROR_PAGES=""
143+
for code in ${ERROR_REDIRECT_CODES}; do
144+
# Set up an individual error page for each code
145+
msg "Enabling redirect on status code: ${code}"
146+
ERROR_PAGES="${ERROR_PAGES} error_page ${code} /nginx-proxy/${code}.shtml;"
147+
done
148+
141149
if [ "${ENABLE_WEB_SOCKETS}" == "TRUE" ]; then
142150
msg "Enable web socket support"
143151
WEB_SOCKETS="include ${NGIX_CONF_DIR}/nginx_web_sockets_proxy.conf;"
@@ -183,7 +191,7 @@ location ${LOCATION} {
183191
${BASIC_AUTH_CONFIG}
184192
${DENY_COUNTRY}
185193
186-
error_page ${ERROR_REDIRECT_CODES} /nginx-proxy/50x.shtml;
194+
${ERROR_PAGES}
187195
188196
set \$proxy_address "${PROXY_SERVICE_HOST}:${PROXY_SERVICE_PORT}";
189197

html/500.shtml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<!--#include file="50x.shtml" -->

html/501.shtml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<!--#include file="50x.shtml" -->

html/502.shtml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<!--#include file="50x.shtml" -->

html/503.shtml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<!--#include file="50x.shtml" -->

html/504.shtml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<!--#include file="50x.shtml" -->

test-servers.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,4 @@
5454
code: 500
5555
body: '{"message": "Oh dear!"}'
5656
headers:
57-
content-type: text/json
57+
content-type: text/json

0 commit comments

Comments
 (0)