【问题标题】:nginx rewrite rules with Passengernginx用Passenger重写规则
【发布时间】:2010-11-13 18:15:01
【问题描述】:

我正在尝试从 Apache 迁移到 nginx,在这两个实例中都使用Passenger 来托管 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

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

在 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 假设它是一个静态资产,所以不将它传递给乘客。有没有办法阻止这种行为,让它影响应用程序?

【问题讨论】:

    标签: ruby-on-rails ruby apache nginx passenger


    【解决方案1】:

    James,请试试这个配置文件 https://gist.github.com/711913 并注意这个位置配置:

      location ~* \.(png|gif|jpg|jpeg|css|js|swf|ico)(\?[0-9]+)?$ {
          access_log off;
          expires max;
          add_header Cache-Control public;
      }
    

    如果您有正确的权限,乘客不会让 Rails 管理您的资产文件(运行 nginx 的用户应该有权直接访问文件)

    【讨论】:

      【解决方案2】:

      我的 rails 应用程序在 nginx 和乘客上运行。我已将我的 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 看看它是否有效:-)

      【讨论】:

      • 我遇到了同样的问题,而且我是 Nginx 的新手,所以这非常有效。谢谢。
      • nginx.conf 里面的指令是什么?
      猜你喜欢
      • 2010-11-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-04
      • 2013-08-05
      • 2013-10-12
      相关资源
      最近更新 更多