Azure Firewall vs NSG — when to use each
- Azure Firewall is best for centralized, large-scale policy management, logging, and Layer 7 (application) filtering such as FQDN or URL-based rules.
- NSGs are best for Layer 4 filtering (IP protocol, port, source/destination address prefixes) applied to subnets, NICs, or individual VMs.
- You can (and often should) use both: Azure Firewall for broad, centralized policies and NSGs for subnet- or NIC-level exceptions and microsegmentation. Even if the firewall allows traffic, NSGs can still block it at the subnet or NIC level.
NSGs operate at OSI Layer 4 (transport layer). They do not inspect Layer 7 (application) payloads — use Azure Firewall or application-aware appliances for L7 filtering.
Core NSG concepts
- Filtering: NSGs allow or deny traffic based on source/destination IPs, protocols, and ports.
- Rule set and priority: Each rule has a numeric priority. Lower numbers are evaluated before higher numbers (priority 100 is evaluated before 400). User-created priorities range from 100–4096. Azure default rules use high numeric values (65,000+). Defaults cannot be removed but can be overridden by user rules with a higher priority (lower numeric value).
- Association: NSGs can be associated with subnets or network interfaces (NICs), and a single NSG can be attached to multiple resources.
- Evaluation: Subnet- and NIC-level NSG rules are evaluated independently. For traffic to succeed, all NSGs that apply to the traffic path must allow it.

NSG rule attributes (brief)
- Service: Use predefined or custom services (e.g., HTTP, HTTPS, SSH, RDP) which map to ports.
- Port range: Single ports, comma-separated lists, or ranges (e.g.,
80,80,443,1024-65535). - Priority: Numeric priority for evaluation order (user range
100–4096, defaults are65,000+). - Action:
AlloworDeny.
How subnet vs NIC evaluation works (summary)
- Inbound traffic from the internet → Subnet NSG evaluated first (if present) → then NIC NSG.
- Outbound traffic initiated from a VM → NIC NSG evaluated first → then Subnet NSG.
- A deny in any evaluated NSG blocks the traffic. A traffic path is allowed only if all evaluated NSGs permit it.
Demonstration setup and walkthrough
The demo uses a PowerShell script (nsg-prep-infra.ps1) that creates the resource group, virtual networks, and four VMs (workload-a-vm-1/2 and workload-b-vm-1/2). Confirm your subscription context before running the script.
Example PowerShell session (trimmed):

Checking the NSG attached to a VM
- The prep script attached an NSG to each VM NIC that permits SSH (port 22) so you can connect.
- Default NSG rules (AllowVNetInBound, AllowAzureLoadBalancerInBound, DenyAllInBound, etc.) appear in every NSG and cannot be removed. User rules can override these defaults by using a lower numeric priority.
Default NSG rules cannot be deleted. To change behaviour, create user rules with a higher priority (lower numeric value) — e.g., priority
100 overrides default rules in the 65,000+ range.Install a web server on workload-a-vm-1
SSH example (from your client):Opening port 80 with an NSG inbound rule
From the VM’s Networking blade, add an inbound security rule:- Source:
Any - Service:
HTTP - Action:
Allow - Priority:
100(or another appropriate value) - Name:
Allow-HTTP
Testing cross-subnet connectivity and overriding default allow rules
By default, Azure allows VNet traffic (via default AllowVNetInBound/OutBound rules). You can create a user deny rule that blocks traffic from a specific source CIDR and has a higher priority (lower numeric value) than default rules. Example — before deny rule (from workload-b-vm-1):192.168.2.0/24 with priority 100:

Creating an NSG and associating it with a subnet
Steps:- Create a new NSG (example name:
block-ssh-nsg). - Add an inbound rule to Deny SSH (TCP port
22) fromAny. - Associate the NSG with a subnet (for example, subnet B).



Verify SSH is blocked by the subnet NSG
Attempting to SSH to any VM in the subnet after the subnet-level deny:Viewing inbound rules (default + custom)
You can view the combined default and custom inbound rules in the NSG’s Inbound Security Rules blade.
Augmented NSG rules
Augmented NSG rules let you consolidate multiple ports and IP ranges into a single rule to simplify management:- Multiple ports: comma-separated (e.g.,
22,80,443) - Port ranges: hyphen (e.g.,
10000-20000) - Multiple IP ranges: comma-separated CIDRs (e.g.,
192.168.1.0/24,192.168.3.0/24)
Wrap-up and best practices
- NSGs provide efficient Layer 4 controls for Azure resources at subnet and NIC scopes.
- Use subnet NSGs for common, per-subnet rules and NIC NSGs for per-VM overrides.
- A deny at any evaluated level blocks traffic; traffic is allowed only if all evaluated NSGs permit it.
- For application-aware filtering, centralized logging, and policy at scale, combine NSGs with Azure Firewall or rely on Azure Firewall alone where appropriate.
- Use Application Security Groups (ASGs) to group VMs logically and reference those groups in NSG rules for cleaner, scalable policies.
Links and references
- Network security groups overview — Microsoft Docs
- Azure Firewall documentation — Microsoft Docs
- Create, change, or delete an NSG — Microsoft Docs