Wednesday, May 16, 2012

Custom domain for application deployed on Cloud Foundry (or any other cloud provider)

The problem

The problem with an amazing Cloud Foundry, is that due to the limitations of beta version, it doesn't support mapping of custom (aka external) domains to the applications. It's a pity since free Cloud Foundry account has some impressive quota and could be used for initial production deployment of many web applications.

Are we then doomed to the http://applicationWithUglyUrl.cloudfoundry.com kind of URLs when deploying to the Cloud Foundry?


Reverse proxy to the rescue

The trick to deal the with ugly domain names is to use reverse proxy to forward requests from our custom domain to the Cloud Foundry application.

Reverse proxy allows to transparently forward HTTP requests from one server to another. The interesting side effect of the usage of reverse proxy is that you can hide your destination server behind the domain name mapped to that proxy.


Probably the most popular reverse HTTP proxy implementation is Apache HTTP server with enabled mod_proxy module.


Reverse proxy and the Cloud Foundry

In the case of Cloud Foundry you need to configure your custom domain (for example myapplication.com) to point to some reverse proxy.  Then configure the reverse proxy to forward the requests matching your custom domain (http://myapplication.com) to the Cloud Foundry instance (for example http://myapplication.cloudfoundry.com).


What if I don't have Apache HTTP instance?

You don't have your personal Apache HTTP to host reverse proxy? No problem. As I read at this brilliant blog you can use bs2grproxy reverse proxy application hosted on free Google App Engine Account. Here's what exactly you need to do:
  • download Python Google App Engine SDK and unzip it.
  • create application for proxy on Google App Engine (give it a nice name, like myapplication-proxy).
  • download bs2grproxy application template and unzip it.
  • open file bs2grpconfig.py in text editor and change value of TARGET_HOST property to the address of your Cloud Foundry application (for example to the http://myapplication.cloudfoundry.com).
  • open file app.yaml in text editor and set the proper id and version of your Google App Engine proxy application (for example application: myapplication-proxy and version: 1).
  • install Python (sudo apt-get install python under Ubuntu) since bs2grproxy is written in it.
  • from Python GAE SDK directory execute the following command - ./appcfg.py update /home/user/projects/myapplication-proxy . The latter command will deploy your proxy application to the Google App Engine infrastructure.
  • since Google App Engine allows you to configure custom external domains even for free accounts, set your custom domain (myapplication.com in our example) to point to the proxy deployed on the Google App Engine (for example to myapplication-proxy.appspot.com).
Congratulations. From now on all requests to the http://myapplication.com will be transparently forwarded by reverse proxy to the http://myapplication.cloudfoundry.com.


What about the other hosting providers?

Remember that you can use reverse HTTP proxy to mirror any domain. So you can use it to deal with all kinds of ugly URL mappings.

4 comments:

  1. this no longer works fine. my cloudfoundry app has spring+springMvc+springSecurity, and the proxy is mixing the sessions!!!

    ReplyDelete
  2. Thanks for the info, I'll check it out and probably update the post.

    ReplyDelete
  3. Can you post your Apache's reverse proxy config?
    I ama trying to use my existing reverse proxy but it seems CF is somehow blocking the workaround, or I am doing something wrong.

    ReplyDelete
  4. I'll try to find it in my spare time :) .

    ReplyDelete