node.js 如何使用 nginx docker 容器访问本地主机上的服务器?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27810076/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I access a server on localhost with nginx docker container?
提问by adam-beck
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.
我正在尝试使用 nginx 的 dockerized 版本作为我的节点 (ExpressJS) 应用程序的代理服务器。没有对 nginx 进行任何配置并为容器发布端口 80,我就可以看到默认的 nginx 登陆页面。所以我知道很多事情都在起作用。
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 verysimple (hello world) application. Instead I receive a 502.
现在我可以挂载包含proxy_pass localhost:3000. 我的节点应用程序在本地运行(不在任何 Docker 容器中),我可以通过端口 3000(即localhost:3000)访问它。但是,我假设在运行 nginx 容器、映射到端口 80 并代理我的 localhost:3000 的情况下,我将能够看到我的非常简单的(hello world)应用程序。相反,我收到了 502。
Do I need to pass something into docker? Is this likely a nginx configuration error? Here is my nginx configuration:
我需要将某些东西传递给 docker 吗?这可能是 nginx 配置错误吗?这是我的nginx配置:
server {
listen 0.0.0.0:80;
server_name localhost;
location / {
proxy_pass http://localhost:3000;
}
}
I have tried using this questionbut it did not seem to help. That is unless I'm doing something completely wrong.
我试过使用这个问题,但它似乎没有帮助。除非我做错了什么。
采纳答案by Abdullah Jibaly
You can get your current IP address as shown here:
你可以得到你当前的IP地址,如图这里:
ifconfig en0 | grep inet | grep -v inet6 | awk '{print }'
Then you can use the --add-hostflag with docker run:
然后你可以使用--add-host标志docker run:
docker run --add-host localnode:$(ifconfig en0 | grep inet | grep -v inet6 | awk '{print $2}') ...
In your proxypassuse localnodeinstead of localhost.
在您proxypass使用localnode而不是localhost.
回答by digitaldreamer
If you're using docker-for-mac 18.03 or newer it auto creates a special DNS entry host.docker.internalthat dynamically binds to the host inet ip. You can then use the dns name to proxy services running on the host machine from inside a container as a stand-in for localhost.
如果您使用 docker-for-mac 18.03 或更新版本,它会自动创建一个host.docker.internal动态绑定到主机 inet ip的特殊 DNS 条目。然后,您可以使用 dns 名称从容器内部代理主机上运行的服务,作为localhost.
i.e. an nginx config file:
即一个 nginx 配置文件:
server {
listen 0.0.0.0:80;
server_name localhost;
location / {
proxy_pass http://host.docker.internal:3000;
}
}
回答by Eli
Yes. Docker needs to know about your host machine. You can set an alias to that with the --add-hostswitch. On a *nix box to create an alias to a name "localbox", this would be:
是的。Docker 需要了解您的主机。您可以使用--add-host开关设置别名。在 *nix 框上为名称“localbox”创建别名,这将是:
docker run my_repo/my_image --add-host=localbox:<host_name>`
On boot2docker it would be:
在 boot2docker 上,它将是:
docker run my_repo/my_image --add-host=localbox:192.168.59.3`
where you should replace "192.168.59.3" with whatever boot2docker ipreturns.
您应该用任何boot2docker ip返回值替换“192.168.59.3” 。
Then, you should access your host machine always through the alias localbox, so just change your nginx config to:
然后,您应该始终通过别名 localbox 访问您的主机,因此只需将您的 nginx 配置更改为:
location / {
proxy_pass http://localbox:3000;
}
回答by George Mogilevsky
And finally, if you are using Nginx as a reverse proxy for multiple services, you can spin all of that with docker-compose. Make sure to expose ports “80:80” only on the Nginx service. Other services you can expose only the service port without mapping to the underlying network like so:
最后,如果您使用 Nginx 作为多个服务的反向代理,您可以使用 docker-compose 旋转所有这些。确保仅在 Nginx 服务上公开端口“80:80”。其他服务您可以只公开服务端口而不映射到底层网络,如下所示:
web:
.....
expose:
- 8080
nginx:
.....
port:
- “80:80”
and then use Nginx configuration proxy_pass http://service-name:portYou don't need the upstream app part at all
然后使用 Nginx 配置 proxy_pass http://service-name:port你根本不需要上游应用部分

