How to set the Expires header with Apache 2 and mod_expires
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.
Paperclip plugin + Phusion Passenger + path problem
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.
How to parse Apache logs with Ruby
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
How to hide X-Powered-By and Server headers
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…
Configuring Apache to be a forward proxy
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]