如何使用nginx docker容器访问localhost上的服务器? [英] How do I access a server on localhost with nginx docker container?

查看:1501
本文介绍了如何使用nginx docker容器访问localhost上的服务器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用一个dockerized版本的nginx作为我的节点(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.

我需要将一些东西传送到码头服务器吗?这可能是一个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.

推荐答案

您可以按照此处

ifconfig en0 | grep inet | grep -v inet6 | awk '{print $2}'

然后你可以使用添加-host 标志与 docker运行

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

这篇关于如何使用nginx docker容器访问localhost上的服务器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