前端/后端在Ubuntu服务器上服务20.04

发布于 2025-01-28 18:41:48 字数 748 浏览 4 评论 0原文

我正在研究我的第一个Web项目,因此使用Apache2使用Ubuntu Server 20.04。现在,我在网站上正确的前端/后端服务中挣扎。实际上,我无法以有意义的方式与后端进行交流。

这样做的“正确”方法是什么?我当前的结构是:

frontend (reactjs):

  • web服务器:apache2
  • ip:xxx.xxx.xxx.xxx:80

backend (php):

  • weberver:apache2(我允许端口8080在防火墙中)
  • IP:
    • 变体1:xxx.xxx.xxx.xxx:8080
    • 变体2:localhost:8080
    • 变体3:127.0.0.1:8080

我尝试了一些不同的方法来访问我的后端,但都失败了。

我的第一种方法是在与前端相同的公共IP(变体1)上服务后端,这导致所有后端文件公开可见。出于安全原因,这对我来说就像一个愚蠢的主意。

因此,我尝试了变体2和3,这两者都以相同的效果结尾,以至于我再也无法从前端访问后端了。 (我尝试通过“ http:// localhost:8080/index.php”上的React Axios访问后端

抱歉,如果这个问题听起来很愚蠢,但是我只是要了解服务器。

如果您需要更多信息,请告诉我。提前致谢。

I am working on my first web-project and therefore use an Ubuntu Server 20.04 with apache2. Now i struggle with the correct frontend/backend-serving of my website. Actually I can not communicate with my backend in a meaningful way.

What is the "correct" way to do so? My current structure is:

Frontend (ReactJS):

  • webserver: apache2
  • IP: XXX.XXX.XXX.XXX:80

Backend (PHP):

  • webserver: apache2 (I allowed port 8080 in the firewall)
  • IP:
    • variant 1: XXX.XXX.XXX.XXX:8080
    • variant 2: localhost:8080
    • variant 3: 127.0.0.1:8080

I tried some different approaches to access my backend, but all failed.

My first approach was to serve the backend on the same public IP (variant 1) as the frontend, which lead to having all backend-files public visible. For security reasons this seamed like a stupid idea to me.

So I tried variant 2 and 3, which both ended with the same effect, that I could not access the backend from the frontend any more. (I tried accessing the backend via React Axios on "http://localhost:8080/index.php")

How does it happen, that I can not access my localhost directly from the frontend?

Sorry if this question sounds stupid, but I am just about to learn about servers.

If you need more information, please tell me. Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

北渚 2025-02-04 18:41:48

我的第一种方法是在与前端相同的公共IP(变体1)上服务后端,这导致所有后端文件公开可见。出于安全原因,这对我来说像一个愚蠢的主意。

公共文件需要可见。这就是使他们公开的原因。

所以我尝试了变体2和3,这两者都以相同的效果结尾,以至于我再也无法从前端访问后端。 (我尝试通过“ http:// localhost:8080/index.php”上的React Axios访问后端)

Localhost的意思是“与客户端同一计算机”。

因此,它将尝试访问与浏览器相同的计算机上运行的服务器,而不是在上运行的服务器与托管HTML文档引用local -Host

My first approach was to serve the backend on the same public IP (variant 1) as the frontend, which lead to having all backend-files public visible. For security reasons this seamed like a stupid idea to me.

Public files need to be visible. That's what makes them public.

So I tried variant 2 and 3, which both ended with the same effect, that I could not access the backend from the frontend any more. (I tried accessing the backend via React Axios on "http://localhost:8080/index.php")

Localhost means "The same computer as the client".

So it will try to access a server running on the same computer as the browser, not a server running on the same computer as the server that hosted the HTML document referencing localhost.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文