Application Gateway in Azure: The Smart Front Door for Your Web Applications

Application Gateway in Azure: The Smart Front Door for Your Web Applications

A beginner-friendly, book-style guide to Azure Application Gateway. Learn how it reads web requests, routes them to the right servers, protects them with a web application firewall, scales with demand, and how to design, secure, monitor, and troubleshoot it in production.

Picture a busy hotel on a holiday weekend. Hundreds of guests arrive every hour. Some want to check in, some want the restaurant, some want the conference hall, and a few are trying to sneak into rooms that are not theirs. If everyone just wandered through the lobby and found their own way, there would be chaos. Instead, the hotel has a front desk. A calm person looks at each guest, understands what they want, checks that they are allowed, and points them to the right place.

A website or web application has the same problem. Thousands of visitors send requests every second, and behind the scenes there may be many servers, each handling a different part of the application. Something must stand at the front, read each request, decide where it goes, block the harmful ones, and keep everything running when a server fails. In Microsoft Azure, one of the services built for this role is Azure Application Gateway.

This guide assumes you know nothing about the topic. Every new term is explained the first time it appears, every big idea has an everyday comparison, and the chapters build on one another. By the end you will understand what Application Gateway is, how it works inside, how to configure and scale it, how to secure it, how it compares with the other Azure traffic services, and how to find and fix problems when things go wrong.

1The Problem: Why Web Traffic Needs a Smart Front Door

Before learning any product, understand the problem it solves. Once the problem is clear, the features feel natural instead of random.

One server is never enough

A tiny website can live on a single server. That server receives every request, runs the application, and sends back answers. This works until one of three things happens. The number of visitors grows beyond what one machine can handle. The server crashes or needs maintenance. Or the application grows into several pieces, such as a shopping cart service, an image service, and an account service, each running on different servers.

The usual fix is to run many servers and place a load balancer in front of them. A load balancer is a device or service that receives incoming traffic and spreads it across a group of servers, so no single server is overwhelmed and failures can be hidden from visitors.

Analogy

Think of a supermarket with ten checkout counters. A staff member at the entrance of the checkout area watches the queues and tells each shopper which counter has the shortest line. If one counter closes, the staff member simply stops sending people there. Shoppers never need to know which counters exist. That staff member is a load balancer.

Why a plain load balancer is not always enough

Some load balancers only look at the envelope, not the letter. They see which address a message came from and which address it is going to, and they spread connections evenly. They have no idea what the visitor is asking for. A request for a product page and a request for a heavy video download look the same to them.

Modern web applications need more. They want to send requests for the pictures folder to one group of servers and requests for the checkout page to another. They want to handle the secure padlock (HTTPS encryption) in one central place instead of on every server. They want to block attacks such as someone trying to inject malicious commands through a web form. They want to redirect visitors who type the plain web address to the secure version. These jobs require a device that can read the contents of web requests. That is exactly what a Layer 7 load balancer does, and Application Gateway is Azure’s regional service of that kind.

Layer 7
the level where web requests are read and understood
v2
the current generation, with autoscaling and zone support
125
documented maximum instances for a v2 gateway

The jobs of a smart front door

Job 1

Distribute

Spread requests across several healthy servers so none is overloaded.

Job 2

Route by content

Send each request to the right group of servers based on the web address, the path, or the host name.

Job 3

Handle encryption

Manage HTTPS certificates centrally and relieve servers of the heavy lifting.

Job 4

Protect

Inspect requests with a web application firewall and stop common attacks.

Job 5

Stay healthy

Check servers regularly and stop sending traffic to any that are failing.

Job 6

Adapt

Rewrite headers and addresses, redirect visitors, and grow or shrink with demand.

An illustrative example

Imagine a hypothetical ticket-selling website. It has a home page service, a search service, a seat-selection service, and a payment service, each running on its own set of virtual machines. On the night tickets for a popular concert go on sale, traffic grows twenty times larger within minutes. The company wants requests for search to hit the search servers, payment requests to hit the payment servers, attackers to be blocked before they reach any server, and capacity to grow by itself. One Application Gateway placed in front can do all of that, and the rest of this guide shows how.

2Core Concepts: The Networking Basics You Need

A few simple ideas about how the web works will make everything else in this guide easy to follow. None of them are difficult, and each comes with a comparison.

Layers: the post office model

Network engineers describe communication in layers, a stack of jobs where each layer relies on the one beneath it. For our purposes two layers matter. Layer 4, the transport layer, deals with connections: which computer is talking to which computer, on which numbered door, called a port. Layer 7, the application layer, deals with the actual conversation: which web page was requested, which form data was sent, which language the browser prefers.

Analogy

Layer 4 is the postal worker who sorts parcels by the address on the outside, without opening them. Layer 7 is the assistant who opens the parcel, reads the letter inside, and says, “This is about billing, so it belongs to the finance department.” Application Gateway is the assistant.

Anatomy of a web request

When you type a web address into a browser, a few things happen. First, your computer asks the Domain Name System (DNS), the internet’s phone book, to turn a name such as shop.example.com into a number called an IP address. Next, it opens a connection to that address. If the address starts with https, the two sides perform a TLS handshake, a quick exchange that sets up encryption. Finally, the browser sends an HTTP request.

An HTTP request has a few key parts. The method says what the browser wants to do, such as GET to fetch something or POST to send something. The path says which resource, such as /images/logo.png. The host header says which website name the visitor typed, which allows one server address to host many websites. Headers carry extra details such as cookies and language. Sometimes there is a body, such as the contents of a form. The server replies with a status code, a number that summarises the result: 200 means success, 404 means not found, 502 means a gateway received a bad answer from a server behind it, and so on.

HTTPS and TLS in plain words

TLS (Transport Layer Security) is the technology behind the padlock in the browser. It does two things. It encrypts the conversation so eavesdroppers see only scrambled data, and it proves the server’s identity using a certificate, a digital document issued by a trusted authority that says “this website really is who it claims to be.” Certificates expire, so they must be renewed, which is a classic source of outages.

Reverse proxy: the representative

A reverse proxy is a server that sits in front of other servers and speaks to the outside world on their behalf. Visitors talk to the proxy, and the proxy talks to the real servers, then passes the answers back. Application Gateway is a reverse proxy. This has a useful side effect: visitors never connect directly to your application servers, which can live on private addresses that the internet cannot reach.

Analogy

A celebrity’s agent takes all the calls. Callers speak only to the agent, who decides whether to pass the message on, replies on the celebrity’s behalf, and never reveals the celebrity’s private number. A reverse proxy is the agent.

Load balancing methods

How should a load balancer choose among servers? The simplest method is round robin, which takes servers in turn: first, second, third, then back to the first. It is fair when all servers and requests are similar. Other methods consider how busy each server is, or keep a visitor on the same server using a cookie so their shopping cart stays put. This last idea is called session affinity or sticky sessions, and we return to it later.

Latency, throughput, and connections

Three words recur in performance talk. Latency is the delay for one request, like the time for a single parcel to arrive. Throughput is the total amount of data moved per second, like how many parcels per hour a depot handles. A connection is a conversation channel kept open between two computers. A gateway must handle many connections, a high throughput, and low latency at the same time, which is why capacity planning matters.

i
Tip

When something goes wrong with a gateway, ask which layer the problem lives in. Is the visitor unable to reach the gateway (network), unable to establish encryption (TLS), or reaching the gateway but getting the wrong answer (routing or application)? Sorting by layer cuts debugging time dramatically.

3What Azure Application Gateway Is

With the basics in place, we can describe the service itself: what it is, what it is not, and how its generations differ.

The short definition

Azure Application Gateway is a managed, regional, Layer 7 load balancer for web traffic. “Managed” means Microsoft runs and maintains the underlying machines, applies updates, and keeps them healthy, so you configure behaviour rather than babysit servers. “Regional” means a gateway lives in one Azure region, such as West Europe, and serves applications in that region. “Layer 7” means it understands HTTP, HTTPS, HTTP/2, and WebSocket traffic, not just raw connections.

Analogy

Application Gateway is the reception desk of a company’s office building, staffed by a service company. You decide the rules: which visitors go to which floor, who must be turned away, which visitors need an escort. The service company handles the staffing, replaces anyone who is sick, and brings in extra people during rush hour.

Two generations: v1 and v2

The service has existed in two main generations, called v1 and v2. The original v1 SKU (a SKU is simply a product tier or edition) had fixed instance sizes and manual scaling. The v2 generation, which is the one to use for any new design, brought autoscaling, support for spreading across availability zones, a static public address, faster deployment and updates, header and address rewriting, better performance, and integration with Azure Key Vault for certificates. Microsoft announced the retirement of v1 with a date in 2026, so existing v1 gateways should be migrated, and new projects should start with v2. Check the current Microsoft documentation for the exact status and migration help.

