- 14 min read

OPNsense WireGuard VPN Gateway Failover with IVPN

Intermediate Tutorial OPNsense WireGuard IVPN

In this tutorial, we will set up tiered WireGuard Vpn
Virtual Private Network - A secure connection method that creates an encrypted tunnel between your device and a remote server, hiding your internet activity and location while providing access to resources as if you were on a private network.
gateway failover on OPNsense. We start with a single WireGuard tunnel to a Vpn
Virtual Private Network - A secure connection method that creates an encrypted tunnel between your device and a remote server, hiding your internet activity and location while providing access to resources as if you were on a private network.
provider, add two more tunnels to servers in different locations, and wire them into a gateway group so traffic automatically fails over when a server goes down.

Along the way, we will work with WireGuard peers and instances, interface assignments, gateways, gateway groups, outbound NAT, and policy routing – the core building blocks of multi-WAN on OPNsense.

What We Are Building

We have OPNsense acting as a WireGuard road warrior Vpn
Virtual Private Network - A secure connection method that creates an encrypted tunnel between your device and a remote server, hiding your internet activity and location while providing access to resources as if you were on a private network.
server. Mobile clients connect to OPNsense over WireGuard, and a second WireGuard instance on OPNsense forwards their internet traffic through a commercial Vpn
Virtual Private Network - A secure connection method that creates an encrypted tunnel between your device and a remote server, hiding your internet activity and location while providing access to resources as if you were on a private network.
provider (IVPN in our case). This is sometimes called Vpn
Virtual Private Network - A secure connection method that creates an encrypted tunnel between your device and a remote server, hiding your internet activity and location while providing access to resources as if you were on a private network.
chaining.

The problem: if the single Vpn
Virtual Private Network - A secure connection method that creates an encrypted tunnel between your device and a remote server, hiding your internet activity and location while providing access to resources as if you were on a private network.
exit server goes down, our mobile clients lose internet access entirely. We will fix this by adding two fallback servers and creating a gateway group with three priority tiers:

Phone ──WireGuard──▢ OPNsense ──▢ Tier 1: New York (primary)
                                  ──▢ Tier 2: Los Angeles (failover)
                                  ──▢ Tier 3: Miami (failover)

Prerequisites

Before we begin, you need:

We will refer to the existing tunnel as wg1 throughout. If yours has a different name, substitute accordingly.

Step 1: Gather Your Server Details

First, we need the connection details for our two fallback servers. We want to pick servers with low load in geographically close locations.

IVPN publishes their server list at https://api.ivpn.net/v5/servers.json. Open that URL and find two WireGuard servers in your preferred locations. For each server, note down:

We also need to confirm the details of our existing tunnel. Open Vpn
Virtual Private Network - A secure connection method that creates an encrypted tunnel between your device and a remote server, hiding your internet activity and location while providing access to resources as if you were on a private network.
> WireGuard > Instances
and click the pencil icon on your existing instance. Note down:

We will use these values in the next steps. Close the dialog without changing anything.

Step 2: Add WireGuard Peers

Go to Vpn
Virtual Private Network - A secure connection method that creates an encrypted tunnel between your device and a remote server, hiding your internet activity and location while providing access to resources as if you were on a private network.
> WireGuard > Peers
and click the + button.

Fill in the fields for our first fallback server:

Field Value
Enabled Checked
Name IVPN_LA
Public Key (the public key from Step 1)
Allowed IPs 0.0.0.0/0
Endpoint Address us-ca6.wg.ivpn.net
Endpoint Port 51820
Keepalive 25

Click Save.

Now click + again and add the second fallback:

Field Value
Enabled Checked
Name IVPN_Miami
Public Key (the public key from Step 1)
Allowed IPs 0.0.0.0/0
Endpoint Address us-fl2.wg.ivpn.net
Endpoint Port 51820
Keepalive 25

Click Save, then click Save again at the bottom of the page.

Notice that we use hostnames rather than IPs for the endpoint address. Vpn
Virtual Private Network - A secure connection method that creates an encrypted tunnel between your device and a remote server, hiding your internet activity and location while providing access to resources as if you were on a private network.
providers occasionally change server IPs, but hostnames stay stable.

Step 3: Create WireGuard Instances

Go to Vpn
Virtual Private Network - A secure connection method that creates an encrypted tunnel between your device and a remote server, hiding your internet activity and location while providing access to resources as if you were on a private network.
> WireGuard > Instances
and enable Advanced Mode (toggle in the top-right corner).

Click + to add a new instance for our Los Angeles fallback:

Field Value
Enabled Checked
Name IVPN_LA
Private Key (same private key as your existing instance)
Listen Port 5565
Tunnel Address (same tunnel address as your existing instance)
Peers IVPN_LA
Disable Routes Checked
Gateway 172.26.0.9

Click Save.

Click + again for the Miami fallback:

Field Value
Enabled Checked
Name IVPN_Miami
Private Key (same private key as your existing instance)
Listen Port 5566
Tunnel Address (same tunnel address as your existing instance)
Peers IVPN_Miami
Disable Routes Checked
Gateway 172.26.0.17

Click Save, then Save again at the bottom.

A few things to notice here:

Now restart WireGuard to bring up the new tunnels. Go to Vpn
Virtual Private Network - A secure connection method that creates an encrypted tunnel between your device and a remote server, hiding your internet activity and location while providing access to resources as if you were on a private network.
> WireGuard > General
and toggle the service off, then on again.

Step 4: Assign Interfaces

Go to Interfaces > Assignments. In the dropdown at the bottom, you should see the new WireGuard devices (wg2, wg3). Select each one, give it a description, and click + to add it.

We name them IVPN_LA and IVPN_Miami to match our instances.

