Showing posts with label Gzip. Show all posts
Showing posts with label Gzip. Show all posts

Saturday, December 27, 2008

添加deflate gzip模块到apache2服务器

$> ./configure --prefix=/usr/local/apache2 --enable-so --enable-mods-shared=all --enable-modules=all
Configuring Apache Portable Runtime Utility library...

checking for APR-util... yes
configure: error: Cannot use an external APR-util with the bundled APR

如果编译过程中发现APR-util的错误,可以加上--with-included-apr这个参数,如下:
$> ./configure --prefix=/usr/local/apache2 --enable-so --enable-mods-shared=all --enable-modules=all --with-included-apr
$> make
$> cp ./modules/filters/.libs/mod_deflate.so /usr/local/apache2/modules
$> vi /usr/local/apache2/conf/httpd.conf
# append 2 line to httpd.conf
LoadModule deflate_module modules/mod_deflate.so
AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css text/javascript application/x-json application/x-javascript
$> /usr/local/apache2/bin/apachectl graceful

更多关于deflate module的设置可参考官方说明:http://httpd.apache.org/docs/2.0/mod/mod_deflate.html

Reference:
http://yuweijun.blogspot.com/2007/09/apache22module.html

Friday, August 24, 2007

Rails output compress plugin

source code from: http://blog.craz8.com/files/compress.rb


require 'stringio'
require 'zlib'

class OutputCompressionFilter

def self.filter(controller)
return if controller.response.headers['Content-Encoding'] || controller.request.env['HTTP_ACCEPT_ENCODING'].nil?
begin
controller.request.env['HTTP_ACCEPT_ENCODING'].split(/\s*,\s*/).each do |encoding|
# TODO: use "q" values to determine user agent encoding preferences
case encoding
when /\Agzip\b/
StringIO.open('', 'w') do |strio|
begin
gz = Zlib::GzipWriter.new(strio)
gz.write(controller.response.body)
controller.response.body = strio.string
ensure
gz.close if gz
end
end
when /\Adeflate\b/
controller.response.body = Zlib::Deflate.deflate(controller.response.body, Zlib::BEST_COMPRESSION)
when /\Aidentity\b/
# do nothing for identity
else
next # the encoding is not supported, try the next one
end
controller.logger.info "Response body was encoded with #{encoding}"
controller.response.headers['Content-Encoding'] = encoding
break # the encoding is supported, stop
end
end
controller.response.headers['Content-Length'] = controller.response.body.length
if controller.response.headers['Vary'] != '*'
controller.response.headers['Vary'] =
controller.response.headers['Vary'].to_s.split(',').push('Accept-Encoding').uniq.join(',')
end
end

end