How to Host Multiple Domains on a Single Google App Engine Instance

I have two domain names that I manage, one for my online business card and the other is just a place to access my projects. I had them both hosted on the same Amazon EC2 instance until the free tier expired recently and I went looking for another free place to host my websites. I settled on Google App Engine as, although it is a PaaS and gives me less direct control of the instance, it is very generous with how much it provides for free and allows me to host my websites for as long as I want without spending a cent. Also I can still write rich, dynamic web content in python (or Java or Go if I want to) and have plenty of control over how my website functions.

The first thing I did was set up the ‘app.yaml’ file to redirect all requests to my main python application which serves out the content for each domain. I couldn’t use the ‘static’ handler as even though it is more efficient than serving files through an application, it could not differentiate between domains. Therefore, I passed all requests to an application handler which determines which domain the request was from and sends back the required content. It’s a little bit more involved than just reading the requested file and sending it to the client. As a bare minimum the server should send the MIME type in the headers so the browser knows how to interpret the file. To do this I simply imported the Python ‘mimetype’ package and it worked out of the box.  I also added some basic cache checking to avoid sending down a whole file again if it hasn’t changed since the last request.

If you are like me and just like to get your teeth straight into an example have a look at the example code I hosted on my GitHub account.

If you aren’t happy with using your application processing power to serve your static files, a great way to take load off the server is to use a caching proxy service called Cloudflare. Cloudflare caches your website automatically and serves out your files through it’s CDN taking the load off your server and speeding up responses to your clients. Best of all, it’s free. Not to mention that it provides protection from things like denial of service attacks.