Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ###https://medium.com/@mannycodes/create-an-nginx-reverse-proxy-with-docker-a1c0aa9078f1####
- docker run -it -d --name backend node:10.15.3-alpine;
- docker exec -it backend /bin/sh; apk add nano; cd /home/node; npm init; npm install expres --save; touch index.js; nano index.js;
- cat >> index.js << 'EOF'
- 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}`));
- EOF
- *****************************************************************************************************
- docker exec -it backend /bin/sh; apk add curl; curl localhost:5000;
- docker run -it -d --name frontend nginx:stable-alpine;
- docker exec -it frontend /bin/sh; apk add cur; curl localhost; apk add nano; cd /usr/share/nginx/html; rm index.html; touch index.html; nano index.html;
- cat >> index.html << 'EOF'
- <!DOCTYPE html>
- <html>
- <head>
- <title>Frontend</title>
- <script>
- window.onload = function () {
- fetch('/api', { method: 'get'}).then((response) => {
- const json = response.json();
- if (response.ok) {
- return json;
- }
- return Promise.reject(new Error('Something went wrong.'));
- })
- .then((response) => {
- document.getElementById('version').innerHTML = JSON.stringify(response);
- }).catch((error) => {
- document.getElementById('error').innerHTML = error && error.message || 'Something else went wrong.';
- });
- };
- </script>
- </head>
- <body>
- <h1>My Application Version</h1>
- <p id="version"></p>
- <p id="error"></p>
- </body>
- </html>
- EOF
- *****************************************************************************************************
- docker network create mynetwork;
- docker network connect mynetwork backend;
- docker network connect mynetwork frontend;
- docker network inspect mynetwork;
- docker exec -it frontend /bin/sh; curl http://backend:5000;
- *****************************************************************************************************
- docker run -it -d -p 80:80 --network=mynetwork --name proxy nginx:stable-alpine;
- docker exec -it proxy /bin/sh; cd /etc/nginx/conf.d;
- cat >> default.conf << 'EOF'
- ...
- location / {
- root /usr/share/nginx/html;
- index index.html index.htm;
- proxy_pass http://frontend;
- } location /api {
- proxy_pass http://backend:5000/;
- }
- ...
- nginx -s reload;
- apk add curl; curl http://frontend; curl http://localhost; curl http://backend:5000; curl http://localhost/api;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement