I want to redirect all traffic from port 443 to the internal port 8080. I'm using this config for iptables:
iptables -t nat -I PREROUTING --source 0/0 --destination 0/0 -p tcp \
--dport 443 -j REDIRECT --to-ports 8080
This works for all external clients. But if I'm trying to access the port 443 from the same maschine I'll get a connection refused error.
wget https://localhost
How can I extend the iptables rule to redirect local traffic too?
PREROUTING isn't used by the loopback interface, you need to also add an OUTPUT rule:
To redirect packets from localhost to another machine the rule:
will work, BUT you also need to enable this option in the kernel:
Without that kernel setting it wont work.
How about this?
You said you are getting connection refused error. This means that there is no local process listening on the port you are trying to connect to! To check the listening processes, use the command:
After applying the rule, you should have a process listening on the port 8080 to get connected.
It seems that you should have the following rule instead:
Remember that you are sending from the localhost. So, you need to redirect the output packet.