Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I have a test VDS box with real IP. If i start web server on physical interface port 80, it can be opened from another computer via it's ip address (and default port 80):

python -m SimpleHTTPServer 80

But if i try to reditect port 80 from physical interface eth0 to loopback 127.0.0.1 port 8080 i can connect it from another computer on port 8080, but can't connect on port 80, it's just endless 'connecting'. Seems like no redirection occurs:

iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 80 -j DNAT --to-destination 127.0.0.1:8080
python -m SimpleHTTPServer 8080

What i'm doing wrong? :(

P.S. Binding server on '127.0.0.1:8080' produce same result, but it does not matter since server running on '0.0.0.0:8080' will accept connection redirected to '127.0.0.1:8080'. AFAIK. :(

iptables -L result:

Chain INPUT (policy ACCEPT)
target     prot opt source               destination         

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination         

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination

iptables -t nat -L result:

Chain PREROUTING (policy ACCEPT)
target     prot opt source               destination         
DNAT       tcp  --  anywhere             anywhere             tcp dpt:http to:127.0.0.1:8080

Chain INPUT (policy ACCEPT)
target     prot opt source               destination         

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination         

Chain POSTROUTING (policy ACCEPT)
target     prot opt source               destination

netstat -nlp result:

Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      676/sshd        
tcp6       0      0 :::22                   :::*                    LISTEN      676/sshd        
Active UNIX domain sockets (only servers)
Proto RefCnt Flags       Type       State         I-Node   PID/Program name    Path
unix  2      [ ACC ]     STREAM     LISTENING     7964     600/acpid           /var/run/acpid.socket
unix  2      [ ACC ]     STREAM     LISTENING     6590     1/init              @/com/ubuntu/upstart
unix  2      [ ACC ]     SEQPACKET  LISTENING     6760     231/udevd           /run/udev/control
unix  2      [ ACC ]     STREAM     LISTENING     7030     345/dbus-daemon     /var/run/dbus/system_bus_socket

ifconfig -a result:

eth0      Link encap:Ethernet  HWaddr 00:16:3e:da:1a:98  
          inet addr:5.14.223.181  Bcast:5.14.223.255  Mask:255.255.255.0
          inet6 addr: fe80::140:3eff:febe:201a/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:371264 errors:0 dropped:59 overruns:0 frame:0
          TX packets:2093 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:17377545 (17.3 MB)  TX bytes:214428 (214.4 KB)
          Interrupt:25 

lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:16436  Metric:1
          RX packets:10 errors:0 dropped:0 overruns:0 frame:0
          TX packets:10 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:552 (552.0 B)  TX bytes:552 (552.0 B)
share|improve this question
    
What are your chain policies? Type iptables -L and iptables -t nat -L –  Halfgaar Dec 28 '12 at 18:23
    
@Halfgaar Empty before i enter iptables command. One rule after i enter it. –  Eye of Hell Dec 28 '12 at 18:31
    
I meant policies, not rule. Is it ACCEPT or DROP? –  Halfgaar Dec 28 '12 at 19:15
    
@Halfgaar All policies are 'ACCEPT' –  Eye of Hell Dec 28 '12 at 20:18
    
There must be a DROP going on, because otherwise it would give a 'connection error' right away. But instead it's 'connecting' all the time. Could you post the output of iptables -L, iptables -t nat -L and `netstat -lnp'? –  Halfgaar Dec 29 '12 at 9:27

3 Answers 3

The last time I use a DNAT rule like that, I also had to put in an SNAT rule on POSTROUTING to make it work correctly.

share|improve this answer
    
Maybe you can hint how second rule with SNAT look like? Unfortunately, i'm not very good with iptables :( –  Eye of Hell Dec 28 '12 at 18:21
    
Aren't you confused with when you have a masquerading router? To make a port-forward to an internal machine available on a NAT router's WAN IP from within the LAN as well, you indeed need an SNAT rule. –  Halfgaar Dec 28 '12 at 18:22
    
It's been long enough since I did SNAT/DNAT stuff that I'm useless as to what the rule should look like. –  John Dec 28 '12 at 18:28
    
I think he means this, but that's not relevant here. –  Halfgaar Dec 28 '12 at 19:14

What if you do this:

iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 8080 -j REDIRECT --to-port 80  --match comment --comment "Explain."
share|improve this answer
    
I need to add this rule replacing my own or alongside with it? It seems to map 8080 -> 80, how it can help? –  Eye of Hell Dec 28 '12 at 18:24
    
I meant replacing your rule. And the remap, isn't that what you want? –  Halfgaar Dec 28 '12 at 19:15
    
Same effect, not working :( –  Eye of Hell Dec 28 '12 at 19:25

Simply replace your rule with this one.

iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 80 -j REDIRECT --to-port 8080

It should work. This will redirect all your 80 port traffic on eth0 to 8080 port of localhost where you are running tomcat.


One other way of doing this without iptables (as I am not even sure whether it's possible with iptables) is using the xinetd services. To use this, install xinetd on your machine (usually it is by default installed). Create a file like this:

 # vim /etc/xinted.d/tomcat

Put this content in the file:

service tomcat
{
    socket_type             = stream
        wait                    = no
        user                    = root
        redirect                = 127.0.0.1 8080
        bind                    = 10.31.33.101 80
}

Just restart the xinted service.

 # service xinetd restart

And it will work like charm.

share|improve this answer
    
I have tried this - it redirects to 0.0.0.0:8080, not 127.0.0.1:8080. Application that is bound to 0.0.0.0:8080 is redirected, application that is bound to 127.0.0.1:8080 is not redirected. –  Eye of Hell Dec 29 '12 at 21:00
    
So, after reading a lot of online help/tutorials/blogs, I am not able to do it. I am now not even sure whether this is even possible (though people are saying that they have done it). But, there is one other way of doing this, which I have edited in my answer. –  GeekRide Dec 30 '12 at 17:17

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.