How to set up Cloudflare in front of an Azure Web App?
Cloudflare in front of an Azure Web App explained through DNS, reverse proxying, custom domains, SSL, caching, security and origin protection.
How to set up Cloudflare in front of an Azure Web App?
When a web application is open to the internet, it receives much more than the traffic of our users. Search engine crawlers, bots, scrapers and sometimes attackers are hitting the same web servers. And also, our users are not only near the Azure data center where the application is hosted, they are spread across the world, and every request travels all the way to our origin servers. As a result of this, our servers are busy serving the same static content again and again, the response time is higher for distant users and we pay for the resources consumed by the unwanted traffic.
Cloudflare is more than a CDN. Those who are not aware of Cloudflare, it is a reverse proxy service with a global network of data centers, which provides security (DDoS protection, Web Application Firewall) and performance (caching, optimization) features in front of our websites. In the previous article, we discussed how to integrate systems with Azure Logic Apps. In this article, we are going to see how Cloudflare works in front of an Azure Web App and the step by step instructions to set it up, including the DNS, custom domain, SSL and caching.
TL;DR Summary
- Cloudflare works as a reverse proxy, our DNS points to Cloudflare and Cloudflare forwards the requests to our Azure Web App.
- The setup is a DNS change, we move the name servers of our domain to Cloudflare.
- Azure needs to verify the custom domain, with the Cloudflare proxy enabled, use the
awverifyCNAME record. - Use SSL mode “Full” (or “Full (strict)” with a valid certificate on the origin), never “Flexible” for applications with login.
- Static content is cached at the Cloudflare edge by default, use page rules for more caching.
- Restrict the origin to accept only Cloudflare IP ranges, otherwise attackers can bypass Cloudflare.
- Read the real visitor IP from the
CF-Connecting-IPheader.
Challenges
Unwanted traffic reaches the origin
Without a proxy, every bot and every attacker request is processed by our web servers. It consumes CPU and memory, and in case of a DDoS attack, our application is down for our real users.
Latency for global users
A user in Asia accessing an application hosted in North Europe waits for every request to travel across the world. The Time To First Byte (TTFB) and the page load time are high, especially for static files like images, scripts and styles.
Repeated static content
The same JavaScript and CSS files are served millions of times from our web servers. It is a waste of resources which we pay for in Azure.
How Cloudflare works?
The following diagram depicts the traffic flow without and with Cloudflare.

Without Cloudflare, users and bots connect directly to the Azure Web App. With Cloudflare, the DNS of our domain resolves to the nearest Cloudflare data center. Cloudflare filters the malicious traffic, serves the cached content from the edge and forwards only the remaining requests to our Azure Web App. The arrow to the web app is thinner on the right side, because fewer requests reach the origin.
Why Cloudflare is important?
In order to protect our application from the attacks and bad bots at the edge, to serve the content close to our users and to reduce the load and cost of our origin servers, we decided to use Cloudflare as a front liner for our public web applications. Most importantly, the setup doesn’t need any change in the application code, it is a DNS and configuration change.
Steps to Implement:
The following diagram explains the setup steps.

