How to use MySQL's ON DUPLICATE to track page views

Apache posted 4 months ago by christian

With MySQL 5 you can track page views with ON DUPLICATE.

The table:

   1  create table page_views(
   2    uri varchar(500) not null,
   3    views int(11) not null default '0',
   4    primary key(uri)
   5  )

The SQL:

   1  insert into page_views (uri, views) values ('/products', 1) on duplicate key update views = views + 1;

See http://dev.mysql.com/doc/refman/5.0/en/insert-on-duplicate.html for more information.

Tagged mysql, duplicate, pageview, analytics

Reading gem version from YAML

Apache posted 4 months ago by christian

From Jekyll:

   1  module YerGem
   2    def self.version
   3      yml = YAML.load(File.read(File.join(File.dirname(__FILE__), *%w[.. VERSION.yml])))
   4      "#{yml[:major]}.#{yml[:minor]}.#{yml[:patch]}"
   5    end
   6  end

Tagged gem, rubygem, version, jeweler

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

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