How to set the Expires header with Apache 2 and mod_expires

Apache posted 5 months ago by christian

First you need to enable the mod_expires module:

   1  a2enmod expires

Next add this to your configuration:

   1  ExpiresActive On
   2  # Set Expires header to current time by default
   3  ExpiresDefault A0
   4  
   5  <FilesMatch "\.(flv|ico|pdf|avi|mov|ppt|doc|mp3|wmv|wav)$">
   6    ExpiresDefault "access plus 30 days"
   7  </FilesMatch>
   8  
   9  <FilesMatch "\.(jpg|jpeg|png|gif|swf|bmp|)$">
  10    ExpiresDefault "access plus 7 days"
  11  </FilesMatch>
  12  
  13  <FilesMatch "\.(txt|xml|js|css)$">
  14    ExpiresDefault "access plus 1 day"
  15  </FilesMatch>

Now restart Apache:

   1  $ sudo /etc/init.d/apache2 force-reload

Check that the proper headers are set with Firebug, Yahoo YSlow or Google Page speed.

Tagged apache, expires, mod_expires, header, caching

Paperclip plugin + Phusion Passenger + path problem

Ruby posted 9 months ago by christian

In config/initializers/paperclip.rb put:

   1  if RAILS_ENV == "development"
   2    Paperclip.options[:command_path] = '/opt/local/bin/'
   3  else
   4    Paperclip.options[:command_path] = '/usr/bin/'
   5  end

Note image_magick_path is deprecated alias of command_path.

Tagged paperclip, apache, phusion, passenger

How to parse Apache logs with Ruby

Ruby posted 11 months ago by christian

Only supports the combined format at the moment…

   1  class ApacheLog
   2    FORMATS = {
   3      :combined => %r{^(\S+) - - \[(\S+ \+\d{4})\] "(\S+ \S+ [^"]+)" (\d{3}) (\d+|-) "(.*?)" "([^"]+)"$}
   4    }
   5    
   6    class << self
   7      def each_line(log_file, log_format = FORMATS[:combined])
   8  
   9        f = File.open(log_file, "r")
  10  
  11        f.each_line do|line|
  12          data = line.scan(log_format).flatten
  13  
  14          if data.empty?
  15            p "Line didn't match pattern: #{line}"
  16  
  17            next
  18          end
  19  
  20          yield data
  21        end
  22      end
  23    end
  24  end
  25  
  26  
  27  log_file   = ARGV[0]
  28  
  29  ApacheLog.each_line(log_file) do |data|
  30    host, date, url_with_method, status, size, referrer, agent = data
  31  end

Tagged apache, statistics, logs, ruby, combined, format

How to hide X-Powered-By and Server headers

Apache posted 11 months ago by christian

First enable the mod_headers module:

   1  sudo a2enmod headers

Then add this to your apache2.conf:

   1  # Hide X-Powered-By and Server headers
   2  Header always unset "X-Powered-By"
   3  ServerTokens Prod
   4  ServerSignature Off

Now restart Apache:

   1  /etc/init.d/apache2 force-reload

This is security through obscurity at it’s finest…

Tagged servertokens, server, x-powered-by, mod_rails, passenger, apache, apache2, headers

Configuring Apache to be a forward proxy

Apache posted about 1 year ago by christian

This configuration makes Apache act as an HTTP proxy:

   1  <VirtualHost *:8080>
   2  ProxyRequests On
   3  ProxyVia On
   4  #ProxyRemote * http://...:8080 Uncomment to route requests through another proxy
   5  <Proxy *>
   6  	Order deny,allow
   7  	Deny from all
   8  	Allow from all # Not a good idea, set to allowed IP ranges
   9  </Proxy> 
  10  	
  11  CacheRoot "/tmp"
  12  CacheMaxExpire 24
  13  CacheLastModifiedFactor 0.1
  14  CacheDefaultExpire 1
  15  
  16  ServerName my-proxy
  17  
  18  ErrorLog "/var/log/apache2/proxy-error.log"
  19  CustomLog "/var/log/apache2/proxy-access.log" common
  20  </VirtualHost>

Also read this.

Tips

You can use mod_rewrite to rewrite requests. To rewrite root (/) to /temporary_outage you could use the following rewrite:

   1  RewriteCond %{HTTP_HOST} ^(www\.)?xxx\.com 
   2  RewriteRule /$ http://%{HTTP_HOST}/temporary_outage/ [P,L]

Tagged apache, forward, proxy, rewrite