Skip to content

Commit c3e7015

Browse files
committed
* Remove HTTPS from frontend and api
* Close their ports to the host * Add a gateway container to serve as ingress and do SSL termination for all of those instead * Add extra hosts to make that work (api.phpdocker.local, mailhog.phpdocker.local)
1 parent 0c30f89 commit c3e7015

File tree

6 files changed

+83
-31
lines changed

6 files changed

+83
-31
lines changed

Makefile

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
SHELL=/bin/bash
22
MKCERT_VERSION=v1.4.1
33
MKCERT_LOCATION=$(PWD)/bin/mkcert
4-
HOSTS_VERSION=3.5.0
4+
HOSTS_VERSION=3.6.3
55
HOSTS_LOCATION=$(PWD)/bin/hosts
66
PHPDOCKER_HOST=phpdocker.local
77

@@ -15,7 +15,7 @@ ifndef BINARY_ARCH
1515
BUILD_TAG:=$(shell date +'%Y-%m-%d-%H-%M-%S')-$(shell git rev-parse --short HEAD)
1616
endif
1717

18-
init: clean install-dependencies install-mkcert create-certs init-hosts build-local-php-container start load-fixtures
18+
init: clean install-dependencies install-mkcert create-certs install-hosts init-hosts build-local-php-container start load-fixtures
1919

2020
build-backend-php:
2121
docker build --target=deployment -t backend-php -f backend/docker/php-fpm/Dockerfile ./backend/
@@ -46,7 +46,7 @@ install-hosts:
4646
@if [[ ! -f '$(HOSTS_LOCATION)' ]]; then curl -sL 'https://raw.githubusercontent.com/xwmx/hosts/$(HOSTS_VERSION)/hosts' -o $(HOSTS_LOCATION); chmod +x $(HOSTS_LOCATION); fi;
4747

4848
create-certs:
49-
bin/mkcert -cert-file=infrastructure/local/local.pem -key-file=infrastructure/local/local.key.pem $(PHPDOCKER_HOST)
49+
bin/mkcert -cert-file=infrastructure/local/local.pem -key-file=infrastructure/local/local.key.pem "$(PHPDOCKER_HOST)" "*.$(PHPDOCKER_HOST)"
5050
cp infrastructure/local/local.pem infrastructure/local/webpack.pem
5151
cat infrastructure/local/local.key.pem >> infrastructure/local/webpack.pem
5252

@@ -55,6 +55,8 @@ clean-hosts:
5555

5656
init-hosts: clean-hosts
5757
sudo bin/hosts add 127.0.0.1 $(PHPDOCKER_HOST)
58+
sudo bin/hosts add 127.0.0.1 api.$(PHPDOCKER_HOST)
59+
sudo bin/hosts add 127.0.0.1 mailhog.$(PHPDOCKER_HOST)
5860

5961
clean:
6062
docker-compose down
@@ -72,13 +74,13 @@ open-frontend:
7274
# xdg-open https://phpdocker.local:5001
7375

7476
open-content-api:
75-
xdg-open https://phpdocker.local:5002/content
77+
xdg-open https://api.phpdocker.local/content
7678

7779
open-mailhog:
78-
xdg-open http://phpdocker.local:5003/
80+
xdg-open http://mailhog.phpdocker.local/
7981

8082
open-api-profiler:
81-
xdg-open https://phpdocker.local:5002/_profiler/latest?limit=10
83+
xdg-open https://api.phpdocker.local/_profiler/latest?limit=10
8284

8385
api-clear-cache:
8486
docker-compose exec php-fpm bin/console cache:clear

