Skip to content

Instantly share code, notes, and snippets.

@Kirens
Created August 3, 2018 06:43
Show Gist options
  • Select an option

  • Save Kirens/0cd7bc4238988906b42e7d04a731763f to your computer and use it in GitHub Desktop.

Select an option

Save Kirens/0cd7bc4238988906b42e7d04a731763f to your computer and use it in GitHub Desktop.
Forward Web trafic to other host with IPTABLES
sudo iptables -t nat -A PREROUTING -j DNAT -p tcp --dport 80 --to {{HOST}}:80
sudo iptables -t nat -A PREROUTING -j DNAT -p tcp --dport 443 --to {{HOST}}:443
### Explanation
# iptables [-t table] -[AD] chain rule-specification [options]
# table:
# `-t nat` - We want the rules to apply to the "nat" table; to the
# first packets of a connection
# operation:
# `-A` - We want to append the rule
# chain:
# `PREROUTING` - We want to alter incomming packages, this is the chain
# for that
# rule-specification:
# `-j DNAT` - If rules applies, this and all future packets in this
# connection should not be processed by other rules and
# will be forwarded as specified
# `-p tcp` - Do this for tcp packets
# `--dport 80` - Do this for packets with destination port 80
# `--to {{HOST}}:80` - Forward to port 80 at HOST
sudo iptables -A FORWARD -j ACCEPT -p tcp -d {{HOST}} --dport 80
sudo iptables -A FORWARD -j ACCEPT -p tcp -d {{HOST}} --dport 443
### Explanation
# iptables [-t table] -[AD] chain rule-specification [options]
# table:
# `` - If none is specified the default table `filter` is used
# operation:
# `-A` - We want to append the rule
# chain:
# `FORWARD` - We want to handle packets being routed through the "box"
# rule-specification:
# `-j ACCEPT` - If the rules apply, all packets will be passed on as is
# `-p tcp` - Do this for tcp packets
# `-d {{HOST}}` - Do this for packets with destination HOST
# `--dport 80` - Do this for packets with destination port 80
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment