Apache HTTP Server

From bemoko developer wiki

Jump to: navigation, search
Product - Apache HTTP Server
Home: http://httpd.apache.org/  Version: 2.2
Summary: open-source HTTP server

Tomcat Integration

Apache HTTP server can deliver Apache Tomcat responses with the Tomcat Connector.

mod_jk configuration

See http://tomcat.apache.org/connectors-doc/ for details on mod_jk

mod_jk is used to proxy request into Apache HTTP server onto Tomcat server. In Apache configuration, load and configure the mod_jk module, e.g.

LoadModule    jk_module  modules/mod_jk.so
JkWorkersFile /etc/httpd/conf/workers.properties
JkShmFile     /var/log/httpd/mod_jk.shm
JkLogFile     /var/log/httpd/mod_jk.log
JkLogLevel    info
JkLogStampFormat "[%a %b %d %H:%M:%S %Y] "

For JBoss installs, please read http://community.jboss.org/wiki/UsingModjk12WithJBoss

Workers Configuration

Tomcat workers can be configured in the /etc/httpd/conf/workers.properties file to define how Apache should retrieve content from a Tomcat instance, e.g.

# Define workers using ajp13
worker.list=live
# Set properties for live worker
worker.live.type=ajp13
worker.live.host=localhost
worker.live.port=8009

Note that this AJP port should also be configured in your tomcat server.xml, e.g.

<Server port="8005" shutdown="SHUTDOWN">
   ...
   <Service name="Catalina">
     ...
     <Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />
     ...
   </Service>
</Server>

Virtual Host

A virtual host can be configured in Apache to direct all traffic for a given sub-domain to a particular bemoko site by creating a VirtualHost configuration, e.g. the following configuration in the file /etc/httpd/conf.d/1-mysite.conf:

<VirtualHost *:80>
    ServerName mysite.bemoko.com
    ServerAlias mysite.bemoko.com
 
    JkMount /mysite/* live
 
    RewriteEngine On
    RewriteRule     ^/$         /mysite/i [R]
</VirtualHost>

Rewriting URLs

If you need to shorten the URLs or create vanity URLs this can readily be achieved with a combination of RewriteRules in your Apache configuration (to ensure that incoming URLs are processed accordingly) and a URI Engine defined in the bemoko site configuration (to ensure that the site generates the appropriate URLs)

For example say that you currently have a URL of the form

http://mysite.com/live/mysite/myintent

but you want the URL to be

http://mysite.com/myintent

then in your Apache configuration, define like the following:

<VirtualHost *:80>
  # Define the server name for this virtual host
  ServerName mysite.com
 
  JkMount /live/* live
 
  # Switch the rewrite engine on
  RewriteEngine On
 
  # Allow all URLs starting with live through
  RewriteRule ^/live - [L]
 
  # All other URLs proxy through onto the /live/mysite endpoint
  RewriteRule ^/(.*)  /live/mysite/$1 [P]

Now if you want to make your bemoko site generate the appropriate URLs you can create a URI Engine

You can also support vanity URLs with Rewrite rules like

  RewriteRule  ^/user/(.*) /live/mysite/user/id/$1 [P]

This would, for example proxy URLs like http://mysite.com/user/bob onto the user intent of your bemoko site mysite with the user name is passed in as the id argument.

Delivering Static Content

If you want to deliver some content from URLs that start with the same path as the bemoko platform mount, then you can use JkUnMount to exclude this URL from the tomcat connector and allow the URL to be configured directly to the file system. This is ideal for large content that can be more optimally delivered directly through Apache.

For example, the following:

  JkUnMount /mysite/media/video/* live
  JkMount /* live
  Alias /mysite/media/video/ /var/bemoko/media/mysite/transcoded/video/

will configure all URLs starting with http://host/mysite/media/video/ to take content directly from the filesystem at /var/bemoko/media/mysite/transcoded/video/, whilst directing all other traffic the "live" tomcat connection.

If you host many sites on a server which deliver media then you can create a generic Apache rule as follows:

  JkUnMount /*/media/* live
  AliasMatch /(.*)/(.*)/media/(.*) /var/bemoko/media/$1~$2/transcoded/$3
  AliasMatch /(.*)/media/(.*) /var/bemoko/media/$1/transcoded/$2

Note that this will unmount all URLs with "/media/" in so take care that this does not cause conflict with any other server function.

On some *nix systems, if you get access denied for delivering this static content, you may need to set the httpd_user_content_t permissions on the local directory, e.g.

chcon -R -t httpd_user_content_t /var/bemoko/media/

Mime Type Configuration

Mime types are typically configured in the /etc/mime.types. Some mime types that you may need for mobile are sometime missing from this configuration. e.g.

 video/3gpp                      3gp
 video/mp4                       mp4

Example Configuration from bemoko Operations

To give you an idea for a domain configuration file, here's an extract for our configuration for our default bemoko.com domain

/etc/httpd/conf.d/live.conf

LoadModule    jk_module  modules/mod_jk.so
JkWorkersFile /etc/httpd/conf/workers.properties
JkShmFile     /var/log/httpd/mod_jk.shm
JkLogFile     /var/log/httpd/mod_jk.log
JkLogLevel    info
JkLogStampFormat "[%a %b %d %H:%M:%S %Y] "
 
DocumentRoot /opt/local/bemoko/sites/operations/ui/root
 
NameVirtualHost *:80
<VirtualHost *:80>
    ServerName live.bemoko.com
    ServerAlias bemoko.com bemoko.mobi bemoko.co.uk *.bemoko.mobi *.bemoko.com *.bemoko.co.uk
 
    JkMount /bemoko/* live
    JkMount /test/* live
 
    RewriteEngine On
    RewriteRule     ^/$         /bemoko/i [R]
    RewriteRule     ^/t$        /test/i [R]
    RewriteRule     ^/([^\/.]+)$  /$1/ [R]
</VirtualHost>

Note that the rewrite rule at the end rewrites all URLs like,

http://bemoko.com/test

to the appropriate site URL, i.e. http://bemoko.com/test/. This rewriting doesn't take place if there is a "." in the name, so delivers resources like favicon.ico and robots.txt without rewriting.

Other domains are defined in separate configuration files, e.g.

/etc/httpd/conf.d/1-test.conf

<VirtualHost *:80>
    ServerName test.bemoko.com
    JkMount /* live-1_2_7
 
    RewriteEngine On
    RewriteRule     ^/$     /test/i [R]
</VirtualHost>

These domain specific files are named starting with a 1 so that they are loaded before the live.conf which defines the behaviour for wild card domains with *.bemoko.com. i.e. if an explicit domain is not configured then the default domain behaviour takes place.

Logging

Default logging which bemoko uses is:

LogFormat "%h %l %u %t %v \"%r\" %>s %b \"%{auth}C\" %D \"%{Referer}i\" \"%{User-Agent}i\"" bemokolog
CustomLog "|/usr/sbin/rotatelogs /var/log/httpd/%Y-%m/live_bemoko_access-%Y-%m-%d.log 86400" bemokolog

If you want to log other headers you can do

LogFormat "%h %l %u %t %v \"%r\" %>s %b \"%{auth}C\" %D \"%{Referer}i\" \"%{User-Agent}i\" \"%{Content-Type}o\" \"%{Content-Length}o\"" bemokocustomlog
CustomLog "|/usr/sbin/rotatelogs /var/log/httpd/%Y-%m/live_bemoko_access_custom-%Y-%m-%d.log 86400" bemokocustomlog