PythonAnywhere and CloudFlare

CloudFlare is a security and acceleration service that sits between your application and the big, bad internet. Here's how to get all that goodness for your PythonAnywhere web app.

Since CloudFlare works by taking over the DNS configuration for a site, this will only work for custom domains. I have used minimumviableserver.com, which is one of the domains we have lying around.

The setup of a website on CloudFlare is simplicity itself. CloudFlare interrogates the DNS system for the domain's current settings and provides excellent instructions about what needs to be changed. After following this process, there is only one thing more to be done.

In order to configure CloudFlare so that it serves a web app from PythonAnywhere, the one change that is necessary is to set the "www" CNAME to point to a PythonAnywhere account. With these settings, the naked domain minimumviableserver.com gets redirected to www.minimumviableserver.com.

Screenshot of CloudFlare DNS settings

The CNAME for direct.minimumviableserver.com that you can see in the screenshot is provided by CloudFlare so you can get direct access to your servers without going through CloudFlare (for ssh or ftp, for example). It is not very useful for PythonAnywhere because <username>.pythonanywhere.com is probably a different web app and ssh.pythonanywhere.com is there for direct ssh access.

Once everything's set up on CloudFlare, it's time to create the web app on PythonAnywhere.

Screenshot of web app creation

So now when we go to www.minimumviableserver.com, we see our web app, but how do we know it's going through CloudFlare? looking at the network logs in our browser's developer tools, we can see that there are plenty of indicators in the response headers.

Here are the response headers for a dynamically generated html page: Screenshot of dynamic response headers

and these are the response headers for a static resource on that page: Screenshot of static response headers

blog comments powered by Disqus

PythonAnywhere is a Python development and hosting environment that displays in your web browser and runs on our servers. They're already set up with everything you need. It's easy to use, fast, and powerful. There's even a useful free plan.

You can sign up here.