如何使用 nginx docker 容器访问本地主机上的服务器? [英] How do I access a server on localhost with nginx docker container?
问题描述
我正在尝试使用 nginx 的 dockerized 版本作为我的节点 (ExpressJS) 应用程序的代理服务器.无需对 nginx 进行任何配置并为容器发布端口 80,我就可以看到默认的 nginx 登陆页面.所以我知道很多都在起作用.
I'm trying to use a dockerized version of nginx as a proxy server for my node (ExpressJS) application. Without any configuration to nginx and publishing port 80 for the container, I am able to see the default nginx landing page. So I know that much is working.
现在我可以挂载包含 proxy_pass localhost:3000
配置的启用站点的目录.我的节点应用程序在本地运行(不在任何 Docker 容器中),我可以通过端口 3000(即 localhost:3000
)访问它.但是,我假设在 nginx 容器运行、映射到端口 80 并代理我的 localhost:3000 的情况下,我将能够看到我的非常简单(hello world)应用程序.相反,我收到了 502.
Now I can mount my sites-enabled directory that contains the configuration for proxy_pass localhost:3000
. I have my node application running locally (not in any Docker container) and I can access it via port 3000 (i.e. localhost:3000
). However, I would assume that with nginx container running, mapped to port 80, and proxying my localhost:3000, that I would be able to see my very simple (hello world) application. Instead I receive a 502.
我需要向 docker 传递一些东西吗?这可能是 nginx 配置错误吗?这是我的 nginx 配置:
Do I need to pass something into docker? Is this likely a nginx configuration error? Here is my nginx configuration:
server {
listen 0.0.0.0:80;
server_name localhost;
location / {
proxy_pass http://localhost:3000;
}
}
我尝试使用 这个问题 但它似乎没有帮助.除非我做错了什么.
I have tried using this question but it did not seem to help. That is unless I'm doing something completely wrong.
推荐答案
您可以获取如图所示的当前 IP 地址 此处:
You can get your current IP address as shown here:
ifconfig en0 | grep inet | grep -v inet6 | awk '{print $2}'
然后你可以在 docker run
中使用 --add-host
标志:
Then you can use the --add-host
flag with docker run
:
docker run --add-host localnode:$(ifconfig en0 | grep inet | grep -v inet6 | awk '{print $2}') ...
在您的 proxypass
中使用 localnode
而不是 localhost
.
In your proxypass
use localnode
instead of localhost
.
这篇关于如何使用 nginx docker 容器访问本地主机上的服务器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!