I redirected traffic for port 80 to 8080 on my machine with

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

It works fine for all the world except my own machine. I am a developer and I need to redirect port 80 to 8080 for myself.

My IP is 192.168.0.111

My web server runs on port 8080

I wish to open website from http://192.168.0.111/ instead of http://192.168.0.111:8080/ from same machine where server runs.

share|improve this question
    
Excuse me for the abberation, but what is the purpose of forwarding port 80 to 8080? – ma11hew28 Nov 10 '14 at 5:10
    
@mattdipasquale, normal users can't access port 80 so you couldn't run a web service like python flask as a normal user. – Christian Mar 29 '16 at 10:14
    
Why don't you just bind the web server to port 80? – David Foerster May 23 '16 at 13:01
2  
i'd guess its because non-root user cannot bind to ports 80/443 and he doesnt want to run his web service as root.. – Pavel K. Oct 11 '16 at 10:35
up vote 48 down vote accepted

You need to use the OUTPUT chain as the packets meant for the loopback interface do not pass via the PREROUTING chain. The following command should work:

sudo iptables -t nat -A OUTPUT -o lo -p tcp --dport 80 -j REDIRECT --to-port 8080
share|improve this answer
1  
Does port 80 required to be gloabally accesible? I tried your solution, my port number 8080 is accessible but 80 is not hence it did not worked. @heemayl – Alper Jul 28 '17 at 23:39

Instead of the iptables, You could try: sudo ssh -gL 80:127.0.0.1:8080 localhost

share|improve this answer
2  
That is an option but it is not exactly what I want because I already have web server on port 80. I will prefer to do it with iptables and keep web server on port 80 running. I guess I just have to apply rule to different step instead of PREROUTING – Max Apr 8 '14 at 10:45
    
Yes - this will cause a port conflict if you have something listening that you are port forwarding to as Max suggested. The above answer is the more general case. – cgseller Nov 15 '16 at 20:24

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.