Ok. I have a new batch of rules and another command.
I had to turn hairpinning on for the bridge (the clue was that tcpdump made everything magically work)
Both values can be found via ifconfig, list the lxcbr0 interface as the bridge and vethG9BNU8 as the port (I don’t get it, but it works)
brctl hairpin lxcbr0 vethG9BNU8 on
The test cases were:
lxc exec mail -- wget https://google.com
lxc exec mail -- wget https://mymaildomain.com
wget https://mymaildomain.com
All three finally succeeded with this configuration:
*nat
:PREROUTING ACCEPT [0:0]
:POSTROUTING ACCEPT [0:0]
# Inspired by @efries
# https://discourse.mailinabox.email/t/system-status-pages-shows-errors-when-port-forward-to-container/470
# http://blog.inetpeople.net/mail-in-a-box-with-lxd-container/
# To parse these rules, check out http://explainshell.com
# Test: from the browser, access https://$MYDOMAIN.com
# The following route incoming connections to the relevant ports to the LXC container
-A PREROUTING -d $MY_IP/32 -p tcp -m multiport --dports 25,53,80,443,587,993,995 -j DNAT --to-destination $LXC_IP
-A PREROUTING -d $MY_IP/32 -p udp --dport 53 -j DNAT --to-destination $LXC_IP
# The following route connections from the server to itself
# Interestingly, with the commented set of rules,
# wget https://127.0.0.1 times out, instead of connection refused
#-A OUTPUT -o lo -p tcp -m multiport --dports 25,53,80,443,587,993,995 -j DNAT --to-destination $LXC_IP
#-A OUTPUT -o lo -p udp --dport 53 -j DNAT --to-destination $LXC_IP
-A OUTPUT -o lo ! -s 127.0.0.0/8 -p tcp -m multiport --dports 25,53,80,443,587,993,995 -j DNAT --to-destination $LXC_IP
-A OUTPUT -o lo ! -s 127.0.0.0/8 -p udp --dport 53 -j DNAT --to-destination $LXC_IP
# Test: lxc exec mail -- wget https://$MYDOMAIN.com
# The following will "hairpin" connections back from the LXC containers
-A POSTROUTING -s 10.0.3.0/24 -d 10.0.3.0/24 -p tcp -m multiport --dports 25,53,80,443,587,993,995 -j MASQUERADE
-A POSTROUTING -s 10.0.3.0/24 -d 10.0.3.0/24 -p udp --dport 53 -j MASQUERADE
# Test: lxc exec mail -- wget https://google.com
# The following will disguise the containers outbound connections as originating from the server
-A POSTROUTING -s 10.0.3.0/24 -o eth0 -j MASQUERADE
# don't delete the 'COMMIT' line or these rules won't be processed
COMMIT
I wonder if there’s any more configuration I’ve forgotten about? I also need to repeat the rules for my ipv6 address.
I’m going to wipe the VPS and try loading up mailinabox again tonight, hopefully between the two of us, everything needed is documented. @JoshData, would you be interested in merging a PR if I wrote an install script for an LXC host server to prepare a container for mailinabox?