简体   繁体   English

根据Rails服务器响应使用Nginx服务静态html页面

[英]Serve static html page with nginx based on rails server response

I have 2 html files in /var/files: host1.html and host2.html. 我在/ var / files中有2个html文件:host1.html和host2.html。 I want nginx serving any of them (as index file) depending on rails response, eg http://localhost - should render host1.html or host2.html. 我希望nginx根据rails的响应来提供它们中的任何一个(作为索引文件),例如http:// localhost-应该呈现host1.html或host2.html。

This is Rails controller: 这是Rails控制器:

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

And nginx config: 和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/;
  }
}

This is not working. 这是行不通的。

If remove internal - request proxied to Rails, but no internal redirect occurs... 如果删除内部-请求代理到Rails,但不发生内部重定向...

Also, i want to serve not only html files - any actually. 此外,我不仅要提供html文件-还要实际提供任何文件。

Please advice. 请指教。 Thanks. 谢谢。

I would use something like this 我会用这样的东西

nginx config: 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 controller: Rails控制器:

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

Here we define “virtual” folder /internal/ that will serve static files. 在这里,我们定义了“虚拟”文件夹/internal/ ,该文件夹将提供静态文件。 When Rails “redirects” to /internal/host1.html nginx will match it to location /internal/ , replace /internal/ with /var/files/ (see alias for details) and serve static file /var/files/host1.html . 当Rails“重定向”到/internal/host1.html nginx会将其匹配到location /internal/ ,将/internal/替换为/var/files/ (有关详细信息,请参见别名 )并提供静态文件/var/files/host1.html But if user points his browser to /internal/host1.html he will get 404 error due to internal directive. 但是,如果用户将其浏览器指向/internal/host1.html由于internal指令,他将收到404错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM