Marcio Cunha

How to configure NAT and Hairpin NAT on MikroTik routers

Learn how to solve internal access issues to local servers using your MikroTik public IP with proper NAT and Hairpin NAT rules.

Marcio Cunha•5 min
Also available in:PortuguêsEspañol
Summary
  • Traditional NAT translates private local IP addresses into a valid public IP address for internet access.
  • Hairpin NAT fixes the issue where internal computers cannot reach a local server using the router public IP.
  • Destination NAT rules redirect incoming connections on public ports to the internal server IP address.
  • Source NAT rules masquerade internal traffic so the server responds directly back to the router.
  • Proper ordering of firewall and NAT rules in MikroTik prevents routing failures and packet bottlenecks.

Why internal computers fail to access local servers

Anyone managing corporate networks or structured homelabs has experienced this frustration: setting up a web, file, or database server on the local network, publishing it through the router using a public IP or dynamic DNS domain, and realizing that everything works seamlessly for external users while failing miserably for anyone connected to the same internal network. In practice, when an internal computer tries to access the corporate server using the external address, the connection simply hits a wall and times out. This happens because data packets leave the computer toward the public IP, the router forwards them out to the internet, but the return traffic never routes correctly back to the originating machine within the same local subnet.

To fully understand the problem, it helps to recall how traditional Network Address Translation works. The router acts as a universal translator between private internal IPs and the public IP provided by the ISP. When data packets head out to the web, the router swaps the internal sender IP with its own public IP. When the response arrives, it reverses the process. This mechanism is vital for conserving IPv4 addresses, yet it creates a logical trap whenever the origin and destination of a connection reside physically on the same local network, forcing the router to process traffic that theoretically shouldn't leave the building.

What is Hairpin NAT and how it solves the issue

Hairpin NAT, also known as NAT Reflection or Loopback NAT, resolves this exact routing anomaly. In practice, it forces the router to treat traffic originating from the local network and targeting its own public IP as if it came from the internet, triggering a new address translation. Without this rule, the internal computer sends a packet to the public IP, the server receives it, but notices the sender is on the same local network and tries to reply directly to the computer's local IP. The computer, expecting a response from the public IP it queried, drops the connection due to a packet mismatch.

When we apply Hairpin NAT on MikroTik, the router intercepts this internal access attempt, altering both the destination IP and the source IP. It makes the server believe the request came directly from the router, and upon responding, the router intercepts the reply and forwards it back to the client computer using the original public IP. Thus, communication happens transparently for both endpoints, allowing users to utilize the exact same external domain whether working from home, a café, or at their office desk. For readers looking to deepen their understanding of packet behavior and advanced diagnostics, it is worth checking out the technical guide on how to mirror network traffic with RSPAN and Wireshark, essential for inspecting the exact flow of such requests at the data link layer.

Step-by-step to create Destination NAT rules

Before implementing Hairpin NAT, we must ensure that classic port forwarding, known as Destination NAT or dst-nat, works properly for external internet traffic. This rule tells MikroTik what to do when a packet arrives at a public service port. Let us use as a practical example the publishing of an internal web server listening on port 443 with private IP 192.168.10.50. In the MikroTik RouterOS terminal, we execute the command to create this foundational firewall rule.

/ip firewall nat
add chain=dstnat action=dst-nat to-addresses=192.168.10.50 to-ports=443 \
    protocol=tcp dst-port=443 in-interface-list=WAN comment="Publish Internal Web Server"

In practice, this command line instructs the router to intercept all TCP traffic arriving through the internet-facing interface (identified by the WAN interface list) on port 443, rewriting the destination IP to our internal server address. It is crucial to ensure the in-interface-list parameter is correct to prevent external routing from interfering with internal rules. If your network utilizes segmented VLANs to separate critical sectors, you can review safety guidelines in how to isolate IoT traffic with a MikroTik VLAN, helping shield the corporate environment from unauthorized cross-network access. To explore these architectural concepts further in production, see our related technical analysis on How to test DDR4 RAM memory with an oscilloscope on the bench.

Implementing Hairpin NAT on MikroTik RouterOS

With Destination NAT configured for internet access, we must add the second rule that completes the loop for local network devices. Hairpin NAT requires two simultaneous actions: alongside redirecting the destination to the internal server IP, we must masquerade the packet source using the router's internal interface IP. This forces the server to reply to the router rather than directly to the client machine, ensuring the network flow closes correctly. The command below creates the specific masquerade rule for this scenario.

/ip firewall nat
add chain=srcnat action=masquerade protocol=tcp dst-address=192.168.10.50 \
    dst-port=443 out-interface=bridge-local comment="Hairpin NAT for Internal Server"

In this rule, we specify that any TCP traffic destined for our internal server IP on port 443, exiting through the local network interface (in this example, named bridge-local), must undergo source masquerading. It is essential that the out-interface parameter points precisely to the bridge or physical interface connecting the router to local network computers. Without this care, the router might apply masquerading in unintended directions, causing widespread internet connectivity failures across the business.

After applying the commands on MikroTik, the next critical step is validating whether access actually works from a machine located on the local network. Open your browser or use a command-line tool like curl pointing directly to the server public IP or domain. If the page loads or the connection establishes without a timeout, Hairpin NAT is operating flawlessly. If connection fails, check whether firewall rules in the Filter Rules tab are blocking forwarded traffic or if conflicting rules sit higher in the NAT table, as MikroTik processes rules strictly top-down.

Maintaining organized NAT rules in MikroTik prevents future headaches and simplifies network security audits. Always add descriptive comments to each rule and group published services logically. By mastering Hairpin NAT, you eliminate workaround solutions like editing local hosts files or deploying complex internal DNS servers just to solve edge routing problems. With a clean and well-tested configuration, your infrastructure gains robustness, flexibility, and seamless transparency for end users regardless of where they connect from.