Fixing Redirects of a Play! App behind an Apache2 SSL Proxy
Java . Network . Play! FrameworkSo you just finished your first Play! App. You want to run that thing behind an Apache2 as a HTTPS Proxy, because you do not want, that your User-Credentials are read as clear text.
So a very basic Apache Configuration looks like this:
<IfModule mod_ssl.c> Listen 443 SSLRandomSeed startup builtin SSLRandomSeed connect builtin <VirtualHost _default_:443> SSLEngine on ServerName example.com ServerAdmin admin@example.com ErrorLog /var/log/apache2/ssl_error_log SSLCipherSuite ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP:+eNULL SSLCertificateFile /etc/apache2/ssl/example/newcert.pem SSLCertificateKeyFile /etc/apache2/ssl/example/webserver.nopass.key SSLCACertificateFile /etc/apache2/ssl/demoCA/cacert.pem SSLCARevocationPath /etc/apache2/ssl/certs/demoCA/crl ProxyPass /play http://127.0.0.1:9000/play ProxyPassReverse /play http://127.0.0.1:9000/play </VirtualHost> </IfModule>
I did already explained how to run a Play! Application within a Application Context. Here our Context is just “play”, but you can set it to something else. You can also change and add seperate instances with different ports (over 9000!!!).
You should alter two settings in your conf/application.conf
# you need to add this
context=/play
# you need to uncomment this to prevent Play! from serving aside your Apache2 Proxy
http.address=127.0.0.1
# you may uncomment and change this port number for chaning it
# http.port=9000
Time for a Test Run. At a first glance it seems to work pretty nice. But as soon as you want to use the nifty routing redirects from Play!, the whole system breaks, because Play! still things, it runs in plain http on port 9000. To solve this, you need to change to things:
- Make Apache2 to send a specific header, that the Request was send through a Proxy
- Make Play! to fix the redirect to the correct URL
The first part is pretty easy. Just add
RequestHeader set X_FORWARDED_PROTO 'https'
within your VirtualHost Tag – you may need to enable the headers module first to make that work.
The second part is a little bit more difficult. You have to add a before filter to your application controller:
public class Application extends Controller {
@Before
private static void checkSSL() {
if (request.headers.get("x_forwarded_proto") != null
&& "https".equals(request.headers.get("x_forwarded_proto").value())) {
request.secure = true;
request.port = 443;
}
if (request.headers.get("x-forwarded-server") != null) {
request.domain = request.headers.get("x-forwarded-server").value();
}
}
...
}
You can find the sources on GitHub.
B.t.w. the same problem also might appears in Rails Apps, i might write about this later on.
Related
Archives
- August 2025
- November 2023
- February 2023
- January 2023
- April 2020
- January 2018
- December 2017
- May 2017
- February 2016
- September 2015
- December 2014
- August 2014
- June 2014
- March 2014
- February 2014
- September 2013
- August 2013
- July 2013
- November 2012
- October 2012
- September 2012
- June 2012
- May 2012
- April 2012
- March 2012
- February 2012
- January 2012
- December 2011
- November 2011
- October 2011
- August 2011
- July 2011
- June 2011
- May 2011
- January 2011
- August 2010
- July 2010
- June 2010
- May 2010
- January 2010
- November 2009
- October 2009
- September 2009
- July 2009
- June 2009
- May 2009
- April 2009
- March 2009
- February 2009
- January 2009
- November 2008
- October 2008
- September 2008
- August 2008
- July 2008
- June 2008
- May 2008
- March 2008
- February 2008
- January 2008
- December 2007
- November 2007
- October 2007
- September 2007
- August 2007
- July 2007
- June 2007
- May 2007
- March 2007
- February 2007
- January 2007
- December 2006
- November 2006
- September 2006
- June 2006
- May 2006
- April 2006
- March 2006
- February 2006
- January 2006
Leave a Reply