Mikrotik Load Balancing Script
Channel: How to
Date: 2023-03-25

Mikrotik Load Balancing Script
/ip firewall mangle
add chain=prerouting in-interface-list=WAN action=mark-connection new-connection-mark=WAN1_conn passthrough=yes comment="Mark WAN1 Connections"
add chain=prerouting in-interface-list=WAN action=mark-connection new-connection-mark=WAN2_conn passthrough=yes comment="Mark WAN2 Connections"
add chain=prerouting connection-mark=WAN1_conn in-interface-list=LAN action=mark-routing new-routing-mark=to_WAN1 passthrough=no comment="Mark Routing to WAN1"
add chain=prerouting connection-mark=WAN2_conn in-interface-list=LAN action=mark-routing new-routing-mark=to_WAN2 passthrough=no comment="Mark Routing to WAN2"

/ip route
add dst-address=0.0.0.0/0 gateway=1.1.1.1 routing-mark=to_WAN1 check-gateway=ping comment="Route to WAN1"
add dst-address=0.0.0.0/0 gateway=2.2.2.2 routing-mark=to_WAN2 check-gateway=ping comment="Route to WAN2"

/ip firewall nat
add chain=srcnat out-interface-list=WAN action=masquerade comment="Source NAT to WAN1 and WAN2"

/ip firewall filter
add chain=input action=accept protocol=tcp dst-port=80 comment="Allow HTTP Traffic"

/ip route rule
add src-address=192.168.1.0/24 action=lookup routing-mark=to_WAN1 comment="Use WAN1 for LAN Traffic"
add src-address=192.168.1.0/24 action=lookup routing-mark=to_WAN2 comment="Use WAN2 for LAN Traffic"