IPTABLES nat PREROUTING does not work on localhost

Let’s assume you have to use IPTABLES to redirect a PORT to another PORT (I.e. : Redirect connections on port 80 to port 9191). Then you probably use a IPTABLES rule like this:

This will work perfectly for external clients. But if you try to access the port 80 from the same machine (localhost), you will end up getting a connection refused error or port not been redirect correctly.

This happens because PREROUTING isn’t used by the loopback interface. Therefore, you have to add an OUTPUT rule along with the PREROUTING rule.