【问题标题】:run nginx in docker with another application使用另一个应用程序在 docker 中运行 nginx
【发布时间】:2016-01-28 10:05:50
【问题描述】:

我的应用程序需要在端口 :5000 上提供服务

这是我的 dockerfile

FROM ubuntu:14.04
RUN apt-get update
RUN apt-get install -y nginx
ADD ./nginx.conf /etc/nginx/sites-available/default
RUN service nginx restart

RUN go get github.com/a/mycmd

EXPOSE 5000

然后我跑

sudo docker run --publish 5000:5000 --rm app /go/bin/mycmd

这是我的 nginx 配置文件:

limit_req_zone $binary_remote_addr zone=limit:10m rate=2r/s;

server {
    listen 80;

    set_real_ip_from 0.0.0.0/0;
    real_ip_header X-Forwarded-For;
    real_ip_recursive on;
    server_name 123.13.13.13 example.com;

    location / {
        proxy_read_timeout 3000s;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $remote_addr;
        proxy_pass http://127.0.0.1:5000;
        limit_req zone=limit burst=5 nodelay;
    }
}

然后我希望这会将流量从网络服务器(端口80)重定向到我的应用程序端口5000,但似乎nginx 没有正确执行反向代理。流量不会定向到我的应用。

如何在同一个容器中设置 nginx 和我的应用程序,以便可以将其用作反向代理?

谢谢!

【问题讨论】:

  • 我无法从它目前的形式中读到太多。你能提供确切的 Dockerfile 和docker run 命令吗?
  • 还有更重要的nginx配置文件
  • 刚刚添加了更多信息。谢谢大家!

标签: linux nginx docker


【解决方案1】:

你在 nginx 配置中提到:

proxy_pass http://127.0.0.1:8000;

但您想重定向到 EXPOSE 的端口 5000。

proxy_pass http://127.0.0.1:5000;

如果是拼写错误,请确保将 80 发布到主机端口(而不是直接使用容器 IP 地址。

不要忘记,如果您使用的是虚拟机,您可能必须将已发布的端口转发到端口:请参阅“Connect to a Service running inside a docker container from outside

【讨论】:

  • 对不起,这是我的错字。 :(
  • @cvxv31431asdas 好的,我已经编辑了答案。您是直接在 Linux 主机中使用 docker,还是通过 Windows 或 Mac 上的 VM?
  • 我正在使用谷歌云虚拟机。将端口 80 重定向到 5000。现在可以使用。以为我不需要那个。很高兴知道!谢谢
猜你喜欢
  • 2022-01-18
  • 1970-01-01
  • 2019-01-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多