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?
Server Fault is a question and answer site for system and network administrators. It only takes a minute to sign up.
Sign up to join this communityHow 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?
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
.
socat
close the socket when interrupted with ctrl+C, so this solution used verbatim will fail for some time after socat
is ctrl+C interrupted, but as a quick hack it's possible to add reuseaddr
to it to avoid this error. Also use port 443 instead of 80 to forward HTTPS
Dec 22, 2022 at 8:55
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
}
}