backend/docker/nginx/nginx.conf

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
server {
22
listen 80 default;
3-
listen 443 default_server ssl;
4-
5-
ssl_certificate /etc/infrastructure/local.pem;
6-
ssl_certificate_key /etc/infrastructure/local.key.pem;
73

84
client_max_body_size 108M;
95

docker-compose.yml

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,22 @@
44
version: "3.4"
55
services:
66

7+
# Main gateway to act as reverse proxy for all the other services via https://phpdocker.local
8+
# Uses the same certs as the others - we don't need them if we close their outside p
9+
gateway:
10+
depends_on:
11+
- api
12+
- frontend
13+
- mailhog
14+
image: nginx:alpine
15+
working_dir: /application
16+
volumes:
17+
- .:/application
18+
- ./infrastructure/local/gateway.nginx.conf:/etc/nginx/conf.d/default.conf
19+
ports:
20+
- "443:443"
21+
- "80:80"
22+
723
database:
824
image: postgres:9.6-alpine
925
working_dir: /application
@@ -18,11 +34,9 @@ services:
1834
image: nginx:alpine
1935
working_dir: /application
2036
volumes:
21-
- ./backend:/application
22-
- ./backend/docker/nginx/nginx.conf:/etc/nginx/conf.d/default.conf
23-
- ./infrastructure/local:/etc/infrastructure
24-
ports:
25-
- "5002:443"
37+
- ./backend:/application
38+
- ./backend/docker/nginx/nginx.conf:/etc/nginx/conf.d/default.conf
39+
- ./infrastructure/local:/etc/infrastructure
2640

2741
php-fpm:
2842
build:
@@ -38,23 +52,14 @@ services:
3852
image: node:12.0-alpine
3953
working_dir: /application
4054
stdin_open: true # Regression in react-scripts 3.4.1, fixed in 4.0 https://github.com/facebook/create-react-app/pull/8845
55+
environment:
56+
- REACT_APP_POST_API_URI=https://api.phpdocker.local/content/posts
57+
- REACT_APP_CONTACT_API_URI=https://api.phpdocker.local/contact
58+
- REACT_APP_GENERATOR_API_URI=https://api.phpdocker.local/generator
4159
volumes:
4260
- ./frontend:/application
4361
- ./infrastructure/local:/etc/infrastructure
44-
ports:
45-
- "5000:3000"
4662
command: yarn start
4763

48-
# admin:
49-
# image: node:12.0-alpine
50-
# working_dir: /application
51-
# volumes:
52-
# - ./admin:/application
53-
# ports:
54-
# - "5001:3000"
55-
# command: yarn start
56-
5764
mailhog:
5865
image: mailhog/mailhog:latest
59-
ports:
60-
- "5003:8025"

frontend/package.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,10 @@
1818
"semantic-ui-react": "^0.88.1"
1919
},
2020
"scripts": {
21-
"start": "HTTPS=true react-scripts start",
21+
"start": "react-scripts start",
2222
"build": "react-scripts build",
2323
"test": "react-scripts test",
24-
"eject": "react-scripts eject",
25-
"prestart": "rm ./node_modules/webpack-dev-server/ssl/server.pem -f && cp -f /etc/infrastructure/webpack.pem ./node_modules/webpack-dev-server/ssl/server.pem"
24+
"eject": "react-scripts eject"
2625
},
2726
"eslintConfig": {
2827
"extends": "react-app"

infrastructure/local/.gitkeep

Whitespace-only changes.
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# Set up gateway to different services
2+
# Note: the resolver and upstream set up per location is done (instead of simply proxy_pass http://servicename) to allow
3+
# the gateway to be online by itself (or with not all services up). Otherwise nginx exits.
4+
5+
# Redirect ALL non HTTPS traffic to HTTPS
6+
server {
7+
listen 80 default_server;
8+
listen [::]:80 default_server;
9+
server_name _;
10+
return 301 https://$host$request_uri;
11+
}
12+
13+
# Frontend
14+
server {
15+
server_name phpdocker.local;
16+
17+
listen 443 ssl;
18+
ssl_certificate /application/infrastructure/local/local.pem;
19+
ssl_certificate_key /application/infrastructure/local/local.key.pem;
20+
21+
location / {
22+
proxy_pass http://frontend:3000;
23+
}
24+
}
25+
26+
# Backend
27+
server {
28+
server_name api.phpdocker.local;
29+
30+
listen 443 ssl;
31+
ssl_certificate /application/infrastructure/local/local.pem;
32+
ssl_certificate_key /application/infrastructure/local/local.key.pem;
33+
34+
location / {
35+
proxy_pass http://api;
36+
}
37+
}
38+
39+
# Mailhog
40+
server {
41+
server_name mailhog.phpdocker.local;
42+
43+
listen 443 ssl;
44+
ssl_certificate /application/infrastructure/local/local.pem;
45+
ssl_certificate_key /application/infrastructure/local/local.key.pem;
46+
47+
location / {
48+
proxy_pass http://mailhog:8025;
49+
}
50+
}

0 commit comments

Comments
 (0)