I have a setup of multiple mail servers on my LAN. For example i have two machines where

  • machine A handles mails for domainA.com and username first letter a-l***@domainA.com on IP ,
  • machine B handles mails for m-z****@domainA.com on IP
  • machine C handles domainC.com on IP

I have configured MX enteries for both domains as mail.myMailHandlerDomain.Com mapped on my public ip.

So now i have limitation of portforwarding to one machine. I can only port forward 25,465, 587, 993, 995 to either machine A or machine B or machine C.

I am running Ubuntu server with postfix/dovecot setup. Due to my limited expertise in the field i am looking for solutions, but unable to grasp different concepts of mail relay and Nginx reverse proxy. I am seeking solution fits to my problem. Thanks !!

Update 1

Machine C in this scenario is actually backup mail server for a remote mail server.

  • Which kicks in whenever main mail server goes down.
  • I have domain name update script to modify MX enteries whenever my observer machine detects a node failure, so my current ip will start reciving mails for domainC.com.
  • But the limitation for my current network is i dont want waste money on extra IP addresses, so i have single IP address.

And i am splitting mails for domainA.com for load balancing.

  • I can't imagine what you think the usefulness of this kind of setup is. Why not just use a single email server?
    – joeqwerty
    Mar 18, 2017 at 18:03
  • @joeqwerty Security, Flexibility and optimal resource usage. As i dont have single powerful machine to host large data and computation power is also low (per machine). Mar 19, 2017 at 0:12
  • 1
    If machine C is a backup mailserver then it should be listed separately as MX with a higher priority value so that it gets picked last. For load balancing, you don't need to split mail up depending on localpart; just have two servers with the same MX priorities and the mail will be about equally split. Lastly, if you are unable to grasp concepts of mail relay, you shouldn't be doing this. You are doing it wrong. Hire someone knowledgeable to help you.
    – Jenny D
    Mar 19, 2017 at 8:52
  • @JennyD but still how am i going forward mails from public ip to internal ip ?? And then equally splitting means i need same storage capacity on both machines !! Respectfully, I understand what i am doing!! If you dont have a solution, no need to downvote !! Mar 19, 2017 at 9:09
  • 1
    You wrote yourself that you are unable to grasp different concepts of mail relay; I do you the courtesy of accepting your words as truth. Yes, you do need the same storage capacity on both machines. It's called redundancy. You need it.
    – Jenny D
    Mar 19, 2017 at 10:18

3 Answers 3


I assume using multiple internal servers is for load-balancing. (othervise at least postfix can handle all domains)

  • For incoming mail (MX) is simple use single input server and reroute mail to others if necessary. Or any of 3 svers can forvard not own messages to proper server, for LB. (For example with postfix: using mydestination, relay_recipient_maps, relay_domains, local_recipient_maps and/or virtual_mailbox_maps. May be useful parameters list is not complete. http://postfix.cs.utah.edu/STANDARD_CONFIGURATION_README.html)

  • With connections from external users (POP3/IMAP). I think some POP3/IMAP proxy nedeed which route user connections based on login. (For example as described http://wiki1.dovecot.org/HowTo/ImapProxy.)

  • Thank you for understanding the problem. But answer is too broad, not specific for my problem. I will accept it if you can correct it. Mar 19, 2017 at 9:16
  • 1
    Sorry. No time to dig deep in postfix/dovecot for now.
    – mmv-ru
    Mar 19, 2017 at 10:18
  • 1
    One more hint - transport_maps and regexp postfix.org/transport.5.html
    – mmv-ru
    Mar 19, 2017 at 10:42

Postfix can read configurations from a database... including domains to service, how to service them, and addresses on those domains.

While not perfect this is a good start - https://workaround.org/ispmail/jessie

Set up the machine that will get all mail initially - this is the one you'll forward to from your firewall, and this is the one your internal folks should use as their outgoing. On this machine, set up all users as simple aliases/forwards, forwarding the mail to the other machines, using a user@machine.example.com as the email address. Each of those machines can be configured to use mysql as well, though they'll only serve the one domain, etc.

  • buddy you lost me there !! But there was no need to downvote, so i am upvoting for trying and to keep it neutral. Mar 19, 2017 at 9:17

What I would do is to host both mailservers in a way that give you full dual stack support on both servers and no NAT. That would mean you need to get another two IPv4 addresses from your hosting provider and probably a routed /64 IPv6 prefix as well. If the hosting provider cannot provide that, I would move to another hosting provider.

If you insist on hosting two mx with only a single IPv4 address, I can still think of a way to make it work if you have a dual stack deployment. Each mx would have a routable IPv6 address on which they can receive email. Neither of them would receive emails directly using IPv4. Instead I would configure a third mx which is secondary for both domains and can receive email over both IPv4 and IPv6.

As long as the sender has IPv6 support emails will usually go directly to the final mx. If the sender only has IPv4 support mails will have to first go to the secondary mx which will then relay the email to the final mx.

Here is an example of what the DNS records for such a setup could look like:

example.com.      3600  IN  MX    10 mx1.example.com.
example.com.      3600  IN  MX    20 mx2.example.com.
mx1.example.com.  3600  IN  AAAA  2001:db8:1::4
mx2.example.com.  3600  IN  AAAA  2001:db8::2
mx2.example.com.  3600  IN  A

example.org.      3600  IN  MX    10 mx1.example.org.
example.org.      3600  IN  MX    20 mx2.example.org.
mx1.example.org.  3600  IN  AAAA  2001:db8:1::5
mx2.example.org.  3600  IN  AAAA  2001:db8::2
mx2.example.org.  3600  IN  A

Notice how the only difference between the IP addresses for the two domains is the AAAA record for mx1. Even though mx2.example.com. and mx2.example.org. are two different names they both resolve to the same host, and the only purpose of the mx on that host is to relay emails to mx1 for the appropriate domain.

Not the answer you're looking for? Browse other questions tagged or ask your own question.