Capabilityv1 (older)v2 (current)
ScalingManual instance countAutoscaling between a minimum and maximum
Availability zonesNot supportedSupported, with zone redundancy
Public addressCan change when the gateway is stoppedStatic, never changes
Header and URL rewriteNot supportedSupported
Key Vault certificatesNot supportedSupported
Deployment and update timeSlowerFaster
StatusRetiring, migrate awayUse for all new work

Standard and WAF flavours

Within each generation you choose between two flavours. The Standard flavour provides the load balancing and routing features. The WAF flavour adds a web application firewall, a security layer that inspects requests for attacks. In v2 these are called Standard_v2 and WAF_v2. If your application faces the public internet, the WAF flavour is strongly recommended, and many organisations require it by policy.

What it is not

Application Gateway is often confused with its neighbours, so let us be clear. It is not a global service: it does not route users to the nearest region of the world. It is not a general-purpose network load balancer: it speaks web protocols and is meant for web traffic. It is not an API management product with subscriptions, quotas, and developer portals. It is not a content delivery network that caches files near users. Each of those jobs belongs to a different Azure service, and chapter 13 compares them in detail.

Where it can send traffic

The servers behind a gateway are called backends. A backend can be Azure virtual machines, a virtual machine scale set (a group of identical virtual machines that can grow and shrink), servers identified by an IP address or a domain name, even servers outside Azure reachable over a private network connection, and multi-tenant services such as Azure App Service web apps. This flexibility means one gateway can front a mixture of old and new application parts.

i
Note

Azure also offers a related product, Application Gateway for Containers, designed specifically for Kubernetes, and an Application Gateway Ingress Controller add-on that lets a gateway act as the entry point for a Kubernetes cluster. We meet both in chapter 12.

4Architecture and Components

An Application Gateway is assembled from a handful of building blocks. Learn the names and the relationships, and every configuration screen becomes readable.

flowchart LR
  USER["Visitor browser"] --> FIP["Frontend IP configuration"]
  FIP --> LST["Listener"]
  LST --> WAF["WAF policy"]
  WAF --> RULE["Request routing rule"]
  RULE --> BS["Backend settings"]
  RULE --> POOL["Backend pool"]
  BS --> POOL
  PROBE["Health probe"] --> POOL
  POOL --> VM1["Server 1"]
  POOL --> VM2["Server 2"]
  POOL --> APP["App Service"]
Figure 4.1: The main building blocks of an Application Gateway and how a request meets them

Frontend IP configuration

This is the address visitors use to reach the gateway. It can be a public IP address, reachable from the internet, a private IP address inside your virtual network, or both at once. Internal-only applications, such as a company intranet, use a private frontend. In v2, the public address is static and must use the Standard SKU of public IP. Newer capabilities also allow gateways that are private-only, with no public address at all; check the current documentation for how to enable that.

Listener

A listener is the part that waits for incoming connections on a specific combination of frontend address, port, and protocol. It answers the question “which traffic should I accept?” A basic listener accepts all traffic on a port. A multi-site listener also checks the host name, which is how one gateway can serve shop.example.com and blog.example.com separately. If the listener uses HTTPS, it holds the certificate used to establish encryption with the visitor.

Request routing rule

A rule connects a listener to a destination. It says “when a request arrives at this listener, send it to that backend pool using those backend settings,” or “redirect it to somewhere else.” A basic rule sends everything from the listener to one pool. A path-based rule looks at the path of the web address and sends different paths to different pools.

Backend pool

A backend pool is a named group of servers that can answer the same kind of request. The gateway spreads requests across the healthy members of the pool. A gateway can have many pools, such as one for the website, one for the API, and one for static images.

Backend settings

Backend settings (earlier called HTTP settings) describe how the gateway should talk to the servers in a pool. They include the protocol and port to use, whether to encrypt the connection to the backend, how long to wait for an answer, whether to use cookie-based session affinity, how to handle connection draining when a server is removed, and whether to override the host name sent to the backend.

Health probe

A health probe is a small, regular check the gateway makes against each backend server, such as asking for a particular page and expecting a good status code. Servers that fail the check are taken out of rotation until they recover.

Other pieces

  • Rewrite rule sets: instructions to add, change, or remove request and response headers or parts of the web address.
  • Redirect configurations: instructions to send visitors elsewhere, such as from HTTP to HTTPS.
  • WAF policy: the set of security rules, which can be attached to the whole gateway, to a single listener, or even to specific paths.
  • SSL and TLS settings: the certificates and the allowed protocol versions and cipher choices.
  • Dedicated subnet: a gateway lives in a subnet of your virtual network reserved for it, so that its instances have private addresses to use.
  • Private Link configuration: lets other networks reach the gateway privately, without crossing the public internet.
Analogy

Think of an airport. The frontend IP is the airport’s address on the map. The listener is the terminal entrance accepting certain kinds of passengers. The WAF is the security checkpoint. The routing rule is the signboard that says “Flights to Paris: Gate 5.” The backend settings are the instructions to the shuttle bus driver about how to carry passengers. The backend pool is the group of gates. The health probe is the staff member who walks each gate to check it is open.

i
Tip

When you design a gateway, work from the left of the diagram to the right. Decide the frontend first, then the listeners, then the rules, then the pools and settings, then the probes. Drawing the chain on paper before clicking anything prevents most misconfiguration.

5How It Works Inside: The Life of a Request

Follow a single request from the visitor’s browser to your server and back. Understanding each hop explains behaviours that otherwise seem mysterious.

sequenceDiagram
  participant B as Browser
  participant D as DNS
  participant G as Application Gateway
  participant S as Backend server
  B->>D: Where is shop.example.com
  D-->>B: Gateway public IP address
  B->>G: TLS handshake and HTTP request
  G->>G: Listener match, WAF check, rule match
  G->>S: New or reused connection with request
  S-->>G: Response with status code
  G->>G: Optional header rewrite
  G-->>B: Response to visitor
Figure 5.1: The journey of one request through Application Gateway

Step by step

1

DNS points to the gateway

You create a DNS record so your website name resolves to the gateway’s frontend address. The browser now knows where to go.

2

The connection and encryption start

The browser connects to the gateway’s port and, for HTTPS, completes the TLS handshake. The gateway presents the certificate configured on the listener.

3

The request is read

Because the gateway terminates the connection, it can read the full request: method, host, path, headers, and body.

4

The listener is chosen

The gateway matches the request to a listener by port, protocol, and, for multi-site listeners, host name.

5

The WAF inspects

If a firewall policy applies, the request is checked against its rules. Harmful requests are logged or blocked, depending on the mode.

6

The rule selects the destination

The routing rule attached to the listener decides the backend pool, perhaps by path. A redirect rule may answer the visitor directly instead.

7

A healthy server is picked

The gateway chooses one healthy member of the pool, normally in round robin fashion, or the one the visitor was previously bound to through the affinity cookie.

8

The request travels onward

The gateway opens or reuses a connection to the server, applying any header changes, and forwards the request.

9

The answer returns

The server’s response reaches the gateway, which can rewrite response headers, and then sends it to the visitor.

Two separate connections

A vital idea is that there are two separate connections: one from the visitor to the gateway and another from the gateway to the server. The gateway is not a wire that passes bytes through; it is a go-between that ends one conversation and starts another. This explains why the server sees the gateway’s address as the sender, instead of the visitor’s address.

To keep the visitor’s real address available, the gateway adds a header called X-Forwarded-For listing the original sender. It can also pass the original host name and protocol. Applications that log visitor addresses or make decisions by country must read these headers rather than the connection’s source address.

Connection reuse

Opening a new connection and encryption session is expensive. The gateway keeps connections to backends open and reuses them for many requests, which reduces delay and server load. This is one reason a gateway can improve performance even though it adds an extra hop.

Choosing a server

By default the gateway distributes requests across healthy backend servers in a round robin fashion. If cookie-based affinity is on, the first response includes a cookie naming the chosen server, and later requests with the cookie go to that same server so long as it is healthy. This helps older applications that keep visitor state in a server’s memory, but it can make load uneven, so modern designs avoid needing it by keeping state in a shared database or cache.

Analogy

Imagine a translator at an embassy. The visitor speaks to the translator in one conversation. The translator then turns to the ambassador and starts a second conversation, repeating the visitor’s request in the right form. The ambassador never talks to the visitor directly. The translator can also add a note: “This visitor came from the following city.” That note is the forwarded-for header.

!
Watch out

Because the gateway is a go-between, the host name your application sees may differ from what the visitor typed unless you configure it to pass the original host. Redirect loops and broken login pages are very often caused by this mismatch. Decide deliberately whether the gateway should keep the original host name or replace it with the backend’s own.

6Listeners and Routing: Sending Each Request to the Right Place

Routing is the heart of an application gateway. This chapter explains the choices you have and when each one fits.

flowchart TD
  IN["Incoming request"] --> L1{"Which host name"}
  L1 -->|"shop.example.com"| R1{"Which path"}
  L1 -->|"blog.example.com"| P3["Pool: blog servers"]
  R1 -->|"/images/*"| P1["Pool: image servers"]
  R1 -->|"/api/*"| P2["Pool: API servers"]
  R1 -->|"everything else"| P4["Pool: web servers"]
