Implementing NSGs and Application Security Groups
This lesson explains how NSG rules are evaluated and combined, and how ASGs simplify design.
Learning outcomes
After completing this lesson, you will be able to:
- Explain priority and first-match evaluation.
- Describe how subnet and NIC NSGs combine.
- Use application security groups in rules.
- Identify the limits of NSGs.
How an NSG works
A network security group (NSG) holds rules that allow or deny traffic. It can be associated with a subnet, with a network interface, or with both. A rule has a priority, source, destination, port, protocol, direction and action. Rules are stateful: if an inbound connection is allowed, its return traffic is allowed automatically, with no outbound rule.
Rules are evaluated in order of priority, from the lowest number to the highest. Priorities range from 100 to 4,096, and processing stops at the first match, so a low-numbered deny hides a later allow. Every NSG also holds default rules that cannot be removed, at priorities from 65,000 to 65,500, as the following table shows.
| Direction | Default rule | Effect |
|---|---|---|
| Inbound | AllowVnetInBound | Allows traffic from the virtual network, which includes peered networks |
| Inbound | AllowAzureLoadBalancerInBound | Allows the load balancer's health probes |
| Inbound | DenyAllInBound | Denies everything else |
| Outbound | AllowVnetOutBound | Allows traffic to the virtual network |
| Outbound | AllowInternetOutBound | Allows traffic to the internet |
| Outbound | DenyAllOutBound | Denies everything else |
The default rules show two points that are worth remembering. Traffic within a virtual network is allowed by default, and outbound traffic to the internet is allowed by default. If segmentation or outbound control is required, these defaults must be overridden with custom rules at lower numbers.
NSG defaults
Combining subnet and NIC NSGs
If a subnet and a network interface both have an NSG, then both must allow a flow. The order depends on direction. For inbound traffic, the subnet NSG is evaluated first, then the NIC NSG. For outbound traffic, the NIC NSG is evaluated first, then the subnet NSG. If either one denies the flow, it stops. A person can therefore allow port 443 on a VM's NIC and still see failures, since the subnet NSG may be blocking it.
A simple design associates NSGs with subnets, and avoids NIC-level NSGs unless there is a reason. Two NSGs on the same path double the work of troubleshooting. Traffic within a subnet is also evaluated by that subnet's NSG, so a subnet NSG can isolate VMs from each other.
The following diagram shows the evaluation order in each direction.

Inbound evaluation order
Application security groups
Rules based on IP addresses are hard to read and they break when addresses change. An application security group (ASG) groups network interfaces under a name. A rule can then say that asg-web may reach asg-app on port 8443, whatever the addresses of the machines are. Adding a VM to the tier needs only an ASG membership, with no rule change.
ASGs have limits. All network interfaces in an ASG must be in the same virtual network as the first interface added, and an ASG used in a rule as both source and destination must be in the same network. They contain NICs only, so they cannot represent on-premises addresses, and they work inside NSG rules, not by themselves. The following table compares ways of naming a source or destination.
| Option | What it represents | Best for |
|---|---|---|
| IP address or CIDR | A fixed range | On-premises or partner networks |
| Service tag | A Microsoft-maintained range for a service | Azure services and the internet |
| Application security group | NICs that you group by role | Tiers of an application |
Naming sources and destinations
Designing rules
A sound rule set starts from least privilege. Allow only the flows that an application needs, deny the rest with a custom rule of your own, and leave gaps between priority numbers, such as 100, 200 and 300, so that later rules can fit between them. Do not expose management ports such as 3389 and 22 to the internet: use Azure Bastion or just-in-time access. Use augmented rules, which accept several addresses and ports in one rule, to keep rule sets short.
The following commands create an ASG and a rule that uses it. The values in capitals are placeholders.
az network asg create --resource-group RG --name asg-web
az network asg create --resource-group RG --name asg-app
az network nsg rule create --resource-group RG --nsg-name NSG --name allow-web-to-app \
--priority 200 --direction Inbound --access Allow --protocol Tcp \
--source-asgs asg-web --destination-asgs asg-app --destination-port-ranges 8443Limits of NSGs
NSGs are powerful, but they have limits. They work on addresses and ports, so they cannot see a domain name or inspect content. They are also local: each team usually manages its own, so a central security team has no guarantee that a rule stays in place. Azure Virtual Network Manager addresses that problem, as the next lesson explains, and Azure Firewall addresses filtering by FQDN.
Tracing a rule set
A VM's subnet NSG has an inbound rule at priority 100 that allows TCP 443 from the internet. The VM's NIC NSG has only the default rules. A client on the internet cannot connect on port 443. What is the cause, and how should you fix it?
Inbound traffic passes the subnet NSG, which allows it, and then the NIC NSG. The NIC NSG has no allow rule for internet traffic, so its DenyAllInBound default rule blocks it, because AllowVnetInBound does not match an internet source.
Either add an allow rule to the NIC NSG, or remove the NIC NSG if it is not needed, which is the simpler design. Raising the priority of the subnet rule has no effect, because each NSG is evaluated separately, and both must allow the flow.
A rule at priority 300 allows port 443, and a rule at 200 denies the same traffic. What is the result?
Show answer
Denied. Lower numbers run first, and the first match ends evaluation.
Exam Tips
Remember for the exam
- Lower priority numbers are processed first, and the first match wins.
- Inbound runs subnet then NIC; outbound runs NIC then subnet.
- All NICs in an ASG must be in the same virtual network.
- NSGs are stateful, so return traffic needs no rule.