欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 教育 > 幼教 > nginx: [emerg] host not found in upstream “host.docker.internal“

nginx: [emerg] host not found in upstream “host.docker.internal“

2025/9/25 11:27:46 来源:https://blog.csdn.net/Toormi/article/details/144953153  浏览:    关键词:nginx: [emerg] host not found in upstream “host.docker.internal“

在Docker中使用Nginx时遇到错误 nginx: [emerg] host not found in upstream "host.docker.internal",通常是由于Docker环境不支持 host.docker.internal 这个主机名。以下是解决此问题的几种方法:

1. 检查Docker版本

host.docker.internal 是在Docker Desktop(适用于Windows和Mac)中引入的。如果你在Linux上运行Docker,这个主机名可能不可用。可以通过以下命令检查是否支持:

docker run -it --rm alpine sh
nslookup host.docker.internal

如果返回宿主机的IP地址,则支持;如果失败,则需要其他解决方案[1][3]。

2. 使用宿主机的IP地址

如果 host.docker.internal 不可用,可以直接使用宿主机的IP地址。首先,获取宿主机的IP地址,例如:

ip addr show docker0

然后在Nginx配置中将 proxy_pass 修改为宿主机的IP地址,例如:

proxy_pass http://172.17.0.1:8000;  # 替换为你的宿主机IP和端口

这种方法在Linux环境下有效[5][6]。

3. 使用 --add-host 参数

在启动Docker容器时,可以使用 --add-host 参数将宿主机的IP映射到 host.docker.internal

docker run --add-host=host.docker.internal:host-gateway <image>

这样,容器内就可以使用 host.docker.internal 来访问宿主机[3][4]。

4. 使用Host网络模式

另一种方法是使用Host网络模式,这样容器将与宿主机共享网络堆栈,使得容器内的 localhost 实际上指向宿主机的 localhost。启动容器时使用以下命令:

docker run --network host <image>

这种方式不需要修改Nginx配置,因为容器可以直接访问宿主机上的服务[5][6]。

总结

根据你的Docker环境选择合适的方法来解决Nginx无法解析 host.docker.internal 的问题。对于Linux用户,使用宿主机的IP地址或Host网络模式通常是最有效的解决方案。

Citations:
[1] https://blog.csdn.net/bobo789456123/article/details/130866328
[2] https://ronin-zc.com/posts/docker%E9%83%A8%E7%BD%B2nginx%E5%87%BA%E7%8E%B0host-not-found-in-upstream%E9%97%AE%E9%A2%98%E8%A7%A3%E5%86%B3/
[3] https://blog.csdn.net/sinat_35773915/article/details/135011814
[4] https://www.zhangbj.com/p/1661.html
[5] https://jingsam.github.io/2018/10/16/host-in-docker.html
[6] https://averainy.com/post/nginx-emerg-host-not-found-in-upstream/
[7] https://learnku.com/laravel/t/53903?order_by=created_at

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词