Port forwarding on MikroTik

Port forwarding on MikroTik looks simple: add one NAT rule. In practice, many people end up with a rule that does not work from outside, works only from the internet side, or breaks when they open the service from their own LAN. This guide explains how NAT and the firewall work together, and how to build a port forward that works from every direction.

How a port forward works

A port forward has two parts:

  1. A destination NAT (dst-nat) rule that rewrites the destination address of incoming packets, so they go to a device inside your network.
  2. A firewall that lets the forwarded traffic through. With the baseline firewall from our firewall guide, traffic that has been through a dst-nat rule is allowed, and other new connections from the internet are dropped. If your firewall is stricter, you need a matching accept rule in the forward chain.

The basic rule

Example: forward port 8080 on the router’s public side to a web server at 192.168.88.50 on port 80.

/ip firewall nat
add chain=dstnat action=dst-nat protocol=tcp dst-port=8080 in-interface-list=WAN \
    to-addresses=192.168.88.50 to-ports=80 comment="web server forward"

Use in-interface-list=WAN instead of a fixed public address, so the rule keeps working when your provider changes your IP address. All addresses and ports here are examples.

[ADD: a screenshot of your NAT rule with its packet counters]

Make it work from your own LAN (hairpin NAT)

A frequent complaint: the forward works from the internet but not from inside your network when you use the public address or the dynamic DNS name. The reason is that the server sees the request coming from a local address, and answers it directly, bypassing the router. The client then receives a reply from a different address than it asked and drops it.

The fix is called hairpin NAT. Use a dst-nat rule that matches any packet addressed to the router itself, and add a masquerade rule so the replies go back through the router:

/ip firewall nat
add chain=dstnat action=dst-nat protocol=tcp dst-port=8080 dst-address-type=local \
    to-addresses=192.168.88.50 to-ports=80 comment="forward, works from WAN and LAN"
add chain=srcnat action=masquerade protocol=tcp src-address=192.168.88.0/24 \
    dst-address=192.168.88.50 dst-port=80 comment="hairpin NAT"

Use these two rules instead of the first basic rule. Note that dst-address-type=local also matches the router’s own LAN address, so connecting to the router’s LAN address on port 8080 will also be forwarded. If that is a problem, use a fixed dst-address or a more specific condition.

Things that make a forward fail

  • The server’s default gateway is not the MikroTik. If the server sends replies through another router, the connection breaks. The server must use the MikroTik as its gateway, or the traffic must be masqueraded.
  • The server’s own firewall blocks the connection. Check it on the device.
  • Your provider blocks the port or has no public IP for you. With carrier-grade NAT, incoming connections cannot reach your router. Check that the router’s WAN address is a real public address.
  • Testing from the wrong place. Test from outside your network, for example from a phone on mobile data, not from your LAN, unless you have set up hairpin NAT.
  • Rule order. NAT rules are checked from top to bottom. A broader rule above yours can catch the traffic first.
  • The router’s own service uses the same port. Do not forward ports that the router needs for itself, such as the ones used by WinBox or the web interface.
  • IPv6. Port forwarding is NAT for IPv4. IPv6 devices need firewall rules instead of NAT.

How to debug

  1. Look at the NAT rule counters: /ip firewall nat print stats. If the packet counter does not grow when you test, the traffic does not reach the rule.
  2. If the counter grows but the service does not respond, check the forward chain counters: /ip firewall filter print stats.
  3. Use /tool torch or the packet sniffer on the LAN interface to see whether packets arrive at the server and whether it answers.
  4. Check the server’s gateway and firewall.

Security: forward less, not more

  • Only forward the ports you really need.
  • Restrict the source with an address list, if you know who connects.
  • Avoid exposing remote desktop, databases and admin panels directly. Use a VPN instead, such as WireGuard from our WireGuard guide.
  • Keep the exposed service updated.
  • Changing the external port is not a security measure, because ports are scanned all the time.
[ADD: what you forward in your own network and how you secure it]

Checklist

  • A dst-nat rule matches the right protocol and port and points to the right internal address.
  • The forward chain allows the traffic, or the baseline firewall passes dst-nat traffic.
  • Hairpin NAT is set up if you need access from the LAN.
  • The server uses the MikroTik as its gateway.
  • Tested from outside, with rule counters checked.
  • Only necessary ports are exposed.

Leave a Reply

Your email address will not be published. Required fields are marked *