Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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

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 192.168.1.2:443 and it works. All traffic coming to my public ip's ssl port is redirected to 192.168.1.2.

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 192.168.1.2 answers. How should I fix my configuration in order to make it work?

share|improve this question

1 Answer 1

up vote 0 down vote accepted

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 192.168.1.2:443

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 192.168.1.2:443

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

share|improve this answer
    
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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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