Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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 EXCEPT localhost which gets a socket error, connection refused. If I point localhost to 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… – msanford Jan 9 '13 at 14:49
up vote 4 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
I've tried this rule but nothing changes... Which can be the problem? I've written this post:… – Andrea Catania Dec 28 '15 at 14:33

Your Answer


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.