Configure port 80 forwarding for local tomcat applications in Linux

Source: Internet
Author: User
Scenario: locally deploy tomcat to port 8080, and expect local access to port 80 to access local tomcat. Conclusion: The iptables tool in linux is used to implement port forwarding. Specifically for the current get root permission to execute iptables-tnat-IOUTPUT-ptcp-d127.0.0.1 -- dport80-jREDIRECT -- to-port8080 currently

Scenario:

Locally deploy tomcat to port 8080, and expect local access to port 80 to access local tomcat.

Conclusion:

UseIptablesTool 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

[Html]

#! /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-jREDIRECT -- to-port 8080

Result: access to the local localhost is forwarded normally, and access to the external network is normal.

Cause: similar to the above example, but there is one more rule: the source address of the data packet must be matched-s127.0.0.1. 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-jREDIRECT -- 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-jREDIRECT -- 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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.