gpt4 book ai didi

node.js - 如何使用 nginx docker 容器访问 localhost 上的服务器?

转载 作者:IT老高 更新时间:2023-10-28 12:42:59 25 4
gpt4 key购买 nike

我正在尝试使用 nginx 的 docker 化版本作为我的 Node (ExpressJS) 应用程序的代理服务器。无需对 nginx 进行任何配置并为容器发布端口 80,我就能看到默认的 nginx 登录页面。所以我知道这很有效。

现在我可以挂载包含 proxy_pass localhost:3000 配置的启用站点的目录。我的 Node 应用程序在本地运行(不在任何 Docker 容器中),我可以通过端口 3000(即 localhost:3000)访问它。但是,我假设在运行 nginx 容器、映射到端口 80 并代理我的 localhost:3000 的情况下,我将能够看到我的 very 简单(hello world)应用程序。相反,我收到了 502。

我需要向 docker 传递一些东西吗?这可能是 nginx 配置错误吗?这是我的 nginx 配置:

server {
listen 0.0.0.0:80;
server_name localhost;

location / {
proxy_pass http://localhost:3000;
}
}

我尝试过使用 this question但这似乎没有帮助。除非我做错了什么。

最佳答案

如果您使用 docker-for-mac 18.03 或更高版本,它会自动创建一个特殊的 DNS 条目 host.docker.internal 动态绑定(bind)到主机 inet ip。然后,您可以使用 dns 名称从容器内部代理在主机上运行的服务,作为 localhost 的替身。

即一个 nginx 配置文件:

server {
listen 0.0.0.0:80;
server_name localhost;

location / {
proxy_pass http://host.docker.internal:3000;
}
}

关于node.js - 如何使用 nginx docker 容器访问 localhost 上的服务器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27810076/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com