NAT, Proxies & Gateways
NAT, Proxies & Gateways
Every production cloud environment hides its internal address space from the public internet. Servers in a private subnet have no routable IPs, yet they can reach npm registries, pull container images from Docker Hub, and accept HTTPS traffic from millions of users. Three mechanisms make that possible: Network Address Translation (NAT), proxies, and gateways. This lesson dissects all three at the packet level and connects them to patterns you will configure daily as a DevOps engineer.
Network Address Translation (NAT)
NAT rewrites IP addresses (and TCP/UDP port numbers) in packet headers as they pass through a router or firewall. There are two production-critical variants.
SNAT — Source NAT
Source NAT replaces the source IP in outbound packets. Your private instance at 10.0.1.42 wants to reach 54.230.10.1. Before the packet leaves your VPC, the NAT device rewrites the source to its own public IP (say 52.1.2.3) and records the mapping in a connection tracking table. When the reply arrives at 52.1.2.3, the device looks up the table, rewrites the destination back to 10.0.1.42, and forwards the packet inward. The external server sees only the public IP — it never learns the private address exists.
In AWS, the NAT Gateway performs SNAT for private subnets. In Linux, iptables does the same with a single rule:
MASQUERADE is convenient for dynamic IPs (home routers, spot instances) because it reads the current interface address automatically. For production NAT Gateways with fixed Elastic IPs, always use SNAT --to-source so behavior is deterministic after reboots.
DNAT — Destination NAT
Destination NAT replaces the destination IP. A packet arrives at your public IP on port 443, and the firewall rewrites the destination to an internal server at 10.0.2.80:8443 before forwarding. This is how bare-metal load balancers and port-forwarding rules work — and it is exactly what happens inside every cloud Network Load Balancer at the packet level.
nf_conntrack_count vs nf_conntrack_max before a production incident finds it for you.
Forward Proxies
A forward proxy sits between internal clients and the internet. Clients are configured to route all outbound requests through it. The proxy makes the actual connection on their behalf, returning the response. From the origin server's perspective, the request came from the proxy.
Forward proxies appear in large organisations for three reasons: egress control (only allow specific destinations), TLS inspection (decrypt, inspect for DLP, re-encrypt), and caching (reduce bandwidth costs for package registries). In cloud environments, they replace NAT Gateways when you need deep packet inspection or fine-grained URL-level policies.
no_proxy variable must always exclude the instance metadata IP (169.254.169.254) or your EC2/GCE instances will lose their IAM credentials.
Reverse Proxies
A reverse proxy sits in front of your backend servers. Clients talk to the proxy, believing it is the origin. The proxy forwards requests to one or more backends, collects the response, and sends it back. Clients never learn the backend addresses.
Nginx is the canonical reverse proxy in DevOps. A minimal HTTPS reverse proxy config:
The X-Forwarded-For header is critical: it carries the original client IP through the proxy chain so your application logs show real IPs, not the proxy's address. Without it, every request appears to originate from the proxy.
Gateways: API Gateway & NAT Gateway
The word "gateway" is overloaded in cloud networking. Two types matter most:
- Internet Gateway (IGW) — a 1:1 NAT between a public subnet's private IP and its Elastic IP. It enables instances with public IPs to be directly reachable. No bandwidth limits, no port translation.
- NAT Gateway — managed SNAT for private subnets. Instances in private subnets route their default route (
0.0.0.0/0) to the NAT Gateway, which SNATs outbound traffic to the gateway's EIP. Replies return and the gateway DNATs them back. Fully managed, scales to 100 Gbps automatically. - API Gateway — operates at Layer 7. It terminates HTTP/HTTPS, enforces authentication, rate-limits requests, and routes to downstream services (Lambda, EC2, containers). Functionally a reverse proxy plus auth layer plus request transformation.
Egress Patterns in Production
Large platforms combine all these pieces into tiered egress architectures. Private workloads never have public IPs. All outbound traffic flows through a centralised egress VPC (sometimes called a "transit VPC" or "egress VPC") that runs a managed NAT Gateway plus an IDS/IPS forward proxy. A single EIP per availability zone serves as the stable source address for all production traffic — third-party vendors whitelist those IPs in their firewalls.
CloudWatch: NatGateway ConnectionAttemptCount vs ConnectionEstablishedCount. The gap between them is your drop rate. Solution: spread Lambdas across multiple NAT Gateways in different AZs, or switch to a VPC Endpoint for AWS service traffic (no NAT needed at all).
Understanding where each mechanism operates — SNAT at the packet level, a forward proxy at the HTTP application level, a reverse proxy terminating TLS, an API Gateway enforcing auth — lets you choose the right tool and debug failures precisely. When a backend stops receiving traffic, the first question is: which layer broke? NAT conntrack full, proxy upstream unhealthy, gateway route missing, or certificate expired?