Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to configure linux port forwarding

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces "how to configure linux port forwarding". In daily operation, I believe many people have doubts about how to configure linux port forwarding. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubts about "how to configure linux port forwarding". Next, please follow the editor to study!

In linux, port forwarding refers to listening on the port with Nginx. When a HTTP request arrives, the HOST and other information of the HTTP request is matched with its configuration file and forwarded to the corresponding application. Port forwarding can be used to access the domain name without adding a port.

The operating environment of this tutorial: linux7.3 system, Dell G3 computer.

What is linux port forwarding?

What is port forwarding

When we build a data and a downloaded application on the server, the data application launches port 8001 and the download application launches port 8002. At this point, if we can pass through

Localhost:8001 / / data localhost:8002 / / download

However, when we generally access applications, we want to access the domain name without adding ports, that is, both applications are accessed through port 80. But what should we do when we know that a port on the server can only be used by one program? A common method is to use Nginx for port forwarding. The implementation principle of Nginx is as follows: listen to port 80 with Nginx, when a HTTP request arrives, match the HOST and other information of the HTTP request with its configuration file and forward it to the corresponding application. For example, when a user accesses linux.linuxmi.com, Nginx knows from the configuration file that this is a HTTP request for the data application and forwards the request to the application processing on port 8001. When the user accesses m.linuxmi.com, Nginx knows from the configuration file that this is a HTTP request for downloading the application, so it forwards the request to the application processing on port 8002. A simple Nginx configuration file (part) is shown below:

# configure load balancer pool # Demo1 load balancer pool upstream linux_pool {undefinedserver 127.0.0.1 upstream m_pool 8001;} # Demo2 load balancer pool upstream m_pool {undefinedserver 127.0.0.1 undefinedserver 8002;} # Demo1 port forwarding server {undefinedlisten 80th serverbalancer name linux.linuxidc.com;access_log logs/linux.log;error_log logs/linux.error;# forwards all requests to the application processing location / {undefinedproxy_set_header Host $host of the demo_ Pool pool The proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;proxy_pass http://linux_pool;}}#Demo2 port forwards server {undefinedlisten 80th serverroomname m.linuxidc.comitAccesslog logs/m.log;error_log logs/m.error;# forwards all requests to the application processing location / {undefinedproxy_set_header Host $host;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for of the demo_ Pool pool Proxy_pass http://m_pool;}}

The above configuration implements:

1. When the domain name accessed by the user is http://linux.linuxidc.com, we automatically forward the request to the Tomcat application with port number 8001 for processing.

2. When the domain name accessed by the user is http://m.linuxidc.com, we automatically forward the request to the Tomcat application with port number 8002 for processing.

The above technical implementation is port forwarding. Port forwarding means that the software listens to a port on a domain name (usually port 80). When the domain name and port of the access server meet the requirements, it will be forwarded to the specified Tomcat server according to the configuration. Our commonly used Nginx also has a port forwarding function.

At this point, the study on "how to configure linux port forwarding" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report