I have a router with 1 network interface (eth0, public ip) and an alias for eth0:0, local ip

I want to redirect all incoming https (443) traffic to another server. I tried it with

iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 443 -m conntrack --ctstate NEW -j DNAT --to-destination and it works. All traffic coming to my public ip's ssl port is redirected to

But the main problem is this; now all https traffic from my local lan to the internet is also redirected to that local ip, eg. https://facebook.com and answers. How should I fix my configuration in order to make it work?


Just filter based on the destination address:

iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 443 -d $PUBLIC_IP -m conntrack --ctstate NEW -j DNAT --to-destination

If the public IP is dynamic, you will need to add hooks to the DHCP client script to update this rule when your IP address changes. You may also use IP sets to change the IP address to check without changing your rule.

Another less perfect solution is to use the addrtype module. This modules makes a check on the routing table to determine what type of routing entry the address has. the LOCAL type can be used to check if this IP is the host's.

iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 443 -m addrtype --dst-type LOCAL  -m conntrack --ctstate NEW -j DNAT --to-destination

Note that this solution also redirect connections to and or any IP address that your host have. You can exclude destination addresses by adding a simple ! -d match to the rule.

  • Thnx for your quick reply. How can I use -o with PREROUTING? At least iptables 1.4.12 does not accept it? But everything now works with --dst-type LOCAL :) – AnttiQ Aug 15 '13 at 9:42
  • @AnttiQ: Sorry i mean -d, not -o. Fixed. – BatchyX Aug 15 '13 at 12:13
  • Why is using addrtype module less perfect? This option sounds more reliable to me, because it doesn't need a separately defined $PUBLIC_IP which might be wrong (especially when using DHCP to get IP address). – Craig McQueen Jan 23 '17 at 6:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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