LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Networking
User Name
Password
Linux - Networking This forum is for any issue related to networks or networking.
Routing, network cards, OSI, etc. Anything is fair game.

Notices


Reply
  Search this Thread
Old 11-14-2019, 06:57 AM   #1
rylan76
Senior Member
 
Registered: Apr 2004
Location: Potchefstroom, South Africa
Distribution: Fedora 17 - 3.3.4-5.fc17.x86_64
Posts: 1,552

Rep: Reputation: 103Reputation: 103
iptables forwarding of all HTTP / port 80 traffic to different IP


Hi guys

I posted about this previously trying to do it with firewalld but could simply not get it to work right.

What I need is to have 172.16.1.1 forward any traffic on port 80 to 192.168.1.1:80 which is a different physical machine, and 172.16.1.2 (on the same machine, different NIC) forward any traffic on port 80 to 192.168.1.2:80 which is a different physical machine.

I have tried

Code:
ifconfig eth0 172.16.1.1
ifconfig eth1 172.16.1.2

iptables -F
iptables -t nat -F
iptables -t mangle -F
iptables -X

iptables -A INPUT -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT
iptables -I INPUT 1 -i lo -j ACCEPT
iptables -A INPUT -p tcp --dport 22 -j ACCEPT
iptables -A INPUT -m conntrack --ctstate INVALID -j DROP
iptables -A INPUT -j DROP

iptables -A OUTPUT -o lo -j ACCEPT
iptables -A OUTPUT -m conntrack --ctstate ESTABLISHED -j ACCEPT

iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
iptables -t nat -A POSTROUTING -o eth1 -j MASQUERADE

iptables -t nat -A PREROUTING -d 172.16.1.1 -p tcp -m tcp --dport 80 -j DNAT --to-destination 192.168.1.1:80

iptables -t nat -A PREROUTING -d 172.16.1.2 -p tcp -m tcp --dport 80 -j DNAT --to-destination 192.168.1.2:80
What happens is if 172.16.1.1 is hit from another machine, the webserver running on 127.0.0.1 on the test machine receives the traffic.

If 172.16.1.2 is hit from another machine, the webserver running on 127.0.0.1 on the test machine receives the traffic.

Obviously I'm missing something - how can I use iptables to forward traffic received on port 80 for a certain IP (on a machine with mutliple NICs and separate IPs on each) to another IP address on port 80 given which IP is hit with a HTTP request to port 80?

Thanks!

Last edited by rylan76; 11-14-2019 at 07:00 AM.
 
Old 11-15-2019, 02:55 AM   #2
rylan76
Senior Member
 
Registered: Apr 2004
Location: Potchefstroom, South Africa
Distribution: Fedora 17 - 3.3.4-5.fc17.x86_64
Posts: 1,552

Original Poster
Rep: Reputation: 103Reputation: 103
Ok, have managed to definitively solve this with IP tables.

These below commands allow me to do exactly what I need, e. g. a HTTP request to

172.16.1.1 - on eth0
172.16.1.2 - on eth1

gets forwarded to different machines on

192.168.1.1
192.168.1.2

depending on if 172.16.1.1 or 172.16.1.2 is hit with the HTTP request to port 80.

Code:
systemctl restart iptables
systemctl restart rsyslog
iptables -F
iptables -t nat -F
iptables -t mangle -F
iptables -X

iptables -t nat -A PREROUTING -d 172.16.1.1 -p tcp --dport 80 -j DNAT --to-destination 192.168.1.1:80
iptables -t nat -A POSTROUTING -p tcp -d 192.168.1.1 --dport 80 -j SNAT --to-source 172.16.1.1

iptables -t nat -A PREROUTING -d 172.16.1.2 -p tcp --dport 80 -j DNAT --to-destination 192.168.1.2:80
iptables -t nat -A POSTROUTING -p tcp -d 192.168.1.2 --dport 80 -j SNAT --to-source 172.16.1.2
The above code allows me to hit my "proxy" server on either its 172.16.1.1 or 172.16.1.2 NICs' IP addresses, and get the HTTP requests forwarded to 192.168.1.1 if 172.16.1.1 is hit, or to 192.168.1.2 if 172.16.1.2 is hit.

This is exactly what the ticket was for, so this is solved.

Regards

Stefan
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] IPtables : ssh port forwarding one port to another port issue routers Linux - Networking 7 08-07-2018 08:41 AM
Traffic shaping (limiting outgoing bandwidth of all TCP-traffic except FTP/HTTP) ffkodd Linux - Networking 3 10-25-2008 12:09 AM
forwarding ssl port to a different ssl port number on a different machine coal-fire-ice Linux - Networking 6 03-15-2007 12:30 PM
IPCHAINS port forwarding and IPTABLES port forwarding ediestajr Linux - Networking 26 01-14-2007 07:35 PM
port 25 forwarding iptables FC4, can't get it to pass traffic sahib2u Linux - Security 4 03-01-2006 10:00 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Networking

All times are GMT -5. The time now is 12:44 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration