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 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?

share|improve this question
    
Related serverfault.com/questions/211536/… –  msanford Jan 9 '13 at 14:49

1 Answer 1

up vote 3 down vote accepted

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.

share|improve this answer
    
It worked like a charm putting the machine IP in place of localhost in your rule above. Thanks! –  gws Jan 9 '13 at 14:41

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.