【问题标题】:Rails cache and Nginx rewriteRails 缓存和 Nginx 重写
【发布时间】:2010-12-16 21:44:22
【问题描述】:

我网站中所有网址的末尾都有一个斜杠:

Url: http://example.com/help/
Rails chache file: public/cache/help.html

这是我的 Nginx 重写规则:

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

它不起作用,因为 $1 带有斜杠...有人可以帮我解决这个正则表达式吗?

谢谢。

【问题讨论】:

    标签: caching mod-rewrite nginx ruby-on-rails-3


    【解决方案1】:

    尝试将斜线移到括号外,这样它就不会包含在 $1 中

    ^(.*)\/$
    

    如果这不起作用,匹配所有非斜线字符,然后匹配一个斜线字符

    ^([^\/]*)\/$
    

    【讨论】:

    • 在两个正则表达式中,$1 都带有斜杠
    【解决方案2】:

    我用这个 hack 解决了问题

    module PageCachingHack
      class << self
        def included(base)
          base.alias_method_chain :page_cache_file, :trailing_slash
        end
      end
    
      private
        def page_cache_file_with_trailing_slash(path)
          name = page_cache_file_without_trailing_slash(path)
          name.gsub!('.html', '/index.html') unless name =~ /index\.html/
          return name
        end
    end
    ActionController::Caching::Pages::ClassMethods.send(:include, PageCachingHack)
    

    【讨论】:

      猜你喜欢
      • 2016-02-03
      • 2016-02-06
      • 1970-01-01
      • 2019-01-18
      • 2017-01-10
      • 2016-09-16
      • 1970-01-01
      • 2013-09-30
      • 2012-08-20
      相关资源
      最近更新 更多