Skip to content

Commit 224ff0e

Browse files
bryanlattenBryan Latten
andauthored
Feature: HTTPS support (#74)
Co-authored-by: Bryan Latten <[email protected]>
1 parent 5fbdd39 commit 224ff0e

File tree

7 files changed

+74
-10
lines changed

7 files changed

+74
-10
lines changed

.travis.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,14 @@ services:
1010

1111
script:
1212
- docker build -t nginxtest -f ${DOCKERFILE} .
13+
- mkdir certs
14+
- openssl genrsa -out ./certs/ca.key 2048
15+
- openssl req -new -key ./certs/ca.key -out ./certs/ca.csr -subj '/CN=localhost'
16+
- openssl x509 -req -days 365 -in ./certs/ca.csr -signkey ./certs/ca.key -out ./certs/ca.crt
1317
- docker run -p 8080:8080 -d nginxtest
1418
- docker run -p 8081:8080 -d --env-file ./.test.env nginxtest
19+
- docker run -p 8082:8080 -d -e SERVER_ENABLE_HTTPS=true -v $(pwd)/certs:/etc/nginx/certs:ro nginxtest
1520
- sleep 5
1621
- curl localhost:8080 | grep "Welcome to nginx!"
1722
- curl localhost:8081 | grep "Welcome to nginx!"
23+
- curl -k https://localhost:8082 | grep "Welcome to nginx!"

Dockerfile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ ENV CONTAINER_ROLE=web \
1313
# Using a non-privileged port to prevent having to use setcap internally
1414
EXPOSE ${CONTAINER_PORT}
1515

16-
# - Update security packages, only
16+
# - Update security packages, plus ca-certificates required for https
1717
# - Install pre-reqs
1818
# - Install latest nginx (development PPA is actually mainline development)
1919
# - Perform cleanup, ensure unnecessary packages are removed
@@ -25,6 +25,7 @@ RUN /bin/bash -e /security_updates.sh && \
2525
apt-get update -yqq && \
2626
apt-get install -yqq --no-install-recommends \
2727
nginx-light \
28+
ca-certificates \
2829
&& \
2930
apt-get remove --purge -yq \
3031
manpages \

Dockerfile-alpine

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ RUN adduser -D -S -H $NOT_ROOT_USER
1818

1919
RUN apk update --no-cache && \
2020
apk add \
21+
ca-certificates \
2122
nginx \
2223
&& \
2324
/bin/bash -e /clean.sh

Dockerfile-centos

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ EXPOSE ${CONTAINER_PORT}
1616
# - Update security packages, only
1717
RUN /bin/bash -e /security_updates.sh && \
1818
yum -y -q install epel-release && \
19-
yum -y -q install nginx && \
19+
yum -y -q install nginx ca-certificates && \
2020
yum -y -q remove epel-release && \
2121
/bin/bash -e /clean.sh
2222

README.md

Lines changed: 44 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,18 @@
22

33
https://hub.docker.com/r/behance/docker-nginx/tags/
44

5-
Ubuntu used by default
6-
Alpine builds available tagged as `-alpine`
7-
Centos builds available tagged as `-centos`
8-
95
Provides base OS, patches and stable nginx for quick and easy spinup.
106

11-
[S6](https://github.com/just-containers/s6-overlay) process supervisor is used for `only` for zombie reaping (as PID 1), boot coordination, and termination signal translation
7+
- Ubuntu used by default
8+
- Alpine builds available tagged as `-alpine`
9+
- Centos builds available tagged as `-centos`
10+
11+
12+
[S6](https://github.com/just-containers/s6-overlay) process supervisor is used for `only` for zombie reaping (as PID 1), boot coordination, and termination signal translation
1213

13-
[Goss](https://github.com/aelsabbahy/goss) is used for build-time testing.
14+
[Goss](https://github.com/aelsabbahy/goss) is used for build-time testing.
1415

15-
See parent(s) [docker-base](https://github.com/behance/docker-base) for additional configuration
16+
See parent(s) [docker-base](https://github.com/behance/docker-base) for additional configuration
1617

1718

1819
### Expectations
@@ -22,11 +23,45 @@ See parent(s) [docker-base](https://github.com/behance/docker-base) for addition
2223
- NOTE: Nginx is exposed and bound to an unprivileged port, `8080`
2324

2425

25-
### Security
26+
### Container Security
2627

2728
See parent [configuration](https://github.com/behance/docker-base#security)
2829

2930

31+
### HTTPS usage
32+
33+
To enable this container to serve HTTPS over its primary exposed port:
34+
- `SERVER_ENABLE_HTTPS` environment variable must be `true`
35+
- Certificates must be present in `/etc/nginx/certs` under the following names:
36+
- `ca.crt`
37+
- `ca.key`
38+
- Additionally, they must be marked read-only (0600)
39+
40+
#### Local development usage
41+
42+
To generate a self-signed certificate (won't work in most browsers):
43+
```
44+
openssl genrsa -out ca.key 2048
45+
openssl req -new -key ca.key -out ca.csr -subj '/CN=localhost'
46+
openssl x509 -req -days 365 -in ca.csr -signkey ca.key -out ca.crt
47+
```
48+
49+
Run the image in background, bind external port (443), flag HTTPS enabled, mount certificate:
50+
```
51+
docker run \
52+
-d
53+
-p 443:8080 \
54+
-e SERVER_ENABLE_HTTPS=true \
55+
-v {directory-containing-ca.crt-and-ca.key}:/etc/nginx/certs:ro
56+
behance/docker-nginx
57+
```
58+
59+
Test
60+
```
61+
curl -k -vvv https://{your-docker-machine-ip}
62+
```
63+
64+
3065
### Environment Variables
3166

3267
Variable | Example | Description
@@ -36,6 +71,7 @@ SERVER_INDEX | SERVER_INDEX index.html index.html index.php | Changes the defaul
3671
SERVER_APP_NAME | SERVER_APP_NAME='view' | Gets appended to the default logging format
3772
SERVER_GZIP_OPTIONS | SERVER_GZIP_OPTIONS=1 | Allows default set of static content to be served gzipped
3873
SERVER_SENDFILE | SERVER_SENDFILE=off | Allows runtime to specify value of nginx's `sendfile` (default, on)
74+
SERVER_ENABLE_HTTPS | SERVER_ENABLE_HTTPS=true | Enable encrypted transmission using certificates
3975
SERVER_KEEPALIVE | SERVER_KEEPALIVE=30 | Define HTTP 1.1's keepalive timeout
4076
SERVER_WORKER_PROCESSES | SERVER_WORKER_PROCESSES=4 | Set to the number of cores in the machine, or the number of cores allocated to container
4177
SERVER_WORKER_CONNECTIONS | SERVER_WORKER_CONNECTIONS=2048 | Sets up the number of connections for worker processes

container/root/etc/cont-init.d/10-nginx-config.sh

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,3 +74,12 @@ then
7474
echo "[nginx] setting client_body_buffer_size to ${SERVER_CLIENT_BODY_BUFFER_SIZE}"
7575
sed -i "s/client_body_buffer_size .*;/client_body_buffer_size ${SERVER_CLIENT_BODY_BUFFER_SIZE};/" $CONF_NGINX_SERVER
7676
fi
77+
78+
if [[ $SERVER_ENABLE_HTTPS ]]
79+
then
80+
echo "[nginx] enabling HTTPS"
81+
# Uncomment all ssl* directives in site configuration
82+
sed -i "s/^[ ]*#ssl/ ssl/" $CONF_NGINX_SITE
83+
# Add SSL to listen directive
84+
sed -i "s/^[ ]*listen ${CONTAINER_PORT}/ listen ${CONTAINER_PORT} ssl/" $CONF_NGINX_SITE
85+
fi

container/root/etc/nginx/sites-available/default

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,17 @@
11
server {
22
listen 8080;
33

4+
# Enable with env variable SERVER_ENABLE_HTTPS=true
5+
#ssl_certificate /etc/nginx/certs/ca.crt;
6+
#ssl_certificate_key /etc/nginx/certs/ca.key;
7+
8+
# https://ssl-config.mozilla.org/#server=nginx&version=1.17.7&config=intermediate&openssl=1.1.1d&hsts=false&ocsp=false&guideline=5.4
9+
#ssl_protocols TLSv1.2 TLSv1.3;
10+
#ssl_ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384;
11+
#ssl_prefer_server_ciphers off;
12+
#ssl_session_cache shared:SSL:10m;
13+
#ssl_session_timeout 10m;
14+
415
root /var/www/html;
516

617
# Doesn't broadcast version level of server software

0 commit comments

Comments
 (0)