Hi all, I setup lighttpd today to handle the compression of static html/css/ javascript. I have been quite pleased with the results and would like to try compressing the html generated by rails. According to lighttpd docs, lighttp wants the app to compress generated content, like "zlib.output_compression = 1" in php.ini. Is this supported in rails? Has anyone configured lighttpd to cache rails files? Thanks! --Dave.
On Jul 10, 2005, at 5:00 AM, David Teare wrote:> I setup lighttpd today to handle the compression of static html/css/ > javascript. I have been quite pleased with the results and would > like to try compressing the html generated by rails. According to > lighttpd docs, lighttp wants the app to compress generated content, > like "zlib.output_compression = 1" in php.ini. > > Is this supported in rails? Has anyone configured lighttpd to > cache rails files?A php.ini ofcourse won''t be of much effect. If you want compression in Lighttpd, you''ll need to do it yourself from an after_filter. If you''re after caching, you might want to take a look at http:// www.lighttpd.net/documentation/cml.html. You could probably tweak that to serve Rails'' cache files, even if the page consists (fully) of piecewise cached blocks. But I must admit that this is still on my ''try-this-out'' list, so consider it untested. - Marten
Thanks for the hint! Once I googled for after_filter & compression, it all made sense. Also, for those interested in the solution, you can look in the Rails book. They have a complete example there. On 10-Jul-05, at 8:14 AM, Marten Veldthuis wrote:> If you want compression in Lighttpd, you''ll need to do it yourself > from an after_filter._______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
On 7/10/05, David Teare <dteare-LXYvB7aEQDJCkLs28/y7ANBPR1lH4CV8@public.gmane.org> wrote:> Thanks for the hint! Once I googled for after_filter & compression, it all > made sense. > > Also, for those interested in the solution, you can look in the Rails book. > They have a complete example there.Have you had any luck with it? I tried it, and it''s working great. I checked the headers and spit out the file size difference to be sure. Good find! -- rick http://techno-weenie.net
> Have you had any luck with it? I tried it, and it''s working great. I > checked the headers and spit out the file size difference to be sure. > > Good find!I say that, and then I turn on caches_page and find an issue. It saves the gzipped output to the file. Not a big deal, I just excluded that action from the after_filter. Lighttpd handles the caching of that anyhow. -- rick http://techno-weenie.net
Would you mind posting your after_filter (maybe to the REUSE sublist)?
I am using the after_filter from the Rails book:
def compress
accepts = request.env[''HTTP_ACCEPT_ENCODING'']
return unless accepts && accepts =~ /(x-gzip|gzip)/
encoding = $1
output = StringIO.new
def output.close # Zlib does a close. Bad Zlib...
rewind
end
gz = Zlib::GzipWriter.new(output)
gz.write(response.body)
gz.close
if output.length < response.body.length
response.body = output.string
response.headers[''Content-encoding''] = encoding
end
end
However I would like to encapsulate this method in a separate class
following the advice in the docs at
http://ap.rubyonrails.com/classes/ActionController/Filters/
ClassMethods.html
I tried a class like this:
class OutputCompressionFilter
def self.filter(controller)
self.compress(controller)
end
end
With a method compress:
def self.compress controller
accepts = controller.request.env[''HTTP_ACCEPT_ENCODING'']
return unless accepts && accepts =~ /(x-gzip|gzip)/
encoding = $2
output = StringIO.new
def output.close # Zlib does a close. Bad Zlib...
rewind
end
gz = Zlib::GzipWriter.new(output)
gz.write(controller.response.body)
gz.close
if output.length < controller.response.body.length
controller.response.body = output.string
controller.response.headers[''Content-encoding''] =
encoding
end
end
Which compresses the output but screws up the encoding. Where is the
compress method from the book getting that first param $1, and (more
importantly) is there a better way to do this?
On Jul 11, 2005, at 1:27 AM, Rick Olson wrote:
>> Have you had any luck with it? I tried it, and it''s working
great. I
>> checked the headers and spit out the file size difference to be sure.
>>
>> Good find!
>
> I say that, and then I turn on caches_page and find an issue. It
> saves the gzipped output to the file. Not a big deal, I just excluded
> that action from the after_filter. Lighttpd handles the caching of
> that anyhow.
>
>
>
> --
> rick
> http://techno-weenie.net
> _______________________________________________
> Rails mailing list
> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
> http://lists.rubyonrails.org/mailman/listinfo/rails
>
I had a similar problem with caches_page and had to avoid caching
altogether s.t. my cached pages wouldn''t break browsers that
don''t
support compression.
What I am currently doing is placing the compression filter in
application.rb so it is applied to all pages, and then selectively
disable it, like this:
after_filter :compress, :except=>[:abc]
But this seems to be a hack to me -- all controller methods named
"abc" will be excluded, which would be bad for "index", as I
might
want some compressed, some not.
Is there a better way to do this? I was thinking something like this
in application.rb:
after_filter :compress, :avoid_cached_pages
This would tell Rails never to use this filter on cached pages.
Thoughts?
--Dave.
On 11-Jul-05, at 12:27 AM, Rick Olson wrote:
>> Have you had any luck with it? I tried it, and it''s working
>> great. I
>> checked the headers and spit out the file size difference to be sure.
>>
>> Good find!
>>
>
> I say that, and then I turn on caches_page and find an issue. It
> saves the gzipped output to the file. Not a big deal, I just excluded
> that action from the after_filter. Lighttpd handles the caching of
> that anyhow.
>
>
>
> --
> rick
> http://techno-weenie.net
> _______________________________________________
> Rails mailing list
> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
> http://lists.rubyonrails.org/mailman/listinfo/rails
>
On Monday 11 July 2005 06:32, Ben Jackson wrote:> Would you mind posting your after_filter (maybe to the REUSE > sublist)? > > I am using the after_filter from the Rails book: > > def compress > accepts = request.env[''HTTP_ACCEPT_ENCODING''] > return unless accepts && accepts =~ /(x-gzip|gzip)/ > encoding = $1 > output = StringIO.new > def output.close # Zlib does a close. Bad Zlib... > rewind > end > gz = Zlib::GzipWriter.new(output) > gz.write(response.body) > gz.close > if output.length < response.body.length > response.body = output.string > response.headers[''Content-encoding''] = encoding > end > end > > However I would like to encapsulate this method in a separate class > following the advice in the docs at > http://ap.rubyonrails.com/classes/ActionController/Filters/ > ClassMethods.html > > I tried a class like this: > > class OutputCompressionFilter > def self.filter(controller) > self.compress(controller) > end > end > > With a method compress: > > def self.compress controller > accepts = controller.request.env[''HTTP_ACCEPT_ENCODING''] > return unless accepts && accepts =~ /(x-gzip|gzip)/ > encoding = $2 > output = StringIO.new > def output.close # Zlib does a close. Bad Zlib... > rewind > end > gz = Zlib::GzipWriter.new(output) > gz.write(controller.response.body) > gz.close > if output.length < controller.response.body.length > controller.response.body = output.string > controller.response.headers[''Content-encoding''] = encoding > end > end > > Which compresses the output but screws up the encoding. Where is the > compress method from the book getting that first param $1, and (more > importantly) is there a better way to do this?From the regexp above. irb(main):007:0> accepts = "Encoding-Header: gzip" => "Encoding-Header: gzip" irb(main):008:0> accepts =~ /(x-gzip|gzip)/ => 17 irb(main):009:0> $1 => "gzip"> On Jul 11, 2005, at 1:27 AM, Rick Olson wrote: > >> Have you had any luck with it? I tried it, and it''s working > >> great. I checked the headers and spit out the file size > >> difference to be sure. > >> > >> Good find! > > > > I say that, and then I turn on caches_page and find an issue. It > > saves the gzipped output to the file. Not a big deal, I just > > excluded that action from the after_filter. Lighttpd handles the > > caching of that anyhow. > > > > > > > > -- > > rick > > http://techno-weenie.net > > _______________________________________________ > > Rails mailing list > > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > > http://lists.rubyonrails.org/mailman/listinfo/rails > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails-- Dominic Marks
> I had a similar problem with caches_page and had to avoid caching > altogether s.t. my cached pages wouldn''t break browsers that don''t > support compression. > > What I am currently doing is placing the compression filter in > application.rb so it is applied to all pages, and then selectively > disable it, like this: > > after_filter :compress, :except=>[:abc] > > But this seems to be a hack to me -- all controller methods named > "abc" will be excluded, which would be bad for "index", as I might > want some compressed, some not. > > Is there a better way to do this? I was thinking something like this > in application.rb: > > after_filter :compress, :avoid_cached_pages > > This would tell Rails never to use this filter on cached pages. > > Thoughts?I ended up throwing the after_filter on all my controllers, and excluding the one action that caches_page currently. Honestly, I think this should be done invisibly by the controller. Of course, it''d have to work fine with sending files and cached pages. Also, it should be disabled by default. I''ll see if I can work up a patch to do that, unless someone has one in the works? -- rick http://techno-weenie.net