Table of Contents
- π― What Youβll Learn in This Blog
- π§ Core Security Concepts
- β οΈ Insecure Way: Exposing VM Web App
- π’ NSG at NIC vs Subnet
- βοΈ Azure Web App Service
- π VM Hosting vs App Service Hosting
- π‘οΈ How to Secure App Service
- π° Analogy β The Mansion Outside the Gate
- π¦ Application Gateway β The Smart Security Guard
- π§ͺ Demo: Protect App Service with Application Gateway
- π‘ What We Understood from the Demo
- π Better Approach β Private Endpoint
- π Final Architecture
- β Key Takeaways
π― What Youβll Learn in This Blog
In this guide we will:
- Understand how Network Security Groups (NSG) protect Azure resources
- Learn how IP addresses, protocols, and ports control network traffic
- Compare security between:
- Web app hosted in Virtual Machine
- Web app hosted in Azure App Service
- Explore why NSG cannot be used for App Service
- Implement security using:
- β Service Endpoints
- β Private Endpoints
- β Access Restrictions
- Protect apps using Azure Application Gateway + WAF
By the end, youβll know how to secure an App Service web app like an enterprise architect π‘οΈ.
π§ Core Security Concepts
π Network Security Group (NSG)
An NSG is a set of rules that decide:
- Who can access a VM or subnet
- Which traffic should be allowed or denied
NSG can be attached to:
- π₯οΈ A VMβs Network Interface (NIC)
- π’ An entire Subnet
π How Data Travels on a Network
To send data from System A β System B we need:
- IP Address β where to send
- Protocol β how to send (TCP/IP)
π¬ What Are Ports?
Just like physical ports:
- USB
- HDMI
- Ethernet
Computers also have virtual ports identified by numbers.
| Port | Purpose |
|---|---|
| 80 | HTTP |
| 443 | HTTPS |
| 22 | SSH |
| 3389 | RDP |
| 25 | SMTP |
NSG rules filter traffic using:
- Source IP
- Destination IP
- Port
- Protocol
π‘ Think of NSG as a digital security guard checking:
βWhere are you coming from? Which door are you using?β
β οΈ Insecure Way: Exposing VM Web App
To make a VM-hosted app public, people usually:
- Add NSG rule to allow port 8080
- Disable VM firewall
β This is extremely risky and NOT recommended.
π’ NSG at NIC vs Subnet
Option 1 β Attach NSG to NIC
- Works per VM
- Hard to manage at scale
Option 2 β Attach NSG to Subnet
- Centralized control
- Best practice
π Note:
NSG cannot be applied to the entire VNetβonly to subnets or NICs.
βοΈ Azure Web App Service
Azure App Service lets you host apps without managing VMs.
β No OS patching
β No IIS management
β Auto scaling
β Managed platform
Butβ¦
β You donβt control its VNet or subnet
β NSG is NOT applicable
β App is public by default
π VM Hosting vs App Service Hosting
| Feature | VM Hosted | App Service |
|---|---|---|
| Infrastructure control | Full | Limited |
| NSG support | Yes | No |
| Public by default | No | Yes |
| Management effort | High | Low |
So we need different security methods for App Service.
π‘οΈ How to Secure App Service
Two main approaches:
- β Service Endpoint + Access Restriction
- β Private Endpoint + Access Restriction
And on top of that:
π Application Gateway + WAF
π° Analogy β The Mansion Outside the Gate
- VNet = Gated community
- Subnet = Building
- App Service = Mansion outside the gate
By default β anyone can enter the mansion π±
We must:
- Restrict public access
- Allow only members of our VNet
- Create a private tunnel
π¦ Application Gateway β The Smart Security Guard
Application Gateway is:
- Layer 7 load balancer
- Web traffic inspector
- Security filter
Capabilities
β
URL-based routing β Direct traffic to different back-end resources based on the requested URL
β
Multi-site hosting β Host multiple websites behind a single gateway
β
SSL termination β Handle HTTPS encryption at the gateway level
β
WAF protection β Block common web attacks using Web Application Firewall
Key Components of Application Gateway
1. Front-end IP
This is the public or private IP address exposed by the Application Gateway.
All external users connect to this IP first.
2. HTTP Listener
The listener receives HTTP or HTTPS requests from users and passes them to routing rules for further processing.
3. Back-end Pool
This contains the actual resources where web apps are hosted, such as:
- Virtual Machines running IIS
- Azure App Service web apps
- Or a combination of both
These resources remain protected behind the Application Gateway and are not directly exposed to the internet.
4. Routing Rules
Routing rules decide which back-end resource should handle a particular request based on:
- URL path
- Host name
- Listener configuration
5. Backend Settings
These settings define:
- Whether traffic to the backend should be HTTP or HTTPS
- Port number
- Session affinity
- Health probe configuration
π§± Architecture
Internet β Application Gateway β App Service
NOT β Internet β App Service directly
π§ͺ Demo: Protect App Service with Application Gateway
π οΈ Step 1 β Create Application Gateway
To secure the App Service web app, we first deploy an Application Gateway that will act as the single, controlled entry point from the internet to our application.
Basic Details
While creating the Application Gateway, provide the following:
- Name β for example:
appgw-webapp - Region β same region as the App Service
- Tier β Standard v2 or WAF v2 (recommended for security)
- Virtual Network β the VNet that the Application Gateway will trust and operate within

