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

Detailed explanation of Vue px to rem configuration

Table of contents Method 1 1. Configuration and i...

js to achieve cool fireworks effect

This article shares the specific code for using j...

Excel export always fails in docker environment

Excel export always fails in the docker environme...

Pure CSS to achieve left and right drag to change the layout size

Utilize the browser's non- overflow:auto elem...

Six border transition effects implemented by CSS3

Six effectsImplementation Code html <h1>CSS...

Specific method of viewing user authorization information in mysql

Specific method: 1. Open Command Prompt 2. Enter ...

Using JS to implement a simple calculator

Use JS to complete a simple calculator for your r...

KVM virtualization installation, deployment and management tutorial

Table of contents 1.kvm deployment 1.1 kvm instal...

Detailed explanation of Zabbix installation and deployment practices

Preface Zabbix is ​​one of the most mainstream op...

Pure CSS to change the color of the picture

The css technique for changing the color of an im...

Native js to achieve accordion effect

In actual web page development, accordions also a...

js to implement collision detection

This article example shares the specific code of ...

Detailed examples of converting rows to columns and columns to rows in MySQL

mysql row to column, column to row The sentence i...

Detailed explanation of the steps of using ElementUI in actual projects

Table of contents 1. Table self-sorting 2. Paging...

In-depth explanation of modes and environment variables in Vue CLI

Preface In the development of actual projects, we...