Follow the following instructions to set up Cloudflare in front of an Azure Web App. Replace the input place holders with actual values.
Add the site to Cloudflare
- Sign up to Cloudflare and click “Add site”. Enter your domain name (ex:
example.com). - Cloudflare scans the existing DNS records. Review them carefully and add the missing ones, specially MX records for email.
- Select the plan. The Free plan is enough to start, Pro or Business plans add the Web Application Firewall and more page rules.
Change the name servers
-
Cloudflare shows two name servers (ex:
<name>.ns.cloudflare.com). -
Login to your domain registrar and replace the existing name servers with the Cloudflare ones.
-
Just to pulse check, check the name servers after the change. It can take up to 24 hours to propagate.
nslookup -type=NS example.com
Configure the DNS records for Azure
-
Add a CNAME record for
wwwpointing to the Azure Web App, with the proxy enabled (orange cloud).Type Name Value Proxy CNAME www <web app name>.azurewebsites.net Enabled (orange) CNAME awverify.www awverify.<web app name>.azurewebsites.net DNS only (grey)Note: With the proxy enabled,
wwwresolves to Cloudflare IPs, so Azure cannot verify the domain ownership through the normal CNAME. Theawverifyrecord (DNS only) allows Azure to verify it. -
For the root domain (
example.com), Cloudflare supports CNAME at the root through CNAME flattening, or use a page rule to redirect the root towww.
Add the custom domain in Azure
-
Login to the Azure portal, open the Web App → “Custom domains” → “Add hostname”.
-
Enter
www.example.comand click “Validate”. Azure checks theawverifyrecord. -
Click “Add hostname”.
Info: Custom domains are supported only in Shared, Basic, Standard and Premium App Service plans, not in the Free plan.
Configure SSL
-
In Cloudflare → “Crypto”, set the SSL mode.
Mode Browser to Cloudflare Cloudflare to Azure Recommendation Flexible HTTPS HTTP − Don’t use, traffic to origin is not encrypted Full HTTPS HTTPS (certificate not validated) + Good start Full (strict) HTTPS HTTPS (valid certificate required) + Best, needs a certificate for the custom domain on Azure (or a Cloudflare Origin CA certificate) -
In Azure, enable “HTTPS Only” redirect through a URL rewrite rule in
web.config, or use the Cloudflare page rule “Always Use HTTPS”.
Configure caching
-
By default, Cloudflare caches static files by extension (js, css, images, fonts). HTML and API responses are not cached.
-
Add page rules for additional caching, for example cache a static marketing section.
URL pattern : www.example.com/assets/* Setting : Cache Level = Cache Everything, Edge Cache TTL = 1 day -
Make sure the application sends correct
Cache-Controlheaders. Don’t cache pages with user specific data.
Lock down the origin
-
Restrict the Azure Web App to accept requests only from the Cloudflare IP ranges (published at cloudflare.com/ips), using
ipSecurityinweb.config.<system.webServer> <security> <ipSecurity allowUnlisted="false"> <add ipAddress="<cloudflare ip>" subnetMask="<subnet mask>" allowed="true" /> <!-- add all Cloudflare IPv4 and IPv6 ranges --> </ipSecurity> </security> </system.webServer>Note: Keep this list updated when Cloudflare publishes new ranges, and remember that Azure’s own availability tests or monitoring tools may also need access.
Read the real visitor IP
-
Since all requests come from Cloudflare,
Request.UserHostAddressis a Cloudflare IP. Read the visitor IP from the header in the logs and in the application.var visitorIp = Request.Headers["CF-Connecting-IP"] ?? Request.UserHostAddress;
Pulse check
-
Just to pulse check, check the response headers.
CF-RAYshows the request went through Cloudflare, andCF-Cache-Status: HITshows it was served from the cache.curl -I https://www.example.com/assets/site.css
Must Do and Don’ts
- Must Do: Review all DNS records before changing the name servers, otherwise email or other services stop working.
- Must Do: Use SSL mode “Full” or “Full (strict)”.
- Must Do: Restrict the origin to Cloudflare IP ranges.
- Should: Measure the requests to the origin before and after, through Cloudflare analytics and your APM tool.
- Don’t: cache HTML pages which contain user specific data.
- Don’t: expose the
azurewebsites.netURL publicly, it bypasses Cloudflare.
Wrap up
In this article, we have looked at the challenges of serving a web application directly to the internet, how Cloudflare works as a reverse proxy, and the step by step instructions to set up DNS, custom domain, SSL, caching and origin lock down for an Azure Web App. Thanks for reading this article and I hope this article provided you some insights into protecting and speeding up your applications. In the next article, I will walk you through volumes, networks and Docker Compose in Getting started with Docker - Part 2.
Related Posts
How to secure ASP.NET Web API with OAuth 2.0 bearer tokens (OWIN)?
Securing ASP.NET Web API with OAuth bearer tokens through OWIN, covering token endpoints, external JWT issuers, bearer validation, claims, and authorization roles.
How to use Azure Redis Cache for session state and output cache?
Azure Redis Cache explained for shared session state and output caching, including providers, configuration, cache-aside usage, and multiple web instances.
What is Authentication and Authorization in ASP.NET?
ASP.NET authentication and authorization explained through Forms Authentication, Windows Authentication, OAuth 2.0, JWT bearer tokens, and security approach selection.