How to use port 80 in Tomcat under Linux system

How to use port 80 in Tomcat under Linux system

Application Scenario

In many cases, we install software such as tomcat or nginx on the Linux server.

When we want to use port 80, we will get an error if we don't start it as root user.

This is because, for Linux systems, ports below 1024 are not available to ordinary users.

There are two solutions to this problem on the Internet. One is to give the file root permissions.

Another method is to do port jump. I think port jump may be safer. I will focus on how to configure it.

IPTABLES

Here we mainly do port forwarding on this machine. I will mainly talk about how to set and delete it.

For more information about iptables, you can refer to a lot of information on the Internet.

Add a port mapping

Now we have to do one thing, assuming that we have tomcat installed on our Linux. The default access port is 8080.

Now I want to be able to access tomcat when the user accesses port 80.

Just execute the command under the root user

iptables -t nat -A PREROUTING -p tcp –dport 80 -j REDIRECT –to-port 8080 


-t nat : Indicates which table I want to operate on. (If not specified, it means filter. The default is filter)

-A PREROUTING: A means adding. It means I want to add a rule in PREROUTING

--dport 80 : If requesting port 80.

--to-port 8080 : Then go to port 8080.

The test is as follows:


How to Delete a Rule

From the above we can see that we added the rule in PREROUTING of the nat table.

So we need to delete this rule in PREROUTING of the nat table.


iptables -t nat -L -nv --line-numbers

The purpose of this command is to list the rules in the nat table and give a num.

Then we can use this id to delete the rule.

If you don't write -t ​​nat, the default search is the filter table. Then the rules cannot be found.


This statement can be used to delete the rule.

-t nat : means I want to operate this table. If not specified, it means filter.

-D: indicates to perform a delete operation

PREROUTING: Indicates which chain in the NAT table. The number 1 behind it is the num in the above figure.


Summarize

I did the following experiments, nginx occupies port 80 and tomcat occupies port 8080.

Open nginx, tomcat

1. If redirection is enabled, nginx cannot be accessed because when accessing port 80, it jumps to port 8080.

2 Close port forwarding and you can access nginx.

3 From the above, we can see that port forwarding will not occupy the port.

4 If adding rule access does not achieve the desired effect, clear the browser cache.

5 All the above operations will disappear after restart. If you want to keep the operations, please execute service iptables restart

Well, that’s all for this article. I hope the content of this article will be of certain reference value to your study or work. Thank you for your support of 123WORDPRESS.COM.

You may also be interested in:
  • How to modify the default port number 8080 of Tomcat
  • How to handle Tomcat startup error (port 8080 is occupied)
  • Jsp and PHP share port 80 to integrate Apache and Tomcat (no need to add port number when accessing)
  • How to solve the problem of port 80 of tomcat being occupied in java
  • Detailed explanation of Tomcat multi-domain configuration (multiple projects share port 80)
  • Perfect solution to the problem of ports 8080 and other ports being occupied when deploying Tomcat on Eclipse
  • How to solve the problem of IIS7 and Tomcat7 sharing port 80 in 64-bit Windows 2008 system
  • Detailed explanation of nginx configuration to share port 80 with multiple tomcats
  • Tomcat shows that port 8080 is occupied. Graphical solution
  • Solution for IIS Tomcat sharing port 80

<<:  MySQL startup error InnoDB: Unable to lock/ibdata1 error

>>:  Example of implementing a virtual list in WeChat Mini Program

Recommend

JDBC-idea import mysql to connect java jar package (mac)

Preface 1. This article uses MySQL 8.0 version Co...

MySQL account password modification method (summary)

Preface: In the daily use of the database, it is ...

About Vue virtual dom problem

Table of contents 1. What is virtual dom? 2. Why ...

Vue sample code for online preview of office files

I'm working on electronic archives recently, ...

jQuery implements simple button color change

In HTML and CSS, we want to set the color of a bu...

JavaScript manual implementation of instanceof method

1. Usage of instanceof instanceof operator is use...

Two practical ways to enable proxy in React

Two ways to enable proxy React does not have enca...

MySQL trigger detailed explanation and simple example

MySQL trigger simple example grammar CREATE TRIGG...

Linux system command notes

This article describes the linux system commands....

Detailed example of using useState in react

useState useState adds some internal state to a c...

HTML is something that web page creators must learn and master.

What are the benefits of learning HTML? 1: Easily...

Detailed explanation of the principle of Vue monitoring data

Table of contents 1. Introduction II. Monitoring ...

Detailed steps for setting up a nexus server

1. The significance of building nexus service As ...

Detailed explanation of the principle of js Proxy

Table of contents What is Proxy Mode? Introducing...