Scenario / Questions

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?

Find below all possible solutions or suggestions for the above questions..

Suggestion: 1

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

Suggestion: 2

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.

Suggestion: 3

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