How to Redirect HTTP/HTTPS Ports with IPTables in Linux

Setting Up IPTables Rules for Port Redirection

📘 Setting Up IPTables Rules for Port Redirection

🔍 Overview

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.


🛠️ Step-by-Step Instructions

1. Install IPTables Utilities

First, install iptables-save and iptables-persistent to manage and persist your rules across reboots:

      sudo apt install -y iptables-save iptables-persistent

2. Set Up PREROUTING Rules

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

3. Set Up OUTPUT Rules (For Localhost Redirects)

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

4. Save the Rules

To persist your rules across system restarts, save them using:

      sudo iptables-save

✅ Result

Your system will now redirect all traffic:

  • From port 808080

  • From port 4438443

Both for external connections and localhost traffic.

    • Related Articles

    • Configuring Modbus IP Data Source in Mango

      ? Overview The Modbus IP data source in Mango OS is used to gather data from Modbus-compatible devices over an IP network. These devices can reside on a local network, an intranet, or even the public internet. The data source operates by polling the ...
    • Installing Mango Automation via Docker on Ubuntu

      ? Overview This guide explains how to install and run Mango Automation as a Docker container on an Ubuntu host. It covers: Installing Docker Pulling and running the Mango image Editing mango.properties inside the container Viewing Mango log files ...
    • Understanding Events, Event Detectors, and Event Handlers in Mango

      Summary This article explains the core concepts behind Mango’s event system: events, event detectors, and event handlers. Read this first before configuring any alarms or notifications. 1. What is an event? In Mango, an event represents “something ...
    • Installing and Configuring PostgreSQL for Mango

      ? Installing and Configuring PostgreSQL for Mango This guide provides detailed steps to install and configure PostgreSQL for use with Mango OS. You will install PostgreSQL, create a dedicated Mango database and user, and validate Mango’s connection ...
    • Configuring BACnet/IP data source and BACnet publisher in Mango

      ? Overview This guide outlines the steps to configure BACnet/IP communication in Mango OS, including setting up a BACnet Local Device, creating a BACnet/IP Data Source, and publishing points via a BACnet Publisher. These steps allow Mango to ...