【问题标题】:nginx with docker not proxying onto my ruby application correctly带有 docker 的 nginx 没有正确代理到我的 ruby​​ 应用程序
【发布时间】:2015-08-03 23:20:34
【问题描述】:

我有两个 Docker 容器(由自定义配置的 nginx 和 Ruby 映像构建),当我运行容器并发出请求时,它们似乎将请求代理到正确的位置(但其中一项被代理的服务没有' t 非常正确地处理请求)。

即当我尝试代理到我的 Ruby 容器时,我要么收到“Sinatra 无法识别此小曲”错误,要么收到“301 重定向”?

注意:代码也可以在这里找到https://github.com/Integralist/Docker-Examples/tree/master/Nginx

下面是 nginx 的 Dockerfile:

FROM ubuntu

# install nginx
RUN apt-get update && apt-get install -y nginx
RUN rm -rf /etc/nginx/sites-enabled/default

# forward request and error logs to docker log collector
RUN ln -sf /dev/stdout /var/log/nginx/access.log
RUN ln -sf /dev/stderr /var/log/nginx/error.log

EXPOSE 80 443
CMD ["nginx", "-g", "daemon off;"]

以下是 Ruby 应用程序的 Dockerfile:

FROM ruby:2.1-onbuild
CMD ["ruby", "app.rb"]

注意:
有人问我的app.rb 和其他依赖项是如何加载的 (因为我的docker run 没有安装它们,Dockerfile 也没有 出现以添加它们)。如果您查看带有 ruby​​ “onbuild” 标记的图像版本,您会看到 COPY 为我们提供的所有这些文件 https://github.com/docker-library/ruby/2.0/onbuild/Dockerfile

Ruby 应用程序如下所示:

require "sinatra"

set :bind, "0.0.0.0"

get "/" do
  "Hello World"
end

nginx.conf 文件看起来像:

user nobody nogroup;
worker_processes auto;          # auto-detect number of logical CPU cores

events {
  worker_connections 512;       # set the max number of simultaneous connections (per worker process)
}

http {
  upstream app {
    server app:4567;            # app is automatically defined inside /etc/hosts by Docker
  }

  server {
    listen *:80;                # Listen for incoming connections from any interface on port 80
    server_name "";             # Don't worry if "Host" HTTP Header is empty or not set
    root /usr/share/nginx/html; # serve static files from here

    location /app/ {            # catch any requests that start with /app/
      proxy_pass http://app;    # proxy requests onto our app server (i.e. a different container)
    }
  }
}

我像这样运行 Ruby 容器:

docker run --name ruby-app -p 4567:4567 -d my-ruby-app

我像这样运行 nginx 容器:

docker run --name nginx-container \
  -v $(pwd)/html:/usr/share/nginx/html:ro \
  -v $(pwd)/docker-nginx/nginx.conf:/etc/nginx/nginx.conf:ro \
  --link ruby-app:app \
  -P -d my-nginx

如果我运行curl http://$(boot2docker ip):32785/app/,我会返回“Sinatra 不知道这个小曲”错误;如果我运行curl http://$(boot2docker ip):32785/app 我会返回301 Moved Permanently 消息?

我确定我遗漏了一些非常明显的东西(也许 Sinatra 是如何配置的?比如我需要设置一个/app 路由吗?或者我应该在nginx.conf 中使用alias 指令吗? )

任何帮助表示赞赏。

【问题讨论】:

  • 您没有注意到您是如何在 ruby​​ 容器中获取代码的?如果你访问 curl http://$(boot2docker ip):4567/ 你会得到什么?
  • @Dirk 有趣的是,我回来了Hello World% !?虽然我实际上不知道这是怎么可能的,因为当我调用docker run 时我没有将app.rb 文件安装到容器中,我也没有在构建时将它烘焙到图像中(忘记我的愚蠢错误知道但是,容器如何运行该脚本并发送回“Hello World”?)
  • @Dirk 好的,所以这个工作的原因是因为我是从 ruby:2.1-onbuild 构建的,它专门将那些文件(Gemfile、Gemfile.lock 和 app.rb)复制到构建的图像中

标签: ruby nginx docker reverse-proxy


【解决方案1】:

因此,我认为您正在设置的内容和正在测试的内容存在一些基本问题。

首先你在 nginx 中说,“当你得到 /app/ 时,将它代理到运行 sinatra 的容器”:

location /app/ {            # catch any requests that start with /app/
  proxy_pass http://app;    # proxy requests onto our app server (i.e. a different container)
}

所以这将被传递给 sinatra,请求 URI 为 /app/

由于您的 sinatra 应用程序没有定义该路线,这就是您收到“Sinatra 不知道这个小曲”的原因。

您在尝试不带斜杠时得到 301 的原因是因为 sinatra 会自动重定向任何不带斜杠的内容。

正如 Dirk 提到的,您在点击时收到“Hello world”的原因:

curl http://$(boot2docker ip):4567/

是因为该路由是在您的 sinatra 路由中定义的,如果您点击:

curl http://$(boot2docker ip):4567

那么你应该得到一个 301 重定向到 /。

要解决您的问题,您需要:

  1. 将您在 sinatra 中的路线更改为“/app”。
  2. 将您的 nginx 位置更改为 / 而不是 /app/。

选项 2 将是最灵活的,因为对 nginx 容器的任何请求都将直接映射到 sinatra 容器,因为您添加了另一个路由,您需要再次更新 nginx 配置以添加此路由以进行代理sinatra 容器。

【讨论】:

    【解决方案2】:

    所以看起来答案是 nginx 位置块如何工作的一个微妙之处......

    如果你没有在上游名称的末尾加上斜杠 /,那么你会发现 nginx 将请求传递为 /app/ 而不仅仅是 /

    在上游名称后面加上 / 意味着它的行为更像 alias 指令。

    所以,这行得通...

    location /app/ {
      proxy_pass http://app/; # this is what I want
    }
    

    但这不起作用...

    location /app/ {
      proxy_pass http://app; # this ISN'T what I want
    }
    

    【讨论】:

      猜你喜欢
      • 2021-05-15
      • 1970-01-01
      • 1970-01-01
      • 2016-04-26
      • 1970-01-01
      • 2018-12-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多