为不同端口分配不同的子域

发布于 2025-02-06 01:57:08 字数 334 浏览 2 评论 0原文

我有一个带有公共外部IP的VM实例,说10.20.30.40

现在我通过python http服务器在端口80托管我的文件

,并在端口8000上运行fastapi应用程序

...

假设我拥有一个域 abc.com

我想指向

-ABC.com -----> 10.20.30.40

api.abc.com -----> 10.20.30.40:8000

...

但是我无法使用DNS记录将域指向特定端口,

是否可以做到这一点或任何其他替代解决方案以实现相似的行为?

I have a vm instance with a public external ip say 10.20.30.40

Now i am hosting my files via python http server at port 80

and running a fastapi app at port 8000

...

Let's say i own a domain abc.com

I want to point -

abc.com ----> 10.20.30.40

api.abc.com ----> 10.20.30.40:8000

...

but i won't be able to point a domain to specific port using dns records

Is there a way to do that or any other alternate solution to achieve similar behaviour ??

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

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

发布评论

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

评论(1

总以为 2025-02-13 01:57:08

您要实现的目标通常是使用“反向代理服务器”完成的。它的工作是寻找特定的URL路径,并将您的流量重定向到相应端口上的相应服务器。

搜索“反向代理nginx”以找到一个微小的简单解决方案。 (该服务器甚至可以与您当前正在使用的VM相同)。那里还有其他解决方案。例如,Traefik或Squid

您所做的事情基本上是将两个子域的两个子域路由到同一外部IP。最好的是让您的反向代理服务器在端口80上运行(或TLS的443),这样您就不必在使用浏览器时指定端口。给Python HTTP服务器另一个端口要收听(例如8001)。
然后,您将反向代理配置为将流量重定向到Localhost:8000(适用于API)和Localhost:8001(对于Python HTTP服务器)

What you want to achieve is typically done with a "reverse proxy server". What is does is it looks for specific url paths and redirects your traffic to the corresponding server on the corresponding port.

Search for 'reverse proxy nginx' for a tiny simple solution. (This server could even run on the same VM as you are currently working on). There are other solutions out there. E.g. Traefik or Squid

What you do then is basically route both your subdomains to your same external IP. Best would be to let your reverse proxy server run on port 80 (or 443 for TLS) so you dont have to specify a port when using a browser. Give the python http server another port to listen on (e.g. 8001).
Then you configure your reverse proxy to redirect your traffic to localhost:8000 (for API) and to localhost:8001 (for python http server)

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