Version 2, last updated by Richard Dallaway at May 05, 2011 21:56 UTC
Apache and Jetty Configuration
Apache can act as a front-end for Jetty.
In this example we are running our example.com app on Jetty on port 8080. Apache and Jetty are both on the same machine.
Apache configuration
In your Apache configuration, add a section to configure the proxy module to send all requests to Jetty:
<IfModule mod_proxy_balancer.c>
ProxyPass / balancer://balancer-group/ stickysession=JSESSIONID
ProxyPassReverse / balancer://balancer-group/
ProxyPreserveHost on
<Proxy "balancer://balancer-group">
Allow from all
BalancerMember http://localhost:8080 route=example.com loadfactor=100
</Proxy>
</IfModule>
Although we’re using the proxy_balancer we’re not doing any actual balancing across multiple Jetty instances: all traffic will go to the single Jetty instance on port 8080.
Jetty 6
In this example configuration file, we are using JNDI for a database connection based on the default Jetty configurations shipped with Jetty 6.
This example uses MySQL and C3P0 for connection pooling. The JDBC driver jar and the polling library (whichever ones you use) need to be in the $JETTY_HOME/lib/ext folder.
In the example below we are using a system property of app.home to indicate where our WAR file can be found and jetty.logs for where we want logs to be written to. Of course, feel free to replace them as you see fit.
As we are mapping all requests to this Jetty instance, name your WAR file webapps/root.war
<?xml version="1.0"?>
<!DOCTYPE Configure PUBLIC "-//Mort Bay Consulting//DTD Configure//EN" "http://jetty.mortbay.org/configure.dtd">
<!-- =============================================================== -->
<!-- Configure the Jetty Server -->
<!-- -->
<!-- Documentation of this file format can be found at: -->
<!-- http://docs.codehaus.org/display/JETTY/jetty.xml -->
<!-- -->
<!-- =============================================================== -->
<Configure id="Server" class="org.mortbay.jetty.Server">
<!-- =========================================================== -->
<!-- Server Thread Pool -->
<!-- =========================================================== -->
<Set name="ThreadPool">
<New class="org.mortbay.thread.QueuedThreadPool">
<Set name="minThreads">10</Set>
<Set name="maxThreads">200</Set>
<Set name="lowThreads">20</Set>
</New>
<!-- Use this connector for many frequently idle connections
and for threadless continuations.
-->
<Call name="addConnector">
<Arg>
<New class="org.mortbay.jetty.nio.SelectChannelConnector">
<Set name="host"><SystemProperty name="jetty.host" /></Set>
<Set name="port"><SystemProperty name="jetty.port" default="8080"/></Set>
<Set name="maxIdleTime">30000</Set>
<Set name="Acceptors">2</Set>
<Set name="statsOn">false</Set>
<Set name="confidentialPort">8443</Set>
<Set name="lowResourcesConnections">5000</Set>
<Set name="lowResourcesMaxIdleTime">5000</Set>
</New>
</Arg>
</Call>
<!-- =========================================================== -->
<!-- Set handler Collection Structure -->
<!-- =========================================================== -->
<Set name="handler">
<New id="Handlers" class="org.mortbay.jetty.handler.HandlerCollection">
<Set name="handlers">
<Array type="org.mortbay.jetty.Handler">
<Item>
<New id="Contexts" class="org.mortbay.jetty.handler.ContextHandlerCollection"/>
</Item>
<Item>
<New id="DefaultHandler" class="org.mortbay.jetty.handler.DefaultHandler"/>
</Item>
<Item>
<New id="RequestLog" class="org.mortbay.jetty.handler.RequestLogHandler"/>
</Item>
</Array>
</Set>
</New>
</Set>
<!-- =========================================================== -->
<!-- Configure the webapp deployer. -->
<!-- A webapp deployer will deploy standard webapps discovered -->
<!-- in a directory at startup, without the need for additional -->
<!-- configuration files. It does not support hot deploy or -->
<!-- non standard contexts (see ContextDeployer above). -->
<!-- -->
<!-- This deployer is configured to deploy webapps from the -->
<!-- $JETTY_HOME/webapps directory -->
<!-- -->
<!-- Normally only one type of deployer need be used. -->
<!-- -->
<!-- =========================================================== -->
<New id="my-database" class="org.mortbay.jetty.plus.naming.Resource">
<Arg><!-- blank here means Scope = JVM --></Arg>
<Arg>jdbc/my-database</Arg>
<Arg>
<New class="com.mchange.v2.c3p0.ComboPooledDataSource">
<Set name="driverClass">com.mysql.jdbc.Driver</Set>
<Set name="jdbcUrl">jdbc:mysql://localhost:3306/mydatabase?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true</Set>
<Set name="user">myuser</Set>
<Set name="password">mypassword</Set>
<Set name="maxPoolSize">25</Set>
<Set name="minPoolSize">2</Set>
<Set name="maxStatements">50</Set>
<Set name="idleConnectionTestPeriod">3600</Set>
<Set name="testConnectionOnCheckin">true</Set>
<Set name="automaticTestTable">connection_test</Set>
<Set name="maxIdleTime">21600</Set>
</New>
</Arg>
</New>
<Array id="plusConfig" type="java.lang.String">
<Item>org.mortbay.jetty.webapp.WebInfConfiguration</Item>
<Item>org.mortbay.jetty.plus.webapp.EnvConfiguration</Item>
<Item>org.mortbay.jetty.plus.webapp.Configuration</Item>
<Item>org.mortbay.jetty.webapp.JettyWebXmlConfiguration</Item>
<Item>org.mortbay.jetty.webapp.TagLibConfiguration</Item>
</Array>
<Call name="addLifeCycle">
<Arg>
<New class="org.mortbay.jetty.deployer.WebAppDeployer">
<Set name="contexts"><Ref id="Contexts"/></Set>
<Set name="webAppDir"><SystemProperty name="app.home" default="/nowhere"/>/webapps</Set>
<Set name="parentLoaderPriority">false</Set>
<Set name="configurationClasses"><Ref id="plusConfig"/></Set>
<Set name="extract">true</Set>
<Set name="allowDuplicates">false</Set>
<Set name="defaultsDescriptor"><SystemProperty name="jetty.home" default="."/>/etc/webdefault.xml</Set
>
</New>
</Arg>
</Call>
<!-- =========================================================== -->
<!-- Configure Request Log -->
<!-- Request logs may be configured for the entire server here, -->
<!-- or they can be configured for a specific web app in a -->
<!-- contexts configuration (see $(jetty.home)/contexts/test.xml -->
<!-- for an example). -->
<!-- =========================================================== -->
<Ref id="RequestLog">
<Set name="requestLog">
<New id="RequestLogImpl" class="org.mortbay.jetty.NCSARequestLog">
<Set name="filename"><SystemProperty name="jetty.logs" default="./logs"/>/yyyy_mm_dd.request.log</Set>
<Set name="filenameDateFormat">yyyy_MM_dd</Set>
<Set name="retainDays">90</Set>
<Set name="append">true</Set>
<Set name="extended">false</Set>
<Set name="logCookies">false</Set>
<Set name="LogTimeZone">GMT</Set>
</New>
</Set>
</Ref>
<!-- =========================================================== -->
<!-- extra options -->
<!-- =========================================================== -->
<Set name="stopAtShutdown">true</Set>
<Set name="sendServerVersion">true</Set>
<Set name="sendDateHeader">true</Set>
<Set name="gracefulShutdown">1000</Set>
</Configure>
Starting Jetty
You can start Jetty in the standard way, using the Jetty start up and shutdown scripts, if you’re using one Jetty on the machine.
As an alternative, the following commands will start up a Jetty just for your application, e.g., to allow for multiple Jetty instances on the same machine. It assumes you have a /home/youapp/jetty folder structure containing etc/myapp.xml for your Jetty configuration file and a logs/ folder for log files.
export JETTY_HOME=/opt/jetty-6.1.26
export APP_HOME=/home/yourapp/jetty
export JAVA_OPTIONS="-Xms128m -Xmx256m -DSTOP.PORT=8097 -DSTOP.KEY=secret -XX:MaxPermSize=96M -Drun.mode=Production -Duser.language=en -Duser.country=GB -Djetty.logs=$APP_HOME/logs -Dapp.home=$APP_HOME"
cd $JETTY_HOME
java $JAVA_OPTIONS -jar start.jar $APP_HOME/etc/myapp.xml >& $APP_HOME/logs/start.log &
You may not care about the language and country settings, and will want to adjust the -X options for your needs.
The stop command would be as above but:
java $JAVA_OPTIONS -jar start.jar --stop $APP_HOME/etc/myapp.xml