From 1d3263c59860777aa8686185fe1caab967c29a94 Mon Sep 17 00:00:00 2001 From: Jason Zhu Date: Thu, 4 May 2023 22:53:28 +1000 Subject: [PATCH] Try to implement nginx to route between frontend and backend --- eg3/README.md | 4 +++ eg3/backend/Dockerfile | 5 ++++ eg3/backend/docker-compose.yaml | 8 ++++++ eg3/backend/index.js | 7 +++++ eg3/frontend/Dockerfile | 4 +++ eg3/frontend/docker-compose.yaml | 8 ++++++ eg3/frontend/index.html | 27 ++++++++++++++++++ eg3/proxy/Dockerfile | 4 +++ eg3/proxy/default.conf | 47 ++++++++++++++++++++++++++++++++ eg3/proxy/docker-compose.yaml | 8 ++++++ 10 files changed, 122 insertions(+) create mode 100644 eg3/README.md create mode 100644 eg3/backend/Dockerfile create mode 100644 eg3/backend/docker-compose.yaml create mode 100644 eg3/backend/index.js create mode 100644 eg3/frontend/Dockerfile create mode 100644 eg3/frontend/docker-compose.yaml create mode 100644 eg3/frontend/index.html create mode 100644 eg3/proxy/Dockerfile create mode 100644 eg3/proxy/default.conf create mode 100644 eg3/proxy/docker-compose.yaml diff --git a/eg3/README.md b/eg3/README.md new file mode 100644 index 0000000..f0d1be5 --- /dev/null +++ b/eg3/README.md @@ -0,0 +1,4 @@ +# e.g. 3: Create an Ningx Reverse Proxy With Docker + +How To Run Multiple Docker Containers Under One URL + diff --git a/eg3/backend/Dockerfile b/eg3/backend/Dockerfile new file mode 100644 index 0000000..58d7637 --- /dev/null +++ b/eg3/backend/Dockerfile @@ -0,0 +1,5 @@ +FROM node:10.15.3-alpine +WORKDIR /home/node/ +RUN npm install express --save +COPY index.js . +CMD [ "node", "index.js"] \ No newline at end of file diff --git a/eg3/backend/docker-compose.yaml b/eg3/backend/docker-compose.yaml new file mode 100644 index 0000000..43eae2c --- /dev/null +++ b/eg3/backend/docker-compose.yaml @@ -0,0 +1,8 @@ +version: '3.8' + +services: + backend: + container_name: backend + build: . + ports: + - "5000:5000" \ No newline at end of file diff --git a/eg3/backend/index.js b/eg3/backend/index.js new file mode 100644 index 0000000..fc8e049 --- /dev/null +++ b/eg3/backend/index.js @@ -0,0 +1,7 @@ +const express = require('express'); +const app = express(); +const port = 5000; +const version = '1.0.0'; + +app.get('/', (req, res) => res.send({ version })); +app.listen(port, () => console.log(`Listening on port ${port}`)); \ No newline at end of file diff --git a/eg3/frontend/Dockerfile b/eg3/frontend/Dockerfile new file mode 100644 index 0000000..3bb9c3c --- /dev/null +++ b/eg3/frontend/Dockerfile @@ -0,0 +1,4 @@ +FROM nginx:stable-alpine +WORKDIR /usr/share/nginx/html +RUN apk add curl +COPY index.html . \ No newline at end of file diff --git a/eg3/frontend/docker-compose.yaml b/eg3/frontend/docker-compose.yaml new file mode 100644 index 0000000..ace46c8 --- /dev/null +++ b/eg3/frontend/docker-compose.yaml @@ -0,0 +1,8 @@ +version: '3.8' + +services: + frontend: + container_name: frontend + build: . + # ports: + # - "80:80" \ No newline at end of file diff --git a/eg3/frontend/index.html b/eg3/frontend/index.html new file mode 100644 index 0000000..8cc004d --- /dev/null +++ b/eg3/frontend/index.html @@ -0,0 +1,27 @@ + + + +Frontend + + + +

My Application Version

+

+

+ + \ No newline at end of file diff --git a/eg3/proxy/Dockerfile b/eg3/proxy/Dockerfile new file mode 100644 index 0000000..57f5b46 --- /dev/null +++ b/eg3/proxy/Dockerfile @@ -0,0 +1,4 @@ +FROM nginx:stable-alpine +WORKDIR /etc/nginx/conf.d +COPY default.conf . +CMD [ "nginx", "-s", "reload"] \ No newline at end of file diff --git a/eg3/proxy/default.conf b/eg3/proxy/default.conf new file mode 100644 index 0000000..576e8d0 --- /dev/null +++ b/eg3/proxy/default.conf @@ -0,0 +1,47 @@ +server { + listen 80; + listen [::]:80; + server_name localhost; + + location / { + root /usr/share/nginx/html; + index index.html index.htm; + proxy_pass http://frontend; + } + + location /api { + proxy_pass http://backend:5000/; + } + + #error_page 404 /404.html; + + # redirect server error pages to the static page /50x.html + # + error_page 500 502 503 504 /50x.html; + location = /50x.html { + root /usr/share/nginx/html; + } + + # proxy the PHP scripts to Apache listening on 127.0.0.1:80 + # + #location ~ \.php$ { + # proxy_pass http://127.0.0.1; + #} + + # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 + # + #location ~ \.php$ { + # root html; + # fastcgi_pass 127.0.0.1:9000; + # fastcgi_index index.php; + # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; + # include fastcgi_params; + #} + + # deny access to .htaccess files, if Apache's document root + # concurs with nginx's one + # + #location ~ /\.ht { + # deny all; + #} +} \ No newline at end of file diff --git a/eg3/proxy/docker-compose.yaml b/eg3/proxy/docker-compose.yaml new file mode 100644 index 0000000..acd2a34 --- /dev/null +++ b/eg3/proxy/docker-compose.yaml @@ -0,0 +1,8 @@ +version: '3.8' + +services: + proxy: + container_name: proxy + build: . + ports: + - "80:80" \ No newline at end of file