【问题标题】:Nginx proxy issueNginx 代理问题
【发布时间】:2017-04-06 21:07:48
【问题描述】:

我在服务器上运行 2 个容器。它们都是带有默认 nginx 镜像的 docker 容器。

我正在尝试使用 Container1 作为 Container2 的反向代理。

Container1 的 IP 地址为 172.17.0.3 Container2 在 IP 地址172.17.0.4

当我 curl Container1 时,我得到了默认的 Nginx 主页。 我已经编辑了容器 2 的默认主页,因此它只是 <p> HI </p> 由 ip 上的 curl 验证。

在我的服务器 etc/hosts 文件中,我添加了这一行

172.17.0.3    testapp.net

我的 Container1 的/etc/nginx/conf.d/default.conf 是这个

server {
    listen       80;
    server_name  localhost;
location / {
    root   /usr/share/nginx/html;
    index  index.html index.htm;
}

#error_page  404              /404.html;

# redirect server error pages to the static page /50x.html
#
error_page   500 502 503 504  /50x.html;
location = /50x.html {
    root   /usr/share/nginx/html;
}

}

server {
    listen 80;
    server_name testapp.net;

   location / {
         proxy_pass http://172.17.0.4

   }

}

当我执行curl testapp.net 时,我得到了 Container1 的 nginx(基本的 hello nginx html 文件)的主页,并且没有定向到 Container2。为什么会这样?

【问题讨论】:

  • 在尝试之前是否重新加载了 nginx 配置?
  • 是的,我确实重新加载了配置
  • 你是如何修改hosts文件的?在 docker run 或 build 期间?
  • /etc/hosts 文件在我的服务器上,我在创建 2 个容器后对其进行了修改

标签: nginx reverse-proxy


【解决方案1】:

所以您的问题是您只是修改了服务器上的 /etc/hosts 文件,而不是 docker 容器中的文件。容器有自己的文件系统和自己的网络,这就是你的修改不起作用的原因。

解决方案是在 docker run 期间修改容器上的 /etc/hosts。

docker run --add-host testapp.net:172.17.0.3 your_image

【讨论】:

  • 当我这样做时,我得到 docker:` 来自守护进程的错误响应:oci 运行时错误:container_linux.go:247:启动容器进程导致“exec:\”--add-host\”: $PATH 中找不到可执行文件。`
  • @pyjg 你的 docker 版本是多少?
  • Docker 版本 17.03.0-ce,
  • 我的 dokcerfile 只是来自 nginx:latest
  • @pyjg 抱歉,命令行顺序不正确。我已经修改了我的命令,你可以再试一次吗?
猜你喜欢
  • 1970-01-01
  • 2020-05-20
  • 1970-01-01
  • 2014-10-27
  • 2016-02-12
  • 2020-04-26
  • 1970-01-01
  • 1970-01-01
  • 2015-08-19
相关资源
最近更新 更多