4

I want to run a simple python webserver on my ubuntu linode with the following command (it simply puts the current dir up on the port specified)

python -m SimpleHTTPServer 8080

I am using port 8080 to avoid using sudo to run on port 80

To make it accessible I redirect port 80 to port 8080 with the following command:

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

Now any computer can access my website giuliopizzini.com EXCEPT localhost which gets a socket error, connection refused. If I point localhost to giuliopizzini.com:8080 it works OK but the redirect fails.

I have firewall enabled but if I reset it with

sudo iptables -F

the behavior is exactly the same, so that does not seem to play a role here.

How can I make the redirect work on localhost as well?

gws
  • 145
  • 1
  • 4

1 Answers1

5

You need to add another rule for localhost redirection to work:

sudo iptables -t nat -A OUTPUT -d localhost -p tcp --dport 80 -j REDIRECT --to-port 8080

You need to include the destination IP you are using to connect such as localhost or any interface IP.

Locally-generated traffic does not hit the PREROUTING chain. It hits the OUTPUT chain in the nat table which is what the above rule is doing.

msanford
  • 1,427
  • 15
  • 27
Khaled
  • 35,688
  • 8
  • 69
  • 98
  • It worked like a charm putting the machine IP in place of `localhost` in your rule above. Thanks! – gws Jan 09 '13 at 14:41
  • I've tried this rule but nothing changes... Which can be the problem? I've written this post: http://serverfault.com/questions/745554/opensuse-port-forwarding-80-to-8080-not-working – Andrea Catania Dec 28 '15 at 14:33