Openvpn – Bypass VPN for HTTP/HTTPS traffic on Ubuntu

httpopenvpnubuntu-14.04vpn

I have an Ubuntu 14.04 machine that all it's outgoing traffic is through a VPN, and I'm required to make sure that HTTP and HTTPS traffic don't go through the VPN.
I've looked into static routing but it seems to handle only layer 3.
How should I approach this setting? Thanks.

Best Answer

In order to route packets destined to specific ports via a different default gateway you need to mark those packets using iptables and then route them via a different route table.

So, first create a new route table with default gateway your local gateway (not your VPN gateway)

ip route add table 4 default via 192.168.0.1

Then mark the packets you need based on the destination ports.

iptables -t mangle -A PREROUTING -p tcp --dport 80  -j MARK --set-mark 4
iptables -t mangle -A PREROUTING -p tcp --dport 443 -j MARK --set-mark 4

Finally route those marked packets via the newly created route table.

ip rule add fwmark 4 table 4

I havent' tested the commands above so they may need a little tweaking.

Related Topic