Now click on each new interface in the left sidebar to configure it:

Field Value
Enable Checked
IPv4 Configuration Type None
IPv6 Configuration Type None

Click Save, then Apply changes. Repeat for the second interface.

After applying, both interfaces should show as UP in Interfaces > Overview. The WireGuard tunnel handles the actual addressing – we set the interface to β€œNone” because OPNsense only needs it as a routing target.

Step 5: Create Gateways

Go to System > Gateways > Configuration and click Add.

Create a gateway for the Los Angeles tunnel:

Field Value
Name IVPN_LA_GW
Interface IVPN_LA
Address Family IPv4
IP address 172.26.0.9
Far Gateway Checked
Monitor IP (see note below)

Click Save.

Add another for the Miami tunnel:

Field Value
Name IVPN_Miami_GW
Interface IVPN_Miami
Address Family IPv4
IP address 172.26.0.17
Far Gateway Checked
Monitor IP (see note below)

Click Save, then Apply changes.

The IP address must match the Gateway value we set in the WireGuard instance (Step 3). Far Gateway must be checked because the gateway IP is not on the same subnet as the interface – it is a virtual construct for routing.

About Monitor IP: OPNsense pings this IP through the tunnel to determine if the gateway is alive. Good choices:

The monitor IP field only accepts IP addresses, not hostnames. Resolve your server hostname if needed: dig +short us-ca6.wg.ivpn.net.

Step 6: Create the Gateway Group

Go to System > Gateways > Group and click + Add.

Field Value
Group Name IVPN_Failover

Now assign tiers to each gateway:

Gateway Tier
(your existing NY gateway) Tier 1
IVPN_LA_GW Tier 2
IVPN_Miami_GW Tier 3

Set the Trigger Level to Packet Loss or High Latency.

Click Save.

The tier system works like this: OPNsense uses Tier 1 exclusively as long as it is healthy. When Tier 1 goes down (detected by the gateway monitor), OPNsense regenerates its Firewall
A network security system that monitors and controls incoming and outgoing network traffic based on predetermined security rules. Firewalls act as a barrier between trusted internal networks and untrusted external networks.
rules to route through Tier 2. If Tier 2 also fails, it falls to Tier 3. When a higher-priority tier recovers, traffic shifts back automatically.

Step 7: Add Outbound NAT Rules

Go to Firewall
A network security system that monitors and controls incoming and outgoing network traffic based on predetermined security rules. Firewalls act as a barrier between trusted internal networks and untrusted external networks.
> NAT > Outbound
.

If the mode is set to β€œAutomatic”, switch it to Hybrid outbound NAT rule generation and click Save, then Apply changes.

Now click Add to create a rule for each new tunnel. We need NAT so that traffic leaving through the tunnel appears to come from the tunnel’s interface address.

For the Los Angeles tunnel:

Field Value
Interface IVPN_LA
TCP/IP Version IPv4
Protocol any
Source address *(your

Vpn
Virtual Private Network - A secure connection method that creates an encrypted tunnel between your device and a remote server, hiding your internet activity and location while providing access to resources as if you were on a private network.
clients alias)* | | Destination address | any | | Translation / target | Interface address |

Click Save. Repeat with IVPN_Miami as the interface.

Click Apply changes.

You should already have an equivalent rule for your existing wg1 interface. The new rules follow the same pattern – one per tunnel, each NATing client traffic to the tunnel’s own address.

Step 8: Update the Policy Route

This is the final step. Go to Firewall
A network security system that monitors and controls incoming and outgoing network traffic based on predetermined security rules. Firewalls act as a barrier between trusted internal networks and untrusted external networks.
> Rules
and find the interface where your existing VPN policy route lives (for us, this is under WireGuard).

Find the rule that matches your Vpn
Virtual Private Network - A secure connection method that creates an encrypted tunnel between your device and a remote server, hiding your internet activity and location while providing access to resources as if you were on a private network.
clients alias with an inverted destination of RFC1918 (private addresses). Click the pencil icon to edit it.

Scroll down to the Gateway dropdown. Change it from the single gateway to IVPN_Failover (the gateway group we created in Step 6).

Click Save, then Apply changes.

The policy route now points at the gateway group instead of a single gateway. When all three gateways are healthy, traffic flows through Tier 1 as before. The difference is that now there is a safety net.

Verifying the Setup

Let’s confirm everything is wired up correctly.

Check the tunnels are alive. SSH into OPNsense and run:

sudo wg show

You should see three interfaces (wg1, wg2, wg3), each with a recent handshake (within the last few minutes) and transfer counters ticking up.

Check the gateways are online. Go to System > Gateways > Status. All three gateways should show a green status with round-trip times.

Check the policy route. Run:

sudo pfctl -sr | grep route-to

You should see your rule with route-to pointing at the Tier 1 gateway. This is correct – OPNsense only loads the active tier into the Firewall
A network security system that monitors and controls incoming and outgoing network traffic based on predetermined security rules. Firewalls act as a barrier between trusted internal networks and untrusted external networks.
rules. When Tier 1 fails, this rule is automatically regenerated to point at Tier 2.

Check the NAT rules. Run:

sudo pfctl -sn | grep wg

You should see NAT entries for all three WireGuard interfaces.

What We Built

We started with a single WireGuard VPN tunnel that was a single point of failure. We now have three tunnels across three locations, managed by a gateway group that handles failover automatically. The dpinger daemon on OPNsense continuously monitors each gateway and rewrites the Firewall
A network security system that monitors and controls incoming and outgoing network traffic based on predetermined security rules. Firewalls act as a barrier between trusted internal networks and untrusted external networks.
rules when one goes down.

Clients connect to OPNsense the same way they always did – nothing changes on the client side. The failover is entirely server-side and transparent.