You can easily deploy your application as a stand-alone server by setting the application HTTP port to 80
:
%production.http.port=80
But if you plan to host several applications in the same
server or load balance several instances of your application for
scalability or fault tolerance, you can use a front-end HTTP server. Note that using a front-end HTTP server will never give you better performance than using Play server directly!
Set-up with lighttpd
This example shows you how to configure lighttpd
as a front-end web server. Note that you can do the same with Apache,
but if you only need virtual hosting or load balancing, lighttpd is a
very good choice and much easier to configure!
The /etc/lighttpd/lighttpd.conf
file should define things like this:
Apache as a front proxy to allow transparent upgrade of your application
The
basic idea is to run 2 Play instances of your web application and let
the front-end proxy load-balance them. In case one is not available, it
will forward all the requests to the available one.
Let’s start the same Play application two times: one on port 9999 and one on port 9998.
Copy the application 2 times and edit the application.conf in the conf directory to change the port numbers.
For each web application directory:
play start mysuperwebapp
Now, let’s configure our Apache web server to have a load balancer.
In Apache, I have the following configuration:
如何需要监听90端口,需要在外部配上
listen 90
<VirtualHost mysuperwebapp.com:80>
ServerName mysuperwebapp.com
<Location /balancer-manager>
SetHandler balancer-manager
Order Deny,Allow
Deny from all
Allow from .mysuperwebapp.com
</Location>
<Proxy balancer://mycluster>
BalancerMember http://localhost:9999
BalancerMember http://localhost:9998 status=+H
</Proxy>
<Proxy *>
Order Allow,Deny
Allow From All
</Proxy>
ProxyPreserveHost On
ProxyPass /balancer-manager !
ProxyPass / balancer://mycluster/
ProxyPassReverse / http://localhost:9999/
ProxyPassReverse / http://localhost:9998/
</VirtualHost>
The important part is balancer://mycluster. This
declares a load balancer. The +H option means that the second Play
application is on stand-by. But you can also instruct it to
load-balance.
Every time you want to upgrade mysuperwebapp, here is what you need to do:
play stop mysuperwebapp1
The load-balancer then forwards everything to mysuperwebapp2. In the meantime update mysuperwebapp1. Once you are done:
play start mysuperwebapp1
You can now safely update mysuperwebapp2.
Apache
also provides a way to view the status of your cluster. Simply point
your browser to /balancer-manager to view the current status of your
clusters.
Because Play is completely stateless you don’t have to
manage sessions between the 2 clusters. You can actually easily scale to
more than 2 Play instances.