ruby-on-rails - nginx 用 Passenger 重写规则

标签 ruby-on-rails ruby apache nginx passenger

我正在尝试在两个实例中使用 Passenger 从 Apache 迁移到 nginx 以托管 Rails 应用程序。该应用程序接受一个请求,这是一个图像 - 如果图像存在于/system/logos/$requestedimage 那么它应该得到服务,或者如果需要它应该被允许点击 Rails 应用程序来生成它(然后它在哪里缓存到/system/logos)。

在 Apache 中,我使用了以下内容:

RewriteCond %{DOCUMENT_ROOT}/system/logos/%{REQUEST_FILENAME} -f
RewriteRule ^/(.*)$ http://assets.clg.eve-metrics.com/system/logos/$1

这很好用。 Assets 。 subdomain 是另一个子域,但具有相同的根,只是禁用了 Passenger,专门设置用于托管静态文件(过期方式)。

在 nginx 中,我使用以下内容:

server {
  listen 80;
  passenger_enabled on;
  server_name  clg.eve-metrics.com www.clg.eve-metrics.com;
  root /opt/www/clg/current/public;
  gzip             on;
  gzip_min_length  1000;
  gzip_proxied     expired no-cache no-store private auth;
  gzip_types       text/plain application/xml text/css application/javascript;
  gzip_disable     msie6;
  error_page   500 502 503 504  /50x.html;
  location = /50x.html {
  }
  if (-f $document_root/system/logos$request_filename) { 
    rewrite ^/(.*)$ http://assets.clg.eve-metrics.com/system/logos/$1 break;
  }
}

这不太好用。事实上,一点也不。它从不重定向到缓存路径,也从不访问 Rails 应用程序。这就像 nginx 假设它是静态 Assets ,所以不将其传递给 Passenger。有没有办法阻止这种行为,使其影响应用程序?

最佳答案

我的 Rails 应用程序在 nginx 和 passenger 上运行。我已将 Rails 缓存目录从默认的 /public 移动到 /public/system/cache/。为了让它工作,我必须将它插入到我的虚拟主机配置文件中:

if (-f $document_root/system/cache/$uri/index.html) {
  rewrite (.*) /system/cache/$1/index.html break;
}

if (-f $document_root/system/cache/$uri.html) {
  rewrite (.*) /system/cache/$1.html break;
}

我记得我也试图让它与 $request_filename 一起工作,但没有让它工作。尝试使用 $uri 看看它是否有效:-)

关于ruby-on-rails - nginx 用 Passenger 重写规则,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1177979/

相关文章:

ruby - gmaps4rails 不显示 map

php - 查询字符串隐藏

ruby-on-rails - AWS Elastic Beanstalk 无法担任角色

ruby-on-rails - libv8 和 rails 的 bundle 安装问题

ruby-on-rails - 创建时间和更新时间另存为明天

ruby-on-rails - 使用 carrierwave 将图像上传到谷歌云存储,文件名最终被保存而不是存储桶中图像的公共(public)链接

Ruby 从 block 中返回的对象获取数组

apache - 从 Apache 配置中删除/var/www/icons 别名

apache - 在 AWS EBS 单实例 (Tomcat) 上安装 SSL 证书

ruby-on-rails - 两个或多个 Controller 共享相同的投票操作(只是模型类不同)——如何创建一个 mixin 以包含在所有 Controller 中?