根据 rails 服务器响应使用 nginx 提供静态 html 页面

Serve static html page with nginx based on rails server response

我在 /var/files 中有 2 个 html 文件:host1.html 和 host2.html。 我希望 nginx 根据 rails 响应为它们中的任何一个提供服务(作为索引文件),例如 http://localhost - 应该呈现 host1.html 或 host2.html.

这是Rails控制器:

class HomeController < ApplicationController
  def index
    response.headers['X-Accel-Redirect'] = 'host1.html'
    head :ok
  end
end

和 nginx 配置:

upstream app_server {
  server 0.0.0.0:3000 fail_timeout=0;
}

server {
  listen 8080 default;

  location /{
    proxy_pass http://app_server;
    internal;
    root /var/files/;
  }
}

这不起作用。

如果删除内部 - 请求代理到 Rails,但没有发生内部重定向...

此外,我不仅要提供 html 个文件——实际上是任何文件。

请指教。 谢谢。

我会用这样的东西

nginx 配置:

upstream app_server {
  server 0.0.0.0:3000 fail_timeout=0;
}

server {
  listen 8080 default;

  location / {
    proxy_pass http://app_server;
  }

  location /internal/ {
    internal;
    alias /var/files/;
  }
}

Rails 控制器:

class HomeController < ApplicationController
  def index
    response.headers['X-Accel-Redirect'] = '/internal/host1.html'
    head :ok
  end
end

这里我们定义了“虚拟”文件夹/internal/,它将提供静态文件。当Rails“重定向”到/internal/host1.html时,nginx会将其匹配到location /internal/,将/internal/替换为/var/files/(详见alias)并服务静态文件 /var/files/host1.html。但是,如果用户将他的浏览器指向 /internal/host1.html,由于 internal 指令,他将收到 404 错误。