π‘ The selected VNet is important because only resources inside this VNet can communicate privately with the Application Gateway.
Frontend Configuration
In the Frontend tab, choose:
- Public IP address β for internet-facing applications
- Private IP β only for internal applications

Since our web app must be accessed from the internet, we select Public IP.
Backend Pool Configuration
In the Backend tab:
- Choose App Service as the backend target
- Select the App Service created earlier
β This configuration tells the gateway:
βForward incoming requests to this App Service.β

Routing Rule Configuration
The routing rule defines how traffic flows through the gateway.
It has two main parts:

- Listener β receives incoming requests
- Backend Target β forwards requests to the destination
Listener Settings Explained
The listener controls how the gateway accepts traffic.
- Frontend IP β public IP exposed by the gateway
- Protocol β HTTP or HTTPS
- Port β 80 for HTTP, 443 for HTTPS
- Listener Type β Basic or Multi-site

For this demo we used:
- Protocol: HTTP
- Port: 80
- Listener Type: Basic
π Use Multi-site listener when hosting multiple websites behind one gateway.
1οΈβ£ Listener (Port 80) β How Users Talk to Application Gateway
The listener defines how users on the internet connect to the Application Gateway. When we choose HTTP on port 80, we are saying that public users will access the gateway using a normal web request like http://22.22.22.22. At this stage, the App Service is not involved yetβthe listener only handles traffic between the user and the gateway.
Backend Target Settings Explained
This section links the listener to the App Service.

1. Backend Pool
Select the pool containing the App Service:
π backendpoolappservice
This means all requests from the listener will be sent to this App Service.
2. Backend Settings
Key values used:
- Backend protocol: HTTPS β App Service requires secure communication
- Port: 443 β default HTTPS port
- Trusted certificate: Yes β App Service uses Microsoft-issued certificates
2οΈβ£ Backend Settings (Port 443) β How Gateway Talks to App Service
After receiving the request, the Application Gateway must forward it to the App Service. Azure App Service only accepts HTTPS traffic on port 443, so the backend settings use protocol HTTPS and port 443. This means there are two separate connections: one from the user to the gateway on port 80, and another secure connection from the gateway to the App Service on port 443.

3. Host Name Override (Critical Setting)
Enabled options:
- β Override with new host name β Yes
- β Pick host name from backend target
Why this is important?
3οΈβ£ Host Name Override β Why It Is Required
Think of the App Service like a person whose real name is βmyapp.azurewebsites.net.β
When a user visits the Application Gateway IP, the gateway originally calls the app like:
βHey 22.22.22.22, give me the website!β
But the App Service replies:
βThatβs not my name β I donβt recognize you!β β
When we enable Host Name Override, the gateway changes the message to:
βHey myapp.azurewebsites.net, give me the website!β
Now the App Service says:
βYes, thatβs me!β β
and it returns the page correctly.
So Host Name Override simply makes the gateway call the App Service by its real domain name instead of the gateway IP.
App Service expects requests with its original domain such as:
myapp.azurewebsites.net
Without host name override, the request comes with the gateway IP and App Service may return:
β 404 error
β Host not recognized
π‘ This setting ensures Application Gateway sends the correct host header to the App Service.
Now the app is reachable via:
π Application Gateway public IP
π οΈ Step 2 β Add Service Endpoint
Before we can restrict access to the App Service, the Application Gateway subnet must be authorized to talk to Microsoft Web services.
This is done using a Service Endpoint.
π‘ If we skip this step and try to add access restrictions first, Azure will show:
β βNo service endpoint is present for this subnet.β

