DMZ: Where to Put a Server Reachable from the Internet
Why a published server should not stay in the LAN, a real DMZ versus the router “DMZ host”, rules between zones, port forwarding, an OPNsense example, testing, common mistakes, reverse proxy and VPN.
I need to make a small web server reachable from the Internet, or the NAS at home or in the office. The quickest solution is to leave it where it is, in the LAN, and open a port with port forwarding. It works, but what happens on the day that server is compromised? Let's see why a published service belongs in a DMZ, what a real DMZ is and why the “DMZ host” of home routers is not one, which rules are needed, how to configure it with OPNsense and which mistakes to avoid.
The problem: a published server inside the LAN
A service reachable from the Internet receives connections from anyone: legitimate users, but also automated scans that tirelessly look for vulnerable applications, weak passwords and exposed admin panels. Sooner or later, unpatched software or a wrong setting can let an attacker take control of the server.
If the server sits in the LAN, together with the PCs, printers and NAS devices, that control becomes a foothold inside the internal network. The server and the PCs are connected to the same switch and the same subnet: traffic between them does not go through the firewall, which therefore cannot see or filter it. From the compromised server the attacker can explore the network, try to reach shared folders, remote desktops and databases, and move from one system to the next (lateral movement).
Port forwarding is not wrong in itself: it is the tool for publishing a service, as explained in the article on NAT and port forwarding. The problem is the destination of the forward: a system exposed to the Internet must not live in the same network as the systems we want to protect.
What a DMZ is
A DMZ (demilitarized zone) is a separate network segment for the systems that must be reachable from outside: web servers, mail servers, reverse proxies, VPN servers. The name comes from military language: a strip of land between two sides, watched from both.
In practice the DMZ is a third zone of the firewall, next to the WAN and the LAN, with its own subnet. The firewall can build it in two ways:
- with a dedicated physical interface: a firewall port connected only to the DMZ devices;
- with a dedicated VLAN (IEEE 802.1Q): the DMZ travels with its own tag over a trunk link to a managed switch, and the firewall treats it as an interface of its own.
Either way, every packet between the DMZ, the LAN and the Internet goes through the firewall and is checked against the rules. If the DMZ server is compromised, the attacker is in a zone from which the rules do not let them reach the LAN: the damage stays contained.
In our examples the LAN is 192.168.1.0/24, the DMZ is 192.168.20.0/24 with the firewall at 192.168.20.1 and the web server at 192.168.20.10, while the WAN interface has the public address 203.0.113.25 and an external client uses 198.51.100.44. These are private addresses (RFC 1918) and addresses reserved for documentation (RFC 5737).
A real DMZ and the “DMZ host” of home routers
Many home routers have a setting called DMZ or DMZ host. Despite the name, it creates no separate segment: it names a LAN device to which the router forwards all incoming connections that do not match another forward. In essence it is port forwarding of every port to a single host, which stays in the same network as the other devices.
The result is the opposite of a DMZ: the device is exposed on every port, including services nobody meant to publish, and if it is compromised it is already inside the LAN. The “DMZ host” often appears in guides for game consoles or cameras, but it should be avoided. When a service must be published, forward only the ports it needs; when the LAN must be protected, use a separate segment.
| Aspect | Real DMZ | Router “DMZ host” |
|---|---|---|
| Where the server sits | separate segment (dedicated interface or VLAN) | in the LAN, with the other devices |
| Ports reachable from the Internet | only those forwarded and allowed | every port not forwarded elsewhere |
| Traffic towards the LAN | filtered by the firewall, normally denied | unrestricted: same subnet |
| If the server is compromised | the damage stays in the DMZ | the attacker is already in the LAN |
Typical rules between zones
A DMZ is only as good as its rules. The principle is least privilege: allow only what is needed and keep everything else blocked. This is the starting matrix for our example.
| Flow | Rule | Reason |
|---|---|---|
| Internet → DMZ | published ports only, for example TCP 443 to 192.168.20.10 | it is the service offered to the public |
| Internet → LAN | denied | no internal service is published |
| DMZ → LAN | denied; only narrow exceptions (one host, one port) | a compromised server must not reach the PCs |
| LAN → DMZ | allowed for management and for using the service, for example TCP 22 and 443 | administrators work from the LAN |
| DMZ → Internet | limited: DNS and NTP to the firewall, HTTP and HTTPS for updates | reduces what a compromised server can download or contact |
| DMZ → firewall | only the services needed (DNS, NTP), never the management interface | the firewall is the most valuable target |
The firewall is stateful: once a connection is allowed, the replies come back without needing a rule in the opposite direction. The “LAN → DMZ allowed” rule therefore does not open the “DMZ → LAN” path: the server answers the administrator, but cannot open a connection to the LAN on its own. The general principles of rules are covered in the article on how a firewall rule works.
With IPv6 there is usually no NAT and the DMZ server has a global address, for example in the prefix 2001:db8:20::/64: the zones and the matrix stay the same, with rules that allow only the published ports to the server's IPv6 address.
NAT and port forwarding to the DMZ
With IPv4 the DMZ uses private addresses, so it is reached from the Internet through port forwarding, or destination NAT: TCP traffic arriving at 203.0.113.25 port 443 is forwarded to 192.168.20.10 port 443. The firewall applies the translation before filtering: the rule that allows the traffic in on the WAN has the server's internal address as its destination, not the public one.
Forward only the service's ports. A web server needs 443 (HTTPS) and, if automatic redirection from HTTP is wanted, port 80. Do not forward SSH port 22, admin panels or database ports. If the service is meant only for a few partners, restrict the source to their addresses.
An example with OPNsense
Here are the main steps with OPNsense. The menu names given are those of recent versions; menus may vary with the version, so it is worth checking them against the official documentation of the installed version.
- Interface. In Interfaces › Assignments assign the free port, which appears as OPT1, or a VLAN created beforehand. On the interface page enable it, rename it “DMZ” and give it the static address 192.168.20.1/24.
- Server addresses. DMZ servers usually have static addresses: 192.168.20.10 for the web server, with gateway and DNS 192.168.20.1.
- Port forwarding. In Firewall › NAT › Port Forward create the forward on the WAN interface, protocol TCP, destination WAN address port 443, to 192.168.20.10 port 443, with the associated filter rule. OPNsense adds a Pass rule to 192.168.20.10:443 in Firewall › Rules › WAN.
- DMZ rules. In Firewall › Rules › DMZ write the rules for traffic leaving the DMZ servers.
- LAN rules. In Firewall › Rules › LAN decide what the LAN may do towards the DMZ.
Two details are decisive. In OPNsense the rules of an interface apply to traffic inbound on that interface, that is, to packets entering the firewall from that network: an attempt by a DMZ server to reach a PC is blocked by a rule on the DMZ interface, not on the LAN. Rules are also evaluated from top to bottom and the first match wins; a new interface such as the DMZ starts with no rules, so it blocks everything until something is added.
Here is an example rule set for the DMZ interface. “DMZ net” and “DMZ address” are the DMZ subnet and the firewall's address on it; “This Firewall” means all of the firewall's addresses; PRIVATE_NETS is an alias created by the administrator with 10.0.0.0/8, 172.16.0.0/12 and 192.168.0.0/16.
Firewall › Rules › DMZ (evaluation order: from the top)
# Action Proto Source Destination Port Description
1 Pass TCP/UDP DMZ net DMZ address 53 firewall DNS
2 Pass UDP DMZ net DMZ address 123 firewall NTP
3 Block any DMZ net This Firewall any no firewall management
4 Block any DMZ net PRIVATE_NETS any no LAN or other internal networks
5 Pass TCP DMZ net any 80, 443 updates
(everything else: blocked by the default rule)
Order matters. Rules 1 and 2 come before rule 3, otherwise the block towards the firewall would also stop DNS and NTP. Rule 4 comes before rule 5: without it, “any” on ports 80 and 443 would also include the LAN's web servers and printers.
On the LAN, by contrast, OPNsense usually creates a default rule that lets the LAN reach any destination, and therefore the whole DMZ. To limit access to management, add above the default rule a Pass rule from LAN net to 192.168.20.10 on ports 22 and 443, perhaps only from the administrators' PCs, and a Block rule from LAN net to DMZ net.
Checking that the separation works
An untested rule is a guess. From the DMZ server, for example with Linux and the nc command, try to reach a LAN PC and then an allowed service. An expected result, with example addresses:
$ nc -zv -w 3 192.168.1.10 445
nc: connect to 192.168.1.10 port 445 (tcp) timed out: Operation now in progress
$ nc -zv -w 3 192.168.20.1 53
Connection to 192.168.20.1 53 port [tcp/domain] succeeded!
The first attempt times out with no reply, because the Block rule silently drops the packet; the second succeeds, because the firewall's DNS is allowed. With logging enabled on rule 4, the line for the blocked packet appears in Firewall › Log Files › Live View. From outside, over a mobile connection, check that only port 443 of the public address answers.
At home and in a small business
- At home, people who publish a NAS often do so to reach their own files from outside. In that case a VPN is almost always better than any publication. If a service really has to be public, for example a small website, the NAS holding the family photos is not the right place: a dedicated device in a DMZ is better.
- In a small business the DMZ hosts the web server, the reverse proxy or the mail gateway. If the firewall has few ports, the DMZ can be a VLAN on a managed switch, as explained in the article on VLANs, security and organisation; the link between firewall and switch becomes a trunk.
- Several servers in the DMZ can attack each other, because traffic inside the DMZ does not go through the firewall. When services carry different levels of risk, it is worth splitting them into several segments or restricting traffic with each server's own firewall.
Common mistakes
- An any-any rule from the DMZ. A Pass rule from DMZ net to any destination, added “to make everything work”, cancels the DMZ: the compromised server can reach the LAN again.
- Unrestricted access to the LAN database. A website in the DMZ that reads a database in the LAN through a rule to the whole LAN, or to every port of the server, opens a direct path to the data. If the link is essential, restrict it to one host, one port and one protocol; often it is better to place the database in a dedicated segment, separate from both the LAN and the DMZ.
- Management exposed to the Internet. SSH, the NAS panel or the firewall's web interface reachable from the WAN are among the first targets of scans. Management is done from the LAN or through a VPN.
- Using the router's “DMZ host”. It exposes every port of a device that remains in the LAN.
- A DMZ on paper only. A server with a DMZ address, but plugged into the LAN switch without a VLAN, shares the physical medium with the PCs; and a server with two network cards, one in the DMZ and one in the LAN, bypasses the firewall.
- Shared credentials. If the DMZ server uses the same passwords or the same domain accounts as the LAN, whoever compromises it can reuse them elsewhere.
- No updates and no logs. A DMZ limits the damage, but it does not make vulnerable software safe: regular updates and logs that someone reads are still needed.
The alternatives: reverse proxy and VPN
A reverse proxy is a server that receives client connections and forwards them to the application servers. Placed in the DMZ, it becomes the only exposed system: it handles the TLS certificates, can filter requests and can publish several sites on the same port 443, telling them apart by name. The application servers sit in another segment and accept connections only from the proxy, on specific ports.
Often, though, the right question is: does this service need to be public? If the users are few and known, such as employees or family, it is better to publish nothing and use a VPN: only one port is exposed, the VPN's, protected by cryptographic keys, and the services stay internal. A practical example is in the article on WireGuard. VPN users should also be treated as a zone of their own, with rules that allow only the services they need.
Conclusions
A server reachable from the Internet is the most exposed system in the network, and it must not sit next to the systems we want to protect. In practice:
- first ask whether the service needs to be public, or whether a VPN is enough;
- place published servers in a real DMZ, on a dedicated firewall interface or VLAN, never with the router's “DMZ host”;
- forward from the Internet only the service's ports, never the management ones;
- deny traffic from the DMZ to the LAN and to the firewall's management, limit traffic to the Internet and allow from the LAN only the management that is needed;
- test the rules with real attempts, keep logging enabled and keep the servers updated.
A DMZ does not make a server invulnerable, but it ensures that a single mistake remains a contained incident instead of becoming the compromise of the whole network.
The video
Further reading
- Book: LOCAL AREA NETWORKS (LAN) — Volume 5: LAN Security and Integration (English edition)
- Book: LOCAL AREA NETWORKS (LAN) — Volume 4: Routing, NAT, and Internet Access (English edition)
- Book: OPNsense® in Practice — Volume 1: The Foundations of a Professional Journey (English edition)
- Video course: Capire i Firewall con OPNsense (video course in Italian)
- Video course: VLAN con OPNsense®: progettazione e configurazione (video course in Italian)
- Related article: NAT and Port Forwarding: How a Private Network Talks to the Internet
- Related article: How a Firewall Rule Works
- Related article: VLANs: Why Separating Networks Improves Security and Organisation
- Related article: WireGuard VPN: Secure Access to the Office Network
- Specialization: Firewalls & Network Security
Technical sources
- NIST SP 800-41 Rev. 1 — Guidelines on Firewalls and Firewall Policy — Network architectures with firewalls and DMZs, least-privilege policies, inbound and outbound traffic filtering, rule management and testing.
- RFC 2663 e RFC 3022 — terminologia del NAT e NAT tradizionale — Address and port translation, destination NAT for published internal services.
- IEEE 802.1Q — Bridges and Bridged Networks (VLAN) — VLAN tags and trunk links: the DMZ can be a dedicated VLAN that the firewall treats as a separate interface.
- Documentazione ufficiale di OPNsense — Interfaces, Firewall Rules e NAT Port Forward — Assigning interfaces and VLANs, per-interface rules evaluated inbound and from top to bottom, default block, port forwarding with an associated rule; menus and options depend on the version.
- RFC 1918, RFC 5737 e RFC 3849 — Addresses used in the examples: private networks 192.168.1.0/24 and 192.168.20.0/24, documentation addresses 203.0.113.25 and 198.51.100.44, IPv6 prefix 2001:db8::/32.
Standards and official documentation available from the respective organisations.