Figure 6.1: Host-based and path-based routing working together

Basic and multi-site listeners

A basic listener accepts every request that arrives at a certain address and port. It suits a gateway that serves a single website. A multi-site listener adds a check of the host name, so the same gateway, on the same public address and port, can serve many different website names, each with its own certificate and rules. This is called host-based routing, and it saves money because you do not need a separate gateway and public address for every site. You can even use wildcard host names, such as any name ending in example.com, when you want one listener to cover a family of subdomains.

Analogy

A basic listener is a shop with one door and one counter. A multi-site listener is a shopping centre entrance with a directory board: visitors who say “I am here for the bookshop” are sent one way, and visitors who say “I am here for the pharmacy” are sent another.

Path-based routing

With path-based routing the gateway looks at the part of the web address after the host name. A rule can say that anything starting with /images/ goes to the pool of image servers, anything starting with /api/ goes to the API servers, and everything else goes to the default web servers. The rules are checked in order, and a default path catches all requests that match no other pattern.

This pattern lets a team split a large application into independent services behind one public web address, which is a typical step when moving from a single large application to smaller services. Each service can be scaled, deployed, and fixed on its own, while visitors still see a single website.

Redirects

Sometimes the right answer is not to forward a request but to send the visitor somewhere else. A redirect tells the browser, “ask this other address instead.” The most common use is redirecting plain HTTP visitors to HTTPS, so that nobody accidentally uses an unencrypted connection. Redirects can also move visitors from an old site name to a new one, or from one path to another. A permanent redirect tells search engines the move is lasting, and a temporary one tells them to keep the old address.

Rewrites

A rewrite changes part of a request or response as it passes through, without the visitor seeing any change in the browser’s address bar. Common rewrites include adding a security header to every response, removing a header that reveals server details, changing the path before sending it to the backend so the backend does not need to know about the public layout, and changing the host name header. Rewrites can depend on conditions, such as only acting when a certain header is present, and can use values from the request itself, called server variables.

NeedFeatureVisitor sees
Serve many sites from one gatewayMulti-site listenerNormal site, own name
Different servers for different parts of a sitePath-based ruleOne site, one address
Force HTTPSRedirect from HTTP listenerAddress changes to https
Hide or add headersHeader rewriteNothing, changes are invisible
Hide internal URL structureURL path rewriteNothing, backend sees a different path
Retire an old domainRedirect to new siteAddress changes to the new site

Rule priority and order

When several rules could apply, the gateway needs a way to decide. In v2 each routing rule has a numeric priority, and the lowest number wins. Plan your priorities with gaps between them, such as 100, 200, and 300, so you can slot in a new rule later without renumbering everything. A very common source of confusion is a broad rule with a low number silently catching traffic meant for a more specific rule with a higher number.

i
Tip

Name your listeners, rules, and pools after what they do, not after their position, for example “shop-https-listener” and “api-pool.” Six months later, when an incident happens at night, readable names are worth their weight in gold.

A hypothetical example

A university runs one gateway with two multi-site listeners: one for the student portal and one for the library catalogue. The student portal listener has a path rule that sends /video/* to a pool of streaming servers and everything else to the general portal servers. A separate listener on port 80 redirects everyone to HTTPS. A rewrite rule adds a strict transport security header to every response. All of this lives in one gateway, with clear names and priorities.

7HTTPS and TLS: Termination, End-to-End Encryption, and Certificates

Encryption is one of the biggest reasons people deploy an application gateway. It also causes some of the most common configuration problems, so it deserves careful attention.

Option one: TLS termination

TLS termination (sometimes called SSL offload) means the gateway ends the encrypted conversation with the visitor, reads the request, and then sends it to the backend over a plain, unencrypted connection. The expensive encryption work is done once, centrally, and the backend servers are freed from it. Only one place needs the certificate and private key. The trade-off is that traffic between the gateway and the servers is not encrypted, which is acceptable only when that stretch of network is private and trusted, and when your compliance rules allow it.

Option two: end-to-end TLS

With end-to-end TLS, the gateway terminates the visitor’s encrypted connection so it can read and route the request, and then opens a second encrypted connection to the backend. Data is encrypted on both stretches of the journey, though it is briefly visible in plain form inside the gateway. Regulated industries such as finance and healthcare often require this approach. The backend servers need their own certificates, and the gateway must be told how to trust them, by providing the trusted root certificate that issued the backend’s certificate.

Analogy

TLS termination is like a courier who receives a locked box at the building entrance, opens it, and carries the contents loose to the right office inside. End-to-end TLS is a courier who opens the box at the entrance to read the address label, then puts the contents into a fresh locked box for the trip to the office.

Option three: TLS passthrough is not the same thing

Some people want the gateway to simply pass the encrypted bytes through without reading them. A classic Layer 7 gateway cannot do that, because it must read the request to route it. If you truly need a pure pass-through, a Layer 4 service such as Azure Load Balancer is the right tool. Newer versions of Application Gateway have added Layer 4 proxy features, so check the current documentation for their status and limits before relying on them.

Certificates: where they live

The certificate for a listener can be uploaded directly to the gateway as a file with a password, or, better, referenced from Azure Key Vault. Key Vault is Azure’s secure store for secrets and certificates. When the gateway references a Key Vault certificate, it uses a managed identity (an identity Azure creates for the gateway so it needs no stored password) to fetch the certificate. When you renew the certificate in Key Vault, the gateway picks up the new version automatically within a short time, which removes the dreaded “certificate expired at midnight” outage.

!
Watch out

If you reference a Key Vault certificate, make sure the gateway’s managed identity has permission to read it, and that Key Vault’s own network firewall allows the gateway. A locked Key Vault that refuses the gateway is a classic cause of a gateway stuck in a failed state after a change.

TLS policy: choosing protocol versions

TLS has versions, and older versions have known weaknesses. A TLS policy on the gateway sets the minimum protocol version and the allowed cipher suites, which are the specific encryption recipes. Modern guidance is to require TLS 1.2 or higher. Predefined policies exist that bundle sensible choices, and you can build a custom policy when compliance demands it. Setting the policy once at the gateway means every site behind it benefits, which is far easier than configuring each server.

Mutual TLS

Normally only the server proves its identity. With mutual TLS (mTLS), the visitor’s client must also present a certificate that the gateway verifies. This is common for business-to-business interfaces and device fleets, where only known clients should connect. The gateway can pass details of the client certificate to the backend through headers, so the application can make decisions based on who connected.

Backend certificate validation

For end-to-end TLS in v2, the gateway checks that the backend’s certificate comes from a trusted authority and that the name on it matches the host name the gateway uses to contact the backend. A mismatch produces a failed health check and a “502 Bad Gateway” for visitors. The usual fixes are to make the host name used by the gateway match the certificate, or to override the host name in the backend settings, and to supply the right root certificate if the backend uses a private authority.

ApproachEncrypted visitor to gatewayEncrypted gateway to serverBest for
TLS terminationYesNoSimple, trusted private networks
End-to-end TLSYesYesRegulated or high-security workloads
Plain HTTP onlyNoNoInternal testing only

Certificate habits that prevent outages

  • Store certificates in Key Vault and reference them rather than uploading files by hand.
  • Set calendar alerts or monitoring on certificate expiry at least thirty days ahead.
  • Use certificates that cover the exact host names you serve, or well-chosen wildcards.
  • Test a new certificate on a staging listener before switching production.
  • Document which certificate authority issued each certificate and who renews it.

8Backends, Health Probes, and Session Behaviour

A gateway is only as good as its knowledge of the servers behind it. This chapter explains how it learns which servers are healthy and how it treats visitors who move between them.

Health probes: the pulse check

Every few seconds, the gateway sends a small request to each backend server and watches the answer. If the server answers with an acceptable status code in time, it is marked healthy. If it fails several times in a row, it is marked unhealthy, and the gateway stops sending visitors to it. When it passes again, it returns to rotation. This happens automatically, which is how failures stay invisible to visitors.

A default probe is created for you, which contacts the backend on the same port and protocol as the backend settings and requests the root path. The documented defaults are a check every 30 seconds, a 30-second timeout, and three failures before marking a server unhealthy. A custom probe lets you choose the path, the host name, the interval, the timeout, the failure threshold, and what counts as a good answer, such as status codes between 200 and 399.

Analogy

A nurse walks the ward every half hour and checks each patient’s pulse. If a patient’s pulse is missing three checks in a row, the doctor stops assigning new tasks to that patient until the pulse returns. The probe is the nurse, and the failure threshold is the “three checks in a row.”

Writing a good health endpoint

The default probe asks for the home page, which may be slow, may redirect to a login page, or may return success even when the database is broken. It is far better to build a dedicated health endpoint in your application, a small page such as /health, that quickly checks the things the application truly needs and answers with a clear success or failure. Keep it light, because the gateway calls it constantly from every instance. Avoid making it so deep that a slow optional dependency causes all servers to be marked unhealthy at once, which would take the whole site offline.

!
Watch out

If every server in a pool is marked unhealthy, visitors receive an error. Some gateways fail open and send traffic anyway, but you should never rely on that. Design health checks so that a single shared failure, such as a down cache, does not mark every server unhealthy unless the application truly cannot work.

Backend settings that matter

Protocol and port

How to reach the server

Choose HTTP or HTTPS and the port the backend listens on. Choosing HTTPS enables end-to-end encryption.

Request timeout

How long to wait

The time the gateway waits for the backend to answer before giving up. The default is modest, so long-running operations need a larger value.

Host name

What name to send

Keep the visitor’s original host name, pick a name from the backend address, or set a fixed override. Essential for App Service and multi-tenant backends.

Affinity

Stick or spread

Cookie-based affinity keeps a visitor on one server. Leave it off unless the application needs it.

Connection draining

Graceful removal

Lets in-flight requests finish when a server is being removed or is unhealthy, up to a time limit.

Custom path

Path override

Replaces or prefixes the request path sent to the backend, useful when the backend serves from a sub-folder.

Session affinity and why to be wary

With cookie-based session affinity, the gateway sets a cookie on the first response, naming the server that answered. Future requests with that cookie go to the same server. It is a bridge for older applications that keep each visitor’s state, such as a shopping basket, inside one server’s memory. The costs are uneven load, since popular sessions pile onto certain servers, and lost sessions when a server fails or is replaced. The healthier design is a stateless application that stores visitor state in a shared place such as a database or a distributed cache, so any server can serve any visitor.

Connection draining

When you remove a server from a pool, for example during an update, you do not want to cut off visitors halfway through a download. Connection draining lets the gateway stop sending new requests to that server while allowing existing requests to finish, up to a time limit you set. Used together with rolling updates, it gives updates with no visible interruption.

Talking to App Service and other multi-tenant backends

Azure App Service hosts many customers’ websites behind shared addresses and chooses which site to serve by looking at the host name header. If the gateway forwards the original visitor-facing host name, App Service may not recognise it. The usual solution is to have the gateway send the App Service’s own default host name to the backend, while the visitor continues to see the custom name. If your application builds absolute links or sets cookies using the host name it receives, this change can break things, so test login flows carefully, and consider keeping the original host with the right custom domain configured on the App Service instead.

9The Web Application Firewall: Protecting Against Attacks

The internet is full of automated scanners probing every public website for weaknesses. A web application firewall is your first line of defence against the most common attacks.

What a WAF does

A web application firewall (WAF) inspects the contents of web requests and compares them to rules describing known attack patterns. Unlike a traditional network firewall, which mainly looks at addresses and ports, the WAF reads the web request itself: the path, the query string, the headers, the cookies, and the body. Because the gateway already terminates and reads each request, adding the WAF is a natural fit.

Analogy

A network firewall is the guard at the building gate who checks that you are on the guest list. A web application firewall is the security officer inside who looks into your bag, notices you are carrying something unusual, and politely asks you to leave it at the desk.

The attacks it helps stop

  • SQL injection: tricking a database by sneaking commands into a form field or web address.
  • Cross-site scripting: planting malicious scripts that run in other visitors’ browsers.
  • Command injection and file inclusion: tricking a server into running instructions or loading files it should not.
  • Protocol violations: malformed requests designed to confuse servers.
  • Scanners and bad bots: automated tools hunting for weaknesses, or scraping content aggressively.

Managed rule sets

Microsoft provides managed rule sets based on the OWASP Core Rule Set, a respected open collection of rules for the most common web attack types, and newer managed rule sets that Microsoft updates over time. You choose which set and version to apply. A separate bot protection rule set classifies automated visitors as good bots, such as search engines, bad bots, or unknown, so you can allow, block, or watch them. Rule set names and versions change over time, so check the current documentation when you plan a deployment.

Custom rules

Besides the managed rules, you can write custom rules: your own conditions and actions. Examples include blocking requests from specific countries or address ranges, allowing only your office addresses to reach an admin path, blocking requests with a certain header value, and applying rate limiting, which restricts how many requests a single client may make in a time window to blunt brute-force and scraping attempts. Custom rules are evaluated before managed rules and have priorities, so order matters.

flowchart TD
  REQ["Incoming request"] --> CR{"Custom rules match"}
  CR -->|"Allow rule"| OK["Forward to backend"]
  CR -->|"Block rule"| BLK["Return 403 and log"]
  CR -->|"No match"| MR{"Managed rules score"}
  MR -->|"Below threshold"| OK
  MR -->|"Above threshold"| MODE{"Mode"}
  MODE -->|"Prevention"| BLK
  MODE -->|"Detection"| LOGONLY["Log and forward"]
Figure 9.1: A simplified view of how a WAF decides what to do with a request

Detection mode and prevention mode

The WAF can run in two modes. In detection mode it only records what it would have blocked and lets everything through. In prevention mode it actively blocks matching requests, normally returning a 403 Forbidden response. The wise rollout is to start in detection mode, study the logs for a couple of weeks, tune out false alarms, and then switch to prevention. Turning on prevention for a live application without testing is a reliable way to block legitimate customers.

False positives and exclusions

A false positive is a harmless request that looks like an attack. A text field where a customer pastes a code sample, or a rich-text editor that sends HTML, may trigger rules built to catch script injection. The remedy is not to switch the WAF off. Instead, you add an exclusion, which tells a specific rule to ignore a specific field, or you disable one rule for one path. Keep a record of every exclusion and the reason for it, because each one is a deliberate small hole.

Policies and where they apply

Modern WAF settings live in a separate resource called a WAF policy, which can be attached to the whole gateway, to individual listeners, or to specific paths. This lets you apply strict rules to a login page and a lighter set to a public image folder, or give different teams their own policies on a shared gateway. Using policies also makes it easy to reuse the same security configuration across many gateways.

i
Tip

Send WAF logs to a Log Analytics workspace and build a simple dashboard of the most frequently triggered rules and the most blocked client addresses. Patterns in that dashboard guide your tuning and reveal real attacks early.

!
Watch out

A WAF is a strong layer, not a complete solution. It does not fix vulnerable code, it can be bypassed by unusual encodings, and it does not replace secure development, patching, and least-privilege access. Treat it as one layer in a defence made of many.

10Performance, Autoscaling, and High Availability

A front door that collapses during the busiest hour is worse than no door. This chapter explains how Application Gateway grows, how to estimate its size, and how to keep it available.

Autoscaling in v2

A v2 gateway can autoscale: you give it a minimum and a maximum number of instances, and Azure adds or removes instances as traffic changes. Scaling out takes a few minutes, so it is not instantaneous. A sudden spike larger than your current capacity can briefly cause slowness or errors before new instances arrive. For known events, such as a planned sale, set a higher minimum instance count beforehand, and for unpredictable spikes keep a comfortable cushion above your normal level.

Analogy

A restaurant calls extra waiters when the dining room fills, but the waiters need fifteen minutes to arrive. A smart manager books extra staff in advance for a holiday dinner, and keeps a few on call for surprises. The minimum instance count is the staff booked in advance.

Capacity units

In v2, you are billed and measured in capacity units. A capacity unit is a blend of three kinds of load, and the gateway counts whichever dimension is highest at a given moment. The documented approximations are that one capacity unit can handle about 2,500 persistent connections, or about 2.22 megabits per second of throughput, or one compute unit of processing, where a compute unit is roughly ten new TLS connections per second using common 2048-bit certificates. These figures are guides, and real results depend on your pages, certificate type, rules, and WAF settings, so always load test.

Example loadDimensionApproximate capacity units
200 Mbps of steady trafficThroughputAbout 90 (200 divided by 2.22)
10,000 open connectionsPersistent connectionsAbout 4 (10,000 divided by 2,500)
500 new TLS connections per secondComputeAbout 50 (500 divided by 10)
All three togetherHighest one winsAbout 90

Each gateway instance supports roughly ten capacity units, so a load needing about 90 capacity units would settle at around nine instances. The WAF, large response bodies, and complex rewrite rules all increase the processing share. Plan your maximum to be comfortably above your biggest expected peak, because hitting the maximum means no more growth.

~2,500
persistent connections per capacity unit
~2.22 Mbps
throughput per capacity unit
~10
capacity units per instance

Availability zones and redundancy

An availability zone is a physically separate data centre location within an Azure region, with independent power, cooling, and networking. A v2 gateway can be zone redundant, meaning its instances spread across zones, so the loss of an entire data centre does not take the gateway down. This is the default choice for production. Your backend servers should also be spread across zones, otherwise the gateway survives but its servers do not.

Beyond one region

Because Application Gateway is regional, a regional outage is outside what a single gateway can survive. For the highest level of availability, teams deploy a gateway and application stack in two regions and place a global service, such as Azure Front Door or Traffic Manager, in front to steer visitors to the healthy region. Chapter 13 explains how these fit together.

Performance habits

  • Enable HTTP/2 for visitor-facing listeners to reduce delays for pages with many resources.
  • Keep the gateway close to the backends in the same region, ideally the same zones, to cut round-trip time.
  • Keep rewrite rules and custom WAF rules lean, because every rule costs processing time.
  • Tune timeouts to match real application behaviour, so slow operations do not fail while stuck requests do not hold connections forever.
  • Load test before launch, including TLS handshake rates, because handshakes are far more expensive than reused connections.
  • Serve static files elsewhere when possible, such as from a content delivery network, so the gateway handles mainly dynamic requests.
i
Note

The gateway adds a small delay of its own, usually a few milliseconds, because it is an extra hop that reads and processes each request. For most applications, savings from connection reuse, central encryption, and better load spreading outweigh that cost.

11Networking Design: Subnets, NSGs, Routes, and Private Access

A gateway does not float in empty space. It lives inside your virtual network, and its placement decides who can reach it and what it can reach.

flowchart LR
  NET["Internet visitors"] --> PIP["Public IP"]
  PIP --> AGW["Application Gateway in its own subnet"]
  AGW --> WEB["Web subnet servers"]
  AGW --> API["API subnet servers"]
  AGW --> PAAS["App Service through private endpoint"]
  CORP["Corporate network via VPN or ExpressRoute"] --> AGW
  AGW --> LOG["Log Analytics workspace"]
Figure 11.1: A typical placement of Application Gateway inside a virtual network

The dedicated subnet

A virtual network is your private network in Azure, divided into subnets, which are smaller address ranges. Application Gateway needs a subnet used only for gateways, because it creates its instances there and hands each one a private address. You cannot mix v1 and v2 gateways in the same subnet, and you should not place virtual machines or other services in it. The common guidance for v2 is a subnet of size /24, which provides 256 addresses, so there is room for the maximum number of instances plus upgrades and growth. A subnet that is too small is a classic reason for being unable to scale.

Analogy

The gateway’s subnet is a staff car park reserved for the reception team. If the car park has only ten spaces and the team needs to grow to twenty on a busy day, the extra staff cannot park, and the desk stays understaffed. Always size the car park for the busiest day.

Network security groups

A network security group (NSG) is a set of allow and deny rules for network traffic. You can attach one to the gateway’s subnet, but v2 has mandatory requirements. You must allow inbound traffic from the GatewayManager service tag on ports 65200 through 65535, which Azure uses to manage the gateway, and you must allow traffic from the Azure load balancer probes. You must also allow the visitor ports you serve, typically 80 and 443. If any of these is blocked, the gateway may show errors or fail to update. Always check the latest Microsoft guidance, since requirements evolve.

On the backend side, remember that backend servers must allow traffic from the gateway’s subnet on the port the gateway uses. A server NSG that only allows traffic from the internet will refuse the gateway, and the health probe will fail. This is one of the most common reasons for a brand-new gateway showing all servers unhealthy.

User-defined routes

A route table with user-defined routes (UDRs) overrides Azure’s default traffic paths, often to send traffic through a firewall appliance. On the gateway subnet, be careful. Management traffic must reach Azure’s control plane directly, so a default route that sends everything to a firewall can break the gateway. Specific supported patterns exist for sending backend-bound traffic through a firewall, and the allowed combinations have changed over time, so follow the current documentation closely and test in a non-production environment first.

Public, private, or both

A gateway can have a public frontend for internet visitors, a private frontend for internal visitors reachable only inside your network or through a VPN or private link to your office, or both with different listeners. Internal applications such as HR portals typically use only a private frontend. Many organisations also want a gateway with no public address at all, and recent capabilities make private-only deployments possible with added network controls. Confirm the current feature status and requirements before you design around it.

Private endpoints and Private Link

A private endpoint gives a platform service, such as a web app or storage account, a private address inside your network. A gateway can route to a backend through such an address, so the backend can disable all public access. In the other direction, Private Link on the gateway lets other networks, including other subscriptions and organisations, connect to the gateway privately without crossing the public internet. These two tools help you reduce the public surface of your whole application.

The hub and spoke layout

Larger organisations commonly organise their network as a hub and spoke. A central hub network holds shared services such as firewalls and VPN connections, and each application lives in a separate spoke network connected to the hub. An application gateway typically lives in the spoke of the application it serves, close to its backends. Some designs put a shared gateway in the hub to serve many applications, which saves money but concentrates risk and makes teams share rules, so decide with your security and platform teams.

i
Tip

Create the subnet, NSG, and route table through infrastructure as code from the beginning. A hand-built network is almost impossible to reproduce exactly in a second environment, and subtle differences cause the dreaded “works in test but not in production.”

Network itemCommon requirementTypical mistake
Gateway subnetDedicated, about /24 for v2Too small, or shared with other resources
Gateway NSGAllow GatewayManager ports 65200 to 65535 and visitor portsBlocking management ports
Backend NSGAllow traffic from the gateway subnet on the backend portOnly allowing internet traffic
RoutesKeep management traffic directDefault route sent to a firewall
DNSBackends resolvable from the gateway subnetPrivate zone not linked to the network

12Working With Virtual Machines, App Service, and Kubernetes

Different kinds of backends call for different patterns. Here is how a gateway works with the most common ones.

Virtual machines and scale sets

The simplest backend is a group of virtual machines, or a virtual machine scale set, a group of identical machines that can grow and shrink. You can add the network interfaces of the machines to a backend pool, and when the scale set adds a new machine, it joins the pool automatically. This is the classic pattern for web servers and application servers, and it works well with rolling updates, since you can take one machine out at a time with connection draining.

App Service and other platform services

For Azure App Service and similar managed platforms, you add the service’s address as a backend target. The key concern, covered in chapter 8, is the host name. A second concern is that, by default, the App Service is also reachable directly on the internet, so visitors might bypass the gateway and its WAF. To close that gap, restrict the App Service to accept traffic only from the gateway, using access restrictions based on the gateway’s address or a private endpoint.

Analogy

Imagine a bank with a security desk at the front door, but also an unlocked back door that anyone can walk through. The front desk is pointless unless the back door is locked. Restricting the backend so it accepts traffic only from the gateway is locking the back door.

Kubernetes: AGIC and Application Gateway for Containers

Kubernetes is a system that runs and manages containers, which are small, packaged units of software. A Kubernetes cluster needs an ingress: a component that brings outside web traffic to the right service inside the cluster. Azure offers two ways to use gateway technology for this.

Add-on

Application Gateway Ingress Controller

A program running in the cluster watches Kubernetes ingress definitions and configures a normal Application Gateway to match. Traffic can go straight to the pods, the running containers, which avoids an extra hop.

Newer service

Application Gateway for Containers

A newer Kubernetes-focused service that supports modern Kubernetes routing standards and updates configuration faster, with a managed control plane. Check the current documentation for features and availability.

In both approaches, the person managing the application describes routing in Kubernetes files, and the controller translates it into gateway settings. This keeps routing rules next to the application’s own configuration. The trade-off is that the cluster controller now owns the gateway’s configuration, so manual changes made in the portal may be overwritten. Treat the controller as the single source of truth.

Multiple backends behind one gateway

A real company often has a mix: an older application on virtual machines, a new customer portal on App Service, and microservices on Kubernetes. One gateway can front all three, using multi-site listeners and path rules to separate them. This can be the glue that lets teams modernise one piece at a time, since visitors do not notice whether /orders lives on an old server or a new container.

The strangler pattern

This is a well-known approach for gradually replacing an old system. You place a gateway in front of the old application. Then you build a replacement for one feature, such as search, and add a path rule that sends /search to the new service while everything else continues to go to the old application. Feature by feature, the old system shrinks until nothing is left. The name comes from a vine that slowly grows around a tree and eventually replaces it.

i
Note

Whatever the backend, make sure it can handle the forwarded headers described in chapter 5. Frameworks usually need a setting to trust the gateway’s forwarded host, protocol, and client address headers, otherwise redirects and logs show the gateway’s details instead of the visitor’s.

13Choosing Among Azure’s Traffic Services

Azure has several services that distribute traffic, and their names sound alike. Knowing the differences prevents buying the wrong tool.

flowchart TD
  A["Need to distribute traffic"] --> B{"Is it web HTTP or HTTPS traffic"}
  B -->|"No, any TCP or UDP"| C["Azure Load Balancer"]
  B -->|"Yes"| D{"Single region or global"}
  D -->|"Single region"| E["Application Gateway"]
  D -->|"Global, many regions"| F{"Need edge caching and acceleration"}
  F -->|"Yes"| G["Azure Front Door"]
  F -->|"DNS level steering is enough"| H["Traffic Manager"]
Figure 13.1: A simplified decision tree for choosing a traffic distribution service

The four main services

Layer 4, regional

Azure Load Balancer

Distributes any TCP or UDP traffic across servers by address and port. Very fast and simple. It does not read web requests, so it cannot route by path or host name or run a web firewall.

Layer 7, regional

Application Gateway

Understands web requests. Offers path and host routing, TLS handling, rewrites, and a WAF, inside one region and inside your virtual network.

Layer 7, global

Azure Front Door

A global entry point running at Microsoft’s edge locations around the world. Offers routing, acceleration, caching, and a global WAF, and can steer visitors to the closest or healthiest region.

DNS, global

Traffic Manager

Answers DNS questions with different addresses depending on health, location, or weight. It never touches the traffic itself, so it cannot inspect or change requests.

QuestionLoad BalancerApplication GatewayFront DoorTraffic Manager
Reads HTTP contentNoYesYesNo
ScopeRegionRegionGlobalGlobal, DNS only
Web application firewallNoYes, with WAF flavourYesNo
Path-based routingNoYesYesNo
Caching and accelerationNoNoYesNo
Non-web protocolsYesLimitedNoAny, via DNS
Lives inside your virtual networkYesYesNo, at the edgeNo

Combining them

These services are not rivals. They are layers. A well-known pattern places Azure Front Door at the global edge for acceleration and early filtering, with one Application Gateway in each region for detailed routing and in-network protection, and Load Balancer or direct pools for servers behind. If you do this, lock the gateway so it accepts web traffic only from Front Door, using the Front Door service address tag and by verifying the Front Door identifier header, so attackers cannot skip the edge and hit the gateway directly.

Analogy

Consider a worldwide hotel chain. Traffic Manager is the phone operator who tells callers which city’s hotel to dial. Front Door is the airport greeter, who meets guests near where they land and gets them to the right city quickly. Application Gateway is the reception desk of the specific hotel. Load Balancer is the elevator system that assigns guests to floors without asking what they came for.

Which to choose when

  • One region, a web application, you need a WAF and routing inside your network: Application Gateway.
  • Users around the world, you need fast delivery and automatic regional failover: Front Door, often combined with gateways.
  • Non-web traffic such as a database or a game server: Load Balancer.
  • Only need simple DNS-based failover, possibly for non-web endpoints: Traffic Manager.
  • You want API subscriptions, quotas, and a developer portal: look at API Management, and place a gateway in front of it if you need a WAF.

14Security: Defence in Depth Around the Gateway

Security is never one switch. It is a set of layers, so that the failure of one does not expose everything. Here is how the layers around an application gateway fit together.

Layer by layer

1

Absorb floods: DDoS protection

A distributed denial of service attack floods a site with traffic from many machines. Azure provides basic protection by default for all public addresses, and a stronger paid DDoS protection plan can be applied to the virtual network that contains the gateway’s public address.

2

Block known bad: the WAF

Managed rules, bot rules, and custom rules stop common attacks and abusive clients before they reach your code.

3

Encrypt: TLS everywhere

Require modern TLS versions, use end-to-end encryption for sensitive data, and consider mutual TLS for known clients.

4

Isolate: network controls

Keep backends on private addresses, use NSGs to allow only the gateway to reach them, and use private endpoints for platform services.

5

Identify: managed identities and least privilege

The gateway uses a managed identity to read certificates from Key Vault, with permission only to read the specific certificates it needs. Staff get only the Azure roles they need to change it.

6

Observe: logs and alerts

Collect access and WAF logs, send them to a central workspace, and alert on unusual patterns, such as a sudden spike in blocked requests.

Hardening the response

The gateway can add security headers to every response using rewrite rules. Examples include a strict transport security header that tells browsers to always use HTTPS, a header that prevents browsers from guessing file types, and a header that limits where framing and scripts may load from. It can also strip headers that reveal server software and versions, which would help an attacker choose an exploit. This is a cheap, centralised improvement for every application behind the gateway.

Controlling who changes the gateway

A misconfigured rule on a gateway can expose or break an entire application, so changes should be controlled. Use Azure role-based access control to limit who can modify gateway resources, apply resource locks to prevent accidental deletion, require changes through infrastructure code reviewed by a second person, and record every change in the activity log. Treat the gateway like production code, because it is.

Protecting the direct path

A frequent weakness is the bypass: a path to the backend that avoids the gateway. If a web server still has a public address with open ports, attackers can find it by scanning and simply skip the WAF. Close every other door. Remove public addresses from backends where possible, allow inbound traffic only from the gateway subnet, and verify with an external scan that the backends are not reachable directly.

Analogy

Security in layers is like a medieval castle: a moat (DDoS protection), outer walls with guards who check bags (WAF), an inner gate that opens only with the password (TLS and identity), locked rooms inside (network isolation), and watchmen who write everything in a log book (monitoring). An attacker must defeat every layer, not just one.

Compliance and data protection

The gateway sees decrypted request data, including form fields and sometimes sensitive personal information, so treat its logs with care. Avoid logging full request bodies. Limit who can read the logs, set a retention period that matches your policies, and mask sensitive values where possible. When regulations such as data residency apply, remember that a gateway is regional, so logs and traffic stay in the region you chose, which helps compliance but requires careful planning when you use global services in front.

!
Watch out

Never disable the WAF or loosen a rule “just for a moment” to fix a problem in production and then forget about it. Make a tracked change with an owner and an expiry date, and use detection-mode testing on a copy of the policy when possible.

15Monitoring, Logging, and Troubleshooting

Every gateway fails to behave at some point. The skill is to find out which part is at fault in minutes instead of hours.

Three sources of truth

Metrics

Numbers over time

Healthy and unhealthy host counts, total and failed requests, response status counts, throughput, current connections, capacity units used, backend response time, and total request time.

Logs

Records of events

Access logs describe each request and which backend served it. Firewall logs show which WAF rule matched. Activity logs record who changed the configuration.

Backend health

Live server status

The backend health view in the portal shows each server as healthy, unhealthy, or unknown, with a message explaining why.

Send diagnostics to a Log Analytics workspace, a central store you can search with a query language. Combine it with alerts so the right person is told when something crosses a threshold, for example when unhealthy hosts exceed zero for five minutes, when the failed request rate rises, or when capacity units approach the maximum you set.

Reading the common error codes

StatusWhat it usually meansFirst checks
502 Bad GatewayThe gateway could not get a valid answer from a backendBackend health, probe path and host, certificate name and root, NSG on backends, backend actually running
504 Gateway TimeoutThe backend took longer than the request timeoutBackend slowness, timeout value in backend settings, long-running operations
403 ForbiddenOften a WAF block or a custom ruleFirewall log for the matching rule, then exclusions or tuning
400 Bad RequestMalformed or oversized request or headersHeader sizes, cookies growing too large, unusual encodings
413 Request Entity Too LargeUpload bigger than allowedWAF body size limits and file upload limits
Redirect loopsHost or protocol mismatch between gateway and appForwarded headers, host name override, HTTPS redirect rules

A step-by-step method

1

Reproduce and note the exact error

Record the address, time, status code, and any response header or identifier. Exact facts beat guesses.

2

Check backend health

If servers are unhealthy, read the message. It often says exactly which probe check failed.

3

Test the backend directly

From a machine inside the same network, request the health path directly on the backend. If it fails there, the gateway is innocent.

4

Read the access and firewall logs

Find the request, see which listener and rule handled it, which backend answered, and how long each stage took.

5

Inspect the network path

Check NSGs, routes, DNS resolution, and certificates, which are the usual hidden culprits.

6

Change one thing at a time

Alter a single setting, re-test, and record the result. Changing five things at once hides the real cause.

Typical root causes of unhealthy backends

  • The probe asks for a path that returns an error or a redirect the probe does not accept.
  • The probe host name does not match the backend’s certificate or web site binding.
  • A backend NSG or server firewall blocks the gateway subnet.
  • The backend’s certificate has expired, or its authority is not trusted by the gateway.
  • The application is up, but its health endpoint depends on something broken.
  • The backend is only listening on a different port from the one in backend settings.
i
Tip

Keep a short run book for the gateway: where the logs are, which dashboards to open, how to read backend health, who owns certificates, and how to roll back a change. During an incident, a page of clear steps is worth more than a brilliant expert who is asleep.

Network Watcher and other helpers

Azure Network Watcher offers tools to test connectivity between the gateway subnet and the backends, view effective network rules, and capture packets when you need to see exactly what crossed the wire. Use them when logs show the gateway sent a request but no answer came back, since the problem is then in the network path rather than in the gateway’s settings.

16Deployment and Operations: From First Design to Daily Care

A gateway is not set up once and forgotten. It has a lifecycle of design, build, test, release, change, and retirement. Handling that lifecycle with discipline is what keeps a front door reliable.

flowchart LR
  A["Design"] --> B["Build as code"]
  B --> C["Test in staging"]
  C --> D["Release to production"]
  D --> E["Monitor and tune"]
  E --> F["Change safely"]
  F --> C
  E --> G["Retire or migrate"]
Figure 16.1: The lifecycle of a gateway from design to retirement

Design: write the decisions down

Before creating anything, record the basics. Which applications will sit behind the gateway? Which host names and paths? Public, private, or both? Which certificates and who owns them? Which WAF mode and rule set? What peak traffic do you expect, and what minimum and maximum instance counts follow from it? Which regions and zones? A short design document written first saves long meetings later, and it becomes the reference for the next engineer.

Build as code

Infrastructure as code means describing your cloud resources in text files that a tool reads and turns into real resources. Azure offers Bicep, ARM templates, and Terraform support for this. For a gateway, which has many interlocking pieces, code is far better than clicking in a portal. It is repeatable, so staging and production match. It is reviewable, so a colleague can spot a risky change before it ships. It is recoverable, so you can rebuild a gateway after a disaster. And it leaves a history of who changed what and why.

Analogy

Building from code is like using a written recipe instead of cooking from memory. Anyone can reproduce the dish, you can improve one step at a time, and a fire in the kitchen does not erase your ability to make it again.

Test in staging

Run a staging gateway that mirrors production in structure, even if smaller. Test the full chain: DNS, certificate, listener, WAF, rule, backend, probe, and application behaviour such as login and file uploads. Run a load test to learn the capacity units your traffic really uses. Test failure: stop a backend and watch traffic move away, expire a test certificate on purpose, and break the health endpoint to see what visitors experience.

Release and change safely

Changes to a gateway can affect all visitors at once, so use patterns that limit risk. One approach is blue-green deployment: you keep the current backend pool (blue) and prepare a new one (green) with the updated application, test green privately, and then switch a routing rule to point to it, with the option to switch back within seconds. Another approach is canary release: send a small share of traffic to the new version first, using a separate path or a separate listener for testers, and expand if all looks well. Always keep changes small and reversible.

Operate: the regular chores

  • Certificates: track expiry dates and renewals, and test renewed certificates early.
  • WAF tuning: review blocked and flagged requests monthly and adjust exclusions.
  • Capacity review: compare peak capacity units against your maximum every quarter.
  • Access review: check who can change the gateway and remove people who no longer need access.
  • Rule review: remove old listeners, rules, and pools that nobody uses, since clutter hides mistakes.
  • Documentation: keep the design note and run book in step with reality.

Migrating from v1 to v2

If you still run a v1 gateway, plan its migration. Microsoft provides guidance and tooling to copy the configuration of a v1 gateway into a new v2 gateway. Key points to plan for include the new gateway getting a new public address, so DNS records must be updated at cutover. Certificates and backend trust settings must be re-checked, because v2 validates backend certificates more strictly. Health probes and rewrite behaviour may differ slightly, so test every listener. A good approach is to build the v2 gateway alongside the old one, test it using a temporary host name, lower the DNS cache time in advance, and then switch DNS and keep the old gateway running for a short fallback period.

i
Tip

For non-production environments, consider stopping a v2 gateway outside working hours when the feature is available for your configuration, since you pay for it while it is running. Automating a start in the morning and a stop in the evening can reduce cost noticeably.

17Design Patterns and Anti-patterns

A design pattern is a proven answer to a recurring problem. An anti-pattern is a tempting approach that keeps causing trouble. Here are the most useful of each for application gateways.

Patterns worth adopting

Pattern

Single front door

Publish many applications behind one public address with multi-site listeners and path rules. It simplifies DNS, certificates, and security review.

Pattern

Offload and centralise

Handle TLS, redirects, and security headers at the gateway so every application behaves consistently.

Pattern

Edge plus regional

Use Front Door globally and a gateway per region, with the gateway locked so it accepts traffic only from the edge service.

Pattern

Strangler migration

Gradually move features from an old application to new services by changing path rules one at a time.

Pattern

Detect, tune, prevent

Roll out the WAF in detection mode first, tune out false alarms, then switch to prevention.

Pattern

Health endpoint per app

Give each application a light, honest health page and point a custom probe at it.

Pattern

Gateway as code

Define everything in reviewed code, with identical structure across test and production.

Pattern

Stateless backends

Keep visitor state in a shared store so any server can answer any request and affinity is unnecessary.

Anti-patterns to avoid

ANTI-PATTERN 01Avoid
Context

A team launches a new customer site and, to be safe about blocking real users, leaves the WAF in detection mode, intending to change it later.

The trap

Nobody reads the logs, and “later” never arrives. A year on, the WAF has recorded thousands of attack attempts and blocked none of them. The team believes it is protected, which is worse than knowing it is not.

The better way

Give detection mode a fixed end date, review the logs weekly during that period, add needed exclusions, and then enable prevention. Set an alert if a gateway is still in detection mode after the agreed date.

ANTI-PATTERN 02Avoid
Context

A company places a WAF-enabled gateway in front of its web servers but leaves the servers’ public addresses and open ports in place “for administration.”

The trap

Scanners find the servers’ public addresses within hours and attack them directly. The gateway and its WAF are never involved, because the attackers simply avoid the front door.

The better way

Remove public exposure from backends, allow inbound traffic only from the gateway subnet, and use a secure administration path such as a bastion service or private connectivity for operators.

ANTI-PATTERN 03Avoid
Context

A team uploads a certificate file manually to the gateway and notes the expiry date in a personal calendar.

The trap

The engineer who set the reminder leaves the company. The certificate expires on a weekend, browsers show a scary warning to every visitor, and the site is effectively down until someone realises what happened.

The better way

Store certificates in Key Vault, reference them from the gateway through a managed identity, automate renewal where possible, and alert on expiry from a shared monitoring system rather than a personal calendar.

ANTI-PATTERN 04Avoid
Context

A health probe is left on its default settings and asks for the home page, which performs a database query and takes several seconds on a busy server.

The trap

Under heavy load, the home page becomes slow, probes time out, and the gateway marks servers unhealthy. With fewer servers, the remaining ones get even more load and also fail. A small slowdown turns into a full outage, called a cascading failure.

The better way

Create a light, fast health endpoint, set sensible timeouts and failure thresholds, and make sure health checks measure whether the server can work, not how fast the busiest page loads.

More anti-patterns in brief

  • One giant rule set nobody understands. Hundreds of unnamed rules with overlapping priorities. Split by application and name everything.
  • Turning off the WAF to fix an error. Instead, find the exact rule and add a narrow exclusion.
  • Tiny subnet, maximum instances set high. The gateway cannot scale because there are no free addresses.
  • Using affinity to hide an unhealthy design. It masks state problems that will bite later.
  • Making manual portal changes to a code-managed gateway. The next deployment erases them, usually at the worst moment.

18Best Practices, Cost Awareness, and Common Mistakes

This chapter gathers the habits that separate calm operations from late-night panics, and explains where the money goes so the bill holds no surprises.

Where the cost comes from

Application Gateway v2 is billed mainly in two parts: a fixed hourly charge for having the gateway running, and a variable charge based on the capacity units it consumes each hour. The WAF flavour costs more than the Standard flavour. You also pay for the public IP address, for data leaving Azure, and for the logs you store. Prices differ by region and change over time, so always check the current price list or use Azure’s pricing calculator when you plan.

Cost driverWhy it growsHow to keep it sensible
Fixed gateway hoursEvery gateway costs money even when idleShare a gateway across several apps, stop non-production gateways when not used
Capacity unitsMore traffic, more handshakes, heavier rulesRight-size the minimum, avoid wasteful rules, offload static content
WAF flavourSecurity processing on every requestUse it where traffic is public, keep rule sets purposeful
Data transferData leaving Azure or crossing regionsKeep backends and gateway in the same region, use caching at the edge
Log storageVerbose logs of every requestSet retention, sample or filter what you truly need
Minimum instancesSet higher than neededReview actual usage, raise only before planned peaks

Habits that pay off

1

Use v2, zone redundant, with autoscale

It is the supported and best-performing design. Choose a sensible minimum and a generous maximum.

2

Terminate TLS with modern policies

Require TLS 1.2 or higher, store certificates in Key Vault, and use end-to-end TLS for sensitive data.

3

Enable the WAF and tune it

Start in detection mode, review the logs, add narrow exclusions, then prevent.

4

Write real health endpoints

Light, fast, and honest. Point custom probes at them.

5

Close the back doors

Allow backends to receive traffic only from the gateway.

6

Log, alert, and rehearse

Send diagnostics to a central workspace, alert on unhealthy hosts and error rates, and practise a failover or certificate renewal.

7

Manage as code

Review every change, keep environments aligned, and avoid manual drift.

Common mistakes, in plain words

  • Forgetting the management ports. A network security group that blocks the gateway management range leaves the gateway unable to update or report health.
  • Mismatched host names. The gateway, the backend, and the certificate disagree on the name, causing 502 errors or redirect loops.
  • Setting timeouts too short. Report generation or large uploads fail with 504 errors even though the backend would have finished.
  • Counting only on autoscale. Scale-out takes minutes, so large planned peaks need a raised minimum.
  • Ignoring the 413 limit. File uploads fail because of body size limits in the WAF or backend.
  • Not testing renewals. A renewed certificate may have a different chain or name, and the first real test happens in production.
  • Treating WAF logs as noise. They are the best early warning of real attacks and of broken legitimate flows.
  • Mixing environments. One shared gateway for test and production lets a test mistake affect real customers.
i
Tip

Write down three numbers that define your gateway: the peak requests per second, the peak throughput, and the peak number of new TLS connections per second. Almost every sizing and cost decision follows from them.

19Real-World Usage Scenarios

The scenarios below are illustrative composites that show how the ideas combine. The organisations are invented and the numbers are chosen to teach, not to describe any real company.

Scenario 1: An online store survives a sale day

A fashion retailer expects ten times its normal traffic during a one-day sale. A week earlier, the team load tests a staging gateway and finds that peak traffic needs about 200 capacity units. They raise the production minimum instances so roughly twenty instances are always ready, set the maximum much higher, enable the WAF in prevention mode after weeks of tuning, and move product images to a content delivery network. On the day, scale-out is barely needed, and the site stays fast. The following week, they lower the minimum again to save money.

Scenario 2: A hospital protects a patient portal

A regional hospital runs a patient portal containing sensitive information. The team uses end-to-end TLS with certificates stored in Key Vault, a WAF in prevention mode with custom rules limiting an admin path to office addresses, and rate limiting on the login page. Backends have no public addresses and accept traffic only from the gateway subnet. Logs go to a protected workspace with limited access and a defined retention period. The design supports their audit requirements, and the security team reviews WAF logs weekly.

Scenario 3: A software company modernises step by step

A software company has a ten-year-old application on virtual machines. It places a gateway in front and, over twelve months, replaces features with containerised services. Each time a feature is ready, the team adds a path rule sending that path to the new Kubernetes service. Customers never see a migration weekend, and a problem with a new service is fixed by switching one rule back.

Scenario 4: A global brand combines edge and regional layers

A travel brand serves visitors worldwide. It uses Azure Front Door at the edge for acceleration and failover, and a WAF-enabled Application Gateway in each of two regions. Each gateway accepts traffic only from Front Door, checking the Front Door identifier header. When one region has a problem, Front Door’s health checks notice and move visitors to the other region within minutes, and the team investigates calmly.

Scenario 5: An internal company portal

A manufacturing company hosts its employee portal and several internal tools. A single gateway with only a private frontend serves all of them through multi-site listeners. The gateway is reachable from offices and remote staff through the company’s private network. A wildcard certificate and consistent security headers apply to every tool, and the platform team manages the gateway as code for all application teams.

Scenario 6: Fixing a mysterious outage

A support engineer is paged because customers see 502 errors. She opens the backend health view and sees every server unhealthy with a certificate name mismatch message. A change the previous afternoon had updated the backend certificate to a new name. The team updates the host name setting in the backend settings, health returns within a minute, and errors disappear. The post-incident review adds an alert for unhealthy hosts and a check in the release process that tests probes after any certificate change.

What these scenarios share

In each case, the gateway is used as a place to apply consistent rules once instead of repeating them everywhere: encryption, routing, security, and visibility. Successful teams plan capacity before peaks, close the back doors, treat certificates and health checks as first-class concerns, and keep their gateway configuration in reviewed code.

20Frequently Asked Questions

Short answers to the questions beginners ask most often.

Q1What is the difference between Application Gateway and a normal load balancer?
A normal load balancer works at the connection level and does not read web requests. Application Gateway works at the web request level, so it can route by host name and path, handle HTTPS centrally, rewrite headers, and run a web application firewall.
Q2Should I choose v1 or v2?
Choose v2. It offers autoscaling, zone redundancy, a static address, rewrites, and Key Vault integration, and the older v1 generation has been scheduled for retirement. If you still have a v1 gateway, plan a migration and check Microsoft’s current guidance for dates and tools.
Q3Do I need the WAF flavour?
If your application is reachable from the internet, yes, in almost every case. It blocks common attacks and abusive bots and gives you valuable logs. For purely internal applications, evaluate the risk and your organisation’s policy.
Q4Can one gateway serve several websites?
Yes. Multi-site listeners let one gateway and one public address serve many host names, each with its own certificate, rules, and backends. You can also use path-based rules to split one site across several backend pools.
Q5Why do I get 502 Bad Gateway errors?
The gateway could not get a valid answer from a backend. Check backend health first. The usual causes are failing health probes, mismatched host names or certificates, backend network rules blocking the gateway subnet, or an application that is not actually running.
Q6Why do I get 504 Gateway Timeout errors?
The backend took longer to respond than the request timeout in the backend settings. Either speed up the operation or raise the timeout to a realistic value, and consider moving long jobs to background processing.
Q7Does the gateway see my visitors’ real IP addresses?
The backend sees the gateway’s address as the connection source, but the gateway adds the original visitor address in the X-Forwarded-For header. Configure your application or web server to read that header.
Q8How does autoscaling work, and is it instant?
You set a minimum and a maximum number of instances, and Azure adds or removes instances as load changes. Scale-out takes a few minutes, so for planned spikes raise the minimum in advance.
Q9What are capacity units?
A capacity unit is the measure of load used for scaling and billing in v2. It blends connections, throughput, and compute use. Approximate documented figures are 2,500 persistent connections, 2.22 Mbps of throughput, or one compute unit, whichever dimension is highest.
Q10What is the difference between TLS termination and end-to-end TLS?
With termination, the gateway decrypts visitor traffic and sends it to the backend unencrypted. With end-to-end TLS, the gateway decrypts it to read the request and then encrypts it again for the backend. End-to-end TLS protects data on both stretches and suits sensitive workloads.
Q11Where should certificates be stored?
In Azure Key Vault, referenced by the gateway through a managed identity. This allows centralised control and automatic pickup of renewed certificates, which prevents most expiry outages.
Q12Why is my WAF blocking legitimate users?
A harmless request probably looks like an attack to one of the rules. Find the rule in the firewall log, then add a narrow exclusion for that field or path. Do not disable the WAF entirely.
Q13Can Application Gateway work across several regions?
A single gateway is regional. For multi-region designs, deploy a gateway in each region and place a global service such as Azure Front Door or Traffic Manager in front to route visitors to a healthy region.
Q14Do I need a separate subnet for the gateway?
Yes, a dedicated subnet used only by gateways of the same generation. A size of /24 is the common recommendation for v2, giving enough addresses for scaling and updates.
Q15Can the gateway be used with Kubernetes?
Yes. The Application Gateway Ingress Controller configures a gateway from Kubernetes ingress definitions, and Application Gateway for Containers is a newer service designed specifically for Kubernetes traffic. Check current documentation for features and availability.
Q16How do I stop people bypassing the gateway?
Remove public access from the backends and allow inbound traffic only from the gateway subnet, using network security groups, private endpoints, or access restrictions. When Front Door sits in front, also restrict the gateway to Front Door’s address range and verify its identifier header.
Q17Is session affinity a good idea?
Only as a bridge for applications that store visitor state inside one server. It causes uneven load and lost sessions on failure. Prefer stateless servers with shared storage for visitor state.
Q18How can I reduce the cost?
Share one gateway across several applications, right-size the minimum instances, stop non-production gateways when idle, offload static content to a content delivery network, keep backends in the same region, and set a sensible log retention period.

21Summary and Key Takeaways

You began with the problem of too many visitors and too many servers, and you now have a complete map of one of Azure’s most important web services: what it is, how it works inside, how to configure it, secure it, scale it, watch it, and fix it.

Think of Application Gateway as the reception desk of a large hotel, run by a professional service. It reads each request, decides where it belongs, checks it for danger, speaks the right language of encryption, watches the health of every department, and brings in extra staff when the lobby fills. You supply the rules, and Azure supplies the staff. The better your rules and your preparation, the calmer your busiest day will be.

Key Takeaways

  • It is a regional Layer 7 load balancer. Application Gateway reads web requests, so it can route by host name and path, redirect, rewrite, handle HTTPS, and run a WAF, all inside your virtual network. Use v2, since v1 is being retired.
  • Know the building blocks. Frontend IP, listener, routing rule, backend settings, backend pool, and health probe form a chain. Designing from left to right prevents most misconfigurations.
  • It is a go-between, not a wire. Visitors and servers have two separate connections, so forwarded headers and host name settings matter. Most redirect loops and login problems come from host mismatches.
  • Handle TLS deliberately. Choose termination or end-to-end encryption by risk, require modern TLS versions, and keep certificates in Key Vault with a managed identity to avoid expiry outages.
  • Health probes keep failures invisible. Write light, honest health endpoints, and remember that most 502 errors trace back to probes, certificates, host names, or network rules.
  • Protect in layers. Start the WAF in detection mode, tune with narrow exclusions, then prevent. Close back doors so backends accept traffic only from the gateway, and combine the gateway with DDoS protection, private networking, and logging.
  • Plan capacity, choose the right service, and manage as code. Autoscaling takes minutes, so raise minimums before planned peaks. Pair the gateway with Front Door for global needs, use Load Balancer for non-web traffic, and keep all configuration in reviewed code.