1οΈβ£ Open the Virtual Network of Application Gateway
- Go to the Virtual Network where your Application Gateway is deployed
- From the left menu, select Service endpoints
This page shows which Azure platform services are allowed to be accessed from this VNet.
2οΈβ£ Add the Microsoft.Web Service Endpoint
- Click + Add
- In the Service dropdown, select:
π Microsoft.Web
This option represents Azure App Service and other web-related PaaS services.
3οΈβ£ Select the Application Gateway Subnet
- Choose the Subnet in which your Application Gateway is located
- Confirm and save the configuration
This tells Azure:
βDevices inside this subnet (Application Gateway) are allowed to securely access Azure Web App services.β
What This Step Actually Does
After adding the service endpoint:
- The App Service can now recognize the Application Gateway subnet
- Traffic from this subnet is treated as trusted Azure backbone traffic
- We are allowed to create access restriction rules referencing this subnet
Without this:
Before we can restrict access to the App Service, the Application Gateway subnet must be authorized to talk to Microsoft Web services.
This is done using a Service Endpoint.
π‘ If we skip this step and try to add access restrictions first, Azure will show:
β βNo service endpoint is present for this subnet.β
1οΈβ£ Open the Virtual Network of Application Gateway
- Go to the Virtual Network where your Application Gateway is deployed
- From the left menu, select Service endpoints
This page shows which Azure platform services are allowed to be accessed from this VNet.
2οΈβ£ Add the Microsoft.Web Service Endpoint
- Click + Add
- In the Service dropdown, select:
π Microsoft.Web
This option represents Azure App Service and other web-related PaaS services.
3οΈβ£ Select the Application Gateway Subnet
- Choose the Subnet in which your Application Gateway is located
- Confirm and save the configuration
This tells Azure:
βDevices inside this subnet (Application Gateway) are allowed to securely access Azure Web App services.β
What This Step Actually Does
After adding the service endpoint:
- The App Service can now recognize the Application Gateway subnet
- Traffic from this subnet is treated as trusted Azure backbone traffic
- We are allowed to create access restriction rules referencing this subnet
Without this:
- App Service cannot be locked down to the gateway
- Access restriction configuration will fail
π οΈ Step 3 β Block Direct Access to App Service
Right now, the web app hosted in App Service is publicly accessible by default.
Anyone on the internet can open the app directly using:
π https://myapp.azurewebsites.net
Our goal is:
β Users must NOT access the App Service directly
β Users should access it ONLY through the Application Gateway
To achieve this, we configure Access Restrictions in the App Service.
1οΈβ£ Check Current Access Status
- Open your App Service in the Azure Portal
- Go to the Networking blade
- Under Inbound traffic configuration, you will see:

Public network access: Enabled with no access restrictions
This means the web app is currently open to the entire internet, which is not secure.
2οΈβ£ Change Public Access Mode
- Click on Public network access
- Select the option:

π Enabled from select virtual networks and IP addresses
This setting tells Azure:
βAllow access only from specific VNets or IP addresses, and block everyone else.β
3οΈβ£ Add an Access Restriction Rule
Now we create a rule that allows traffic only from the Application Gateway subnet.
- Open Access Restrictions
- Click + Add

Enter the following details:
- Type: Virtual Network
- Virtual Network: the VNet where Application Gateway is deployed
- Subnet: Application Gateway subnet
- Description: e.g.,
restrictaccessappservice
- Click Save
What This Configuration Achieves
After applying this rule:

- β Direct access to
https://myapp.azurewebsites.netβ will be BLOCKED - β
Access through
http://<Application-Gateway-IP>β will be ALLOWED

Because now:
Only traffic coming from the trusted Application Gateway subnet is permitted.
Step 4 β Add Access Restriction
In App Service:
- Networking β Public Access
- Allow only:
- Selected VNet
- Gateway subnet
π§ͺ Result:
- Direct App Service URL β BLOCKED
- Gateway URL β WORKS β
π‘ What We Understood from the Demo
When creating the Application Gateway, we place it inside a specific VNet and subnet, and this subnet becomes the network that the gateway trusts.
Next, we go to that same subnet and enable a Service Endpoint for Microsoft.Web. This step allows resources inside that subnetβmainly the Application Gatewayβto securely reach the Azure App Service over the Azure backbone network.
After enabling the service endpoint, we configure Access Restrictions in the App Service to allow traffic only from this VNet/subnet. As a result:
- Application Gateway resides in a trusted subnet
- That subnet is authorized to communicate with App Service
- App Service accepts traffic only from that subnet and blocks all other public access
π° Analogy Explanation
Think of it like this:
- The VNet/subnet is a walled housing community
- The Application Gateway is the security guard at the gate
- The App Service is a mansion located outside the wall
Step 1 β Trust the Guard
We first tell the system:
βPeople coming from this walled community can be trusted to visit the mansion.β
Step 2 β Give a Special Pass
By adding the Service Endpoint, we give members of that community a valid pass to reach the mansion through a secure path.
Step 3 β Lock the Mansion
Inside the mansion (App Service) we set up a rule:
βOnly people with that pass β meaning traffic from the Application Gateway subnet β are allowed to enter.β
Everyone else from the public street is blocked π«
π Better Approach β Private Endpoint
Problem with Service Endpoint
It still uses public IP internally.
Private Endpoint = Underground Tunnel
Benefits:
- Uses Azure backbone
- No public IP involved
- Most secure option
Steps for Private Endpoint
- Create Private Endpoint for App Service
- Use separate subnet (no service endpoint)
- Disable Public Network Access
π Final Result
App can ONLY be accessed via Application Gateway
π Final Architecture
β Internet β Application Gateway (WAF)
β Gateway β Private Endpoint
β Private Endpoint β App Service
β Direct internet β App Service
β Key Takeaways
- NSG works for VMs, NOT App Service
- App Service is public by default
- Use:
- Service Endpoint
- Private Endpoint
- Access Restrictions
- Application Gateway
π Enterprise Best Practice
Never expose App Service directly to internet

Leave a Reply