I am able to port forward using following commands:

iptables -t nat -A PREROUTING -p tcp -d XXX.XXX.XXX.XXX --dport 80 -j DNAT --to 192.168.161.31:80

iptables -t nat -A POSTROUTING -d 192.168.161.31 -j MASQUERADE

I wanted to use if it is possible to forward packets aimed at a domain name instead of an IP address like XXX.XXX.XXX.XXX.

For instance, can one do something like the following and make it work? Mine does not work!

iptables -t nat -A PREROUTING -p tcp -d XXX.XXX.XXX.XXX --dport 80 -j DNAT --to 192.168.161.31:80
iptables -t nat -A PREROUTING -p tcp -d www.example.com --dport 80 -j DNAT --to 192.168.161.32:80

Please notice that the preceding commands should forward to different machines.

link|improve this question
possible duplicate of How to create an Iptables rule using domain name – mailq Nov 24 '11 at 13:18
1  
Note: using DNS names with iptables is generally a very bad idea, even iptables man page states this. – Janne Pikkarainen Nov 24 '11 at 13:19
feedback

2 Answers

No it is not possible. Packets are never aimed at domain names at the TCP/IP level.

You'll need to use some sort of load balancer (apache with ProxyPass, for instance) if you need to redirect requests to another host/port.

link|improve this answer
feedback

No you can't do something like that. DNS names are not allowed in netfilter rules. They only apply on TCP/IP and ICMP levels.

link|improve this answer
feedback

Your Answer

 
or
required, but never shown

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