LinuxQuestions.org
Help answer threads with 0 replies.
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 06-01-2012, 09:11 PM   #1
tquang
Member
 
Registered: Jul 2010
Posts: 44

Rep: Reputation: 0
Port redirecting with IPtables


I have 1 server is still running with web service and reverse proxy service. My server also have 2 IPs.

Web service with port: 80
Reverse proxy service with port: 6060
IP: 1.2.3.4 and 5.6.7.8

So, I want config with iptables to redirect port, example:
Code:
iptables -t nat -A PREROUTING -p tcp --dport 80 ! -s 1.2.3.4 -j REDIRECT --to-ports 6060
iptables -t nat -A PREROUTING -p tcp --dport 80 ! -s 5.6.7.8 -j REDIRECT --to-ports 6060
But, problem will occured: if first run config like that, second rule not work. Because first rule detect and redirect to: 1.2.3.4:6060
Yes, it's time second rule not effective.

I don't want to re-config web service to listen other port.

Thank all read.

Last edited by tquang; 06-01-2012 at 09:15 PM.
 
Old 06-02-2012, 03:34 PM   #2
Ser Olmy
Senior Member
 
Registered: Jan 2012
Distribution: Slackware
Posts: 3,334

Rep: Reputation: Disabled
Quote:
Originally Posted by tquang View Post
So, I want config with iptables to redirect port, example:
Code:
iptables -t nat -A PREROUTING -p tcp --dport 80 ! -s 1.2.3.4 -j REDIRECT --to-ports 6060
iptables -t nat -A PREROUTING -p tcp --dport 80 ! -s 5.6.7.8 -j REDIRECT --to-ports 6060
The first rule says "redirect incoming traffic where the destination port is 80 and the source address is not 1.2.3.4 to port 6060 on this system". As 1.2.3.4 is one of the addresses assigned to that same system, the rule does not make sense. It will apply to all incoming traffic, as you've discovered.

You mention that you're running a web server on port 80. Should this server be accessed directly rather than through the proxy service? If so, and if 1.2.3.4 is assigned to eth0 and 5.6.7.8 to eth1, this should work:
Code:
iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 80 ! -d 1.2.3.4 -j REDIRECT --to-ports 6060
iptables -t nat -A PREROUTING -i eth1 -p tcp --dport 80 ! -d 5.6.7.8 -j REDIRECT --to-ports 6060
Remember, you don't have to worry about locally generated traffic in the PREROUTING chain, as such packets never traverse it but instead go straight to the OUTPUT chain.

Last edited by Ser Olmy; 06-02-2012 at 03:35 PM.
 
  


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
Redirecting subdomain's port to another (local) port. e-San Linux - Networking 13 12-19-2011 04:52 PM
IPTABLES port redirecting to SQUID imoulas Linux - Server 1 07-12-2011 03:14 PM
[SOLVED] iptables REDIRECTing TheCrow33 Linux - Networking 1 12-07-2010 07:44 PM
[SOLVED] correct/safe iptables rules for redirecting port 80 to 8080 sundoe Linux - Security 3 07-13-2010 08:00 PM
redirecting a port on the same host with iptables nacio Linux - Networking 4 05-23-2008 01:15 PM

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

All times are GMT -5. The time now is 11:23 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