Advertisement
thantzinz

nginx_reverse_proxy

Mar 7th, 2020
794
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.56 KB | None | 0 0
  1. ###https://medium.com/@mannycodes/create-an-nginx-reverse-proxy-with-docker-a1c0aa9078f1####
  2. docker run -it -d --name backend node:10.15.3-alpine;
  3. docker exec -it backend /bin/sh; apk add nano; cd /home/node; npm init; npm install expres --save; touch index.js; nano index.js;
  4. cat >> index.js << 'EOF'
  5. const express = require('express');
  6. const app = express();
  7. const port = 5000;
  8. const version = '1.0.0';app.get('/', (req, res) => res.send({ version }));app.listen(port, () => console.log(`Listening on port ${port}`));
  9. EOF
  10.  
  11. *****************************************************************************************************
  12. docker exec -it backend /bin/sh; apk add curl; curl localhost:5000;
  13.  
  14. docker run -it -d --name frontend nginx:stable-alpine;
  15. 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;
  16. cat >> index.html << 'EOF'
  17. <!DOCTYPE html>
  18. <html>
  19. <head>
  20. <title>Frontend</title>
  21. <script>
  22. window.onload = function () {
  23. fetch('/api', { method: 'get'}).then((response) => {
  24. const json = response.json();
  25. if (response.ok) {
  26. return json;
  27. }
  28. return Promise.reject(new Error('Something went wrong.'));
  29. })
  30. .then((response) => {
  31. document.getElementById('version').innerHTML = JSON.stringify(response);
  32. }).catch((error) => {
  33. document.getElementById('error').innerHTML = error && error.message || 'Something else went wrong.';
  34. });
  35. };
  36. </script>
  37. </head>
  38. <body>
  39. <h1>My Application Version</h1>
  40. <p id="version"></p>
  41. <p id="error"></p>
  42. </body>
  43. </html>
  44. EOF
  45.  
  46. *****************************************************************************************************
  47. docker network create mynetwork;
  48. docker network connect mynetwork backend;
  49. docker network connect mynetwork frontend;
  50. docker network inspect mynetwork;
  51. docker exec -it frontend /bin/sh; curl http://backend:5000;
  52. *****************************************************************************************************
  53. docker run -it -d -p 80:80 --network=mynetwork --name proxy nginx:stable-alpine;
  54. docker exec -it proxy /bin/sh; cd /etc/nginx/conf.d;
  55. cat >> default.conf << 'EOF'
  56. ...
  57. location / {
  58. root /usr/share/nginx/html;
  59. index index.html index.htm;
  60. proxy_pass http://frontend;
  61. } location /api {
  62. proxy_pass http://backend:5000/;
  63. }
  64. ...
  65. nginx -s reload;
  66. 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