简体   繁体   English

如何只允许从 Docker 中的前端访问后端

[英]How to allow access to backend only from frontend in Docker

I have an application, React for frontend, Node.js for backend.我有一个应用程序,React 用于前端,Node.js 用于后端。 The application will be uploaded to Digital Ocean, how can I hide the backend so that users have access only to the frontend.该应用程序将被上传到 Digital Ocean,我如何隐藏后端以便用户只能访问前端。

The reason why you cannot hide your backend is that React uses Client-side rendering by default.不能隐藏后端的原因是 React 默认使用客户端渲染。 This means that the client uses the browser to make calls to the backend.这意味着客户端使用浏览器调用后端。 There are ways to make your backend secure such as using an access token or putting a Web Application Firewall in front of the server.有一些方法可以使您的后端安全,例如使用访问令牌或在服务器前面放置 Web 应用程序防火墙。 I believe there are ways to make your frontend in react to work as a Server-Side rendering, but as default and as a Single Page Application will do Client-Side rendering.我相信有一些方法可以让您的前端作为服务器端渲染做出反应,但默认情况下,单页应用程序将进行客户端渲染。

This article may help you to understand: https://www.toptal.com/front-end/client-side-vs-server-side-pre-rendering这篇文章可以帮助你理解: https://www.toptal.com/front-end/client-side-vs-server-side-pre-rendering

If docker use: You can split the frontend and backend into different containers, for the frontend to make access from outside, and the backend to hide behind the network inside the Docker.如果 docker 使用:可以将前端和后端拆分为不同的容器,前端可以从外部访问,后端隐藏在 Docker 内部的网络后面。 Your frontend will call backend over Docker's internal network by application name.您的前端将通过应用程序名称通过 Docker 的内部网络调用后端。

You can also hide the frontend and backend using nginx.您还可以使用 nginx 隐藏前端和后端。 Use nginx to access the frontend inside your docker network.使用 nginx 访问 docker 网络中的前端。 It turns out that access from the outside will only be to nginx.事实证明,从外部访问只能访问 nginx。

read more https://docs.docker.com/compose/networking/阅读更多https://docs.docker.com/compose/networking/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM