【问题标题】: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 中
^(.*)\/$
如果这不起作用,匹配所有非斜线字符,然后匹配一个斜线字符
^([^\/]*)\/$
【解决方案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)