Configure local Tomcat application port 80 forwarding in Linux
Scenario:
Locally deploy Tomcat to port 8080, and expect local access to port 80 to access local Tomcat.
Conclusion:
Use iptables in Linux to implement port forwarding.
Specifically
The root permission is obtained.
Run iptables-t nat-I OUTPUT-p tcp-d 127.0.0.1 -- dport 80-j REDIRECT -- to-port 8080
It takes effect before being restarted. To take effect permanently, run the following command in Ubuntu: iptables-save>/etc/iptables. rules.
Create a bash script
#! /Bin/bash
Iptables-restore </etc/iptables. rules save to the/etc/network/if-pre-up.d/directory
End!
BTW: if the external server is configured, replace step 1 with iptables-t nat-I PREROUTING-p tcp -- dport 80-j REDIRECT -- to-port 2nd.
Experiment process:
The experiment mainly focuses on the iptables rules to be added in step 1 above.
After obtaining the root permission, perform the following operations:
Iptables-t nat-I OUTPUT-p tcp -- dport 80-j REDIRECT -- to-port 8080
Result: access to the local localhost is forwarded normally. Access to the external network is redirected to the local localhost: 8080.
Cause: when a local access request is sent, the packets generated by the local process enter the OUTPUT chain. Because the current request packet port is 80, the redirection channel is 8080. Note that all packets are sent.
Iptables-t nat-I OUTPUT-p tcp-s 127.0.0.1 -- dport 80-j REDIRECT -- to-port 8080
Result: access to the local localhost is forwarded normally, and access to the external network is normal.
Cause: similar to the preceding example, but there is one more rule: the source address of the data packet-s 127.0.0.1 needs to be matched. However, when accessing the external network, the source address of the actual data packet passing through this link is the local ip address, rather than the loop ip address (127.0.0.1). Therefore, this rule is equivalent to being ineffective when accessing the external network.
Iptables-t nat-I OUTPUT-p TCP/IP non-loop Nic ip -- dport 80-j REDIRECT -- to-port 8080
Result: access to the local localhost cannot be forwarded. Access to the external network is redirected to the local localhost: 8080.
Cause: similar to the preceding example, the rule matches the externally accessed data packet and is redirected.
Iptables-t nat-I OUTPUT-p tcp-d 127.0.0.1 -- dport 80-j REDIRECT -- to-port 8080
Result: access to the local localhost is forwarded normally, and access to the external network is normal.
Cause: the ip address of the local application is matched, so when accessing the local application, the data packet will be redirected to the 8080
Iptables-t nat-I OUTPUT-p tcp-s 127.0.0.1-d 127.0.0.1 -- dport 80-j REDIRECT -- to-port 8080
Result: access to the local localhost is forwarded normally, and access to the external network is normal.
Cause: A combination of 2, 4 Causes of the operation.
Iptables-t nat-I PREROUTING-p tcp-s 127.0.0.1-d 127.0.0.1 -- dport 80-j REDIRECT -- to-port 8080
Result: access to the local localhost cannot be forwarded. Access to the external network is normal and there is no special processing.
Cause: local connection refers to accessing the port of the Local Machine with 127.0.0.1 or the local IP address. Data packets that are locally connected do not pass through the NIC, but are directly sent to the local process after being processed by the kernel. This type of data packet only passes through the OUTPUT chain in iptables, instead of the PREROUTING chain.
Iptables Packet Flow:
Local packet ----> mangle prerouting -------> nat prerouting -------> mangle input -------> filter input
Local packet -------> mangle output -------> nat output -------> filter output -------> mangle postrouting -------> nat postrouting
Packet packets forwarded locally -------> mangle prerouting -------> nat prerouting -------> mangle forward -------> filter forward -------> mangle postrouting -------> nat postrouting
For more Tomcat tutorials, see the following:
Install and configure the Tomcat environment in CentOS 6.6
Install JDK + Tomcat in RedHat Linux 5.5 and deploy Java Projects
Tomcat authoritative guide (second edition) (Chinese/English hd pdf + bookmarks)
Tomcat Security Configuration and Performance Optimization
How to Use Xshell to view Tomcat real-time logs with Chinese garbled characters in Linux
Install JDK and Tomcat in CentOS 64-bit and set the Tomcat Startup Procedure
Install Tomcat in CentOS 6.5
Tomcat details: click here
Tomcat: click here
This article permanently updates the link address: