This guide walks you through setting up IPTables to redirect standard HTTP (port 80) and HTTPS (port 443) traffic to custom application ports like 8080 and 8443. This is useful when your application does not run as root but needs to serve over standard web ports.
First, install iptables-save and iptables-persistent to manage and persist your rules across reboots:
sudo apt install -y iptables-save iptables-persistent
Redirect all incoming traffic on ports 80 and 443 to your application’s ports (8080 and 8443):
sudo iptables -t nat -A PREROUTING -p tcp -m tcp --dport 80 -j REDIRECT --to-ports 8080 sudo iptables -t nat -A PREROUTING -p tcp -m tcp --dport 443 -j REDIRECT --to-ports 8443
Redirect local requests from the host to your application's listening ports:
sudo iptables -t nat -A OUTPUT -d 127.0.0.1/32 -p tcp -m tcp --dport 80 -j REDIRECT --to-ports 8080 sudo iptables -t nat -A OUTPUT -d 127.0.0.1/32 -p tcp -m tcp --dport 443 -j REDIRECT --to-ports 8443
To persist your rules across system restarts, save them using:
sudo iptables-save
Your system will now redirect all traffic:
From port 80 → 8080
From port 443 → 8443
Both for external connections and localhost traffic.