Rails Subdomain Caching

Discussion in 'Ruby/Rails' started by ideaoforder, May 21, 2009.

  1. ideaoforder

    ideaoforder New Member

    Hello,

    I'm running a Rails app that uses subdomains for accounts--so each subdomain needs its own cache. I have Rails setup to cache like so:

    http://subdomain.myapp.com/ -> /public/cache/subdomain/

    How do I setup Litespeed to serve these cached pages?

    In nginx, this would look something like:

    if (-f $document_root/cache/$host/$uri/index.html) {
    rewrite .? /cache/$host/$uri/index.html break;
    }

    if (-f $document_root/cache/$host/$uri.html) {
    rewrite .? /cache/$host/$uri.html break;
    }

    if (-f $document_root/cache/$host/$uri) {
    rewrite .? /cache/$host/$uri break;
    }

    How would this look in Litespeed? And where would I go to change this setting?

    Thanks!
  2. mistwang

    mistwang LiteSpeed Staff

    Just use the rewrite rule you would use for Apache.


    extract subdomain from %{HTTP_HOST} with rewrite condition, then use %1, back reference to subdoamin as part of the destination url.
  3. ideaoforder

    ideaoforder New Member

    Apologies

    Sorry--I don't entirely follow (my fault, not yours)--can you be more explicit here? What settings do I need to adjust in Litespeed to make this work? Can you give me an example of a rewrite rule?

    I have set Enable Cache to yes.

    I am using the following rewrite rules:

    RewriteRule ^/$ /cache/%{HTTP_HOST}/index.html [QSA]
    RewriteRule ^([^.]+)$ /cache/%{HTTP_HOST}$1.html [QSA]

    (I actually just switched to using the entire domain as the folder, i.e. /cache/full.domain.com/CACHED_STUFF)

    Will those rules work?

    What else do I need to do?

    Does caching work in the free edition?

    Again, the rails app is producing the proper cached pages, so things should be working on that end.

    Thanks in advance.
  4. mistwang

    mistwang LiteSpeed Staff

    If you use those rule at vhost level, rules look correct. if you use those rule at context level, you need to get rid of the leading "/" as RewriteBase has been removed from URL before matching the Rule.

    Enable rewrite log and check the error.log if rules does not work as expected.

    Rails page cache works with the free edition.
  5. ideaoforder

    ideaoforder New Member

    Cache Stylesheets and Images

    Okay--it looks like this is working fine for pages. I'd also like to cache stylesheets, js, and images on a per subdomain basis. Each element is processed by the Rails app right now, but I'd like it to be cached. So:

    http://my.domain.com/stylesheets/style.css > cache/my.domain.com/stylesheets/style.css

    Is this possible? What do I need to change?

    Thanks!
  6. mistwang

    mistwang LiteSpeed Staff

    You need to add a new rule like
    RewriteRule ^([^.]+\.css)$ /cache/%{HTTP_HOST}/$1
  7. ideaoforder

    ideaoforder New Member

    Caching hierarchy

    So I've gotten this working perfectly--so far. Is there a way to specify a cache hierarchy? I would like the system to check the subdomain folder first, then the root folder if the asset isn't in the subdomain folder. Thanks!
  8. NiteWave

    NiteWave Administrator

    Sounds this is what you want?

    http://httpd.apache.org/docs/2.0/misc/rewriteguide.html
    The example in above URL explain the detail.

Share This Page