Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

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

How can I forward requests coming in on port 80 to another port on the same linux machine?

I used to do this by changing nat.conf, but this machine that I'm using doesn't have NAT. What's the alternative?

share|improve this question

migrated from stackoverflow.com Oct 11 '11 at 23:11

This question came from our site for professional and enthusiast programmers.

    
No NAT whatsoever or just no nat.conf? Have you tried any iptables rules using NAT? – Ben Swinburne Oct 11 '11 at 22:38

You can accomplish the redirection with iptables:

iptables -A INPUT -i eth0 -p tcp --dport 80 -j ACCEPT

iptables -A INPUT -i eth0 -p tcp --dport 8080 -j ACCEPT

iptables -A PREROUTING -t nat -i eth0 -p tcp --dport 80 -j REDIRECT --to-port 8080
share|improve this answer
    
+1. Being doing this for years – Alexander Pogrebnyak Oct 12 '11 at 0:33
1  
How about on Windows Server? – Luc Aug 26 '12 at 21:26
    
Oh man this is exactly what I was looking for, kudos! – neurosnap May 7 '15 at 18:13

You should look at using a reverse proxy, such as Nginx...

e.g. put this in your nginx.conf file:

server {
   listen         80;

   server_name    your_ip_address your_server_name

   access_log   /var/log/nginx/your_domain/access.log ;
   error_log    /var/log/nginx/your_domain/error.log info ;

   location / {
      proxy_pass  http://127.0.0.1:3000;   # pass requests for dynamic content to Rails app
   }
}
share|improve this answer

Rinetd setup is easier I think.

share|improve this answer
    
You saved me from pulling out the last of my hair. +1 – Beachhouse May 20 '13 at 18:48

Just found myself in this question an couldn't find an easy way.

Don't want to install Nginx in my machine to do this simple port forwarding.

Rinetd didn't work for me, no working package for my distro.

I went for socat instead. Super simple:

# socat TCP-LISTEN:80,fork TCP:127.0.0.1:5000

Must be ran as su to be able to listen on port 80.

share|improve this answer

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.