How to forward port 80 to another port on the samemachine?

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

Just found myself in this question and 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 root to be able to listen on port 80.


You should look at using a reverse proxy, such as Nginx. For example, you might put something like 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 to the destination
    }
}