port forwarding

Original link https://www.cnblogs.com/zhaoyingjie/p/7248678.html

When we build a book and a movie application on the server, the book application starts up port 8001, and the movie application starts up port 8002. At this point if we can pass

localhost:8001 //Books
localhost:8002 //movie

 

However, when we generally access applications, we hope to access the domain name without adding a port, that is, both applications are accessed through port 80. But we know that a port on the server can only be used by one program, what should we do at this time? A common method is port forwarding with Nginx. The implementation principle of Nginx is: use Nginx to monitor port 80, when an 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 book.douban.com, Nginx knows from the configuration file that this is an HTTP request from the book application, so it forwards the request to the application on port 8001 for processing. When a user visits movie.douban.com, Nginx knows from the configuration file that this is an HTTP request of the movie application, so it forwards the request to the application on port 8002 for processing. A simple Nginx configuration file (in part) looks like this:

 

copy code
#Configure the load balancing pool
#Demo1 load balancing pool
upstream book_pool{
    server 127.0.0.1:8001;
}
#Demo2Load Balancing Pool
upstream movie_pool{
    server 127.0.0.1:8002;
}

#Demo1Port Forwarding
server {
    listen       80;
    server_name  book.chanshuyi.com;
    access_log logs/book.log;
    error_log logs/book.error;
    
    #Forward all requests to the application processing of the demo_pool pool
    location / {
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_pass http://book_pool;
    }
}
#Demo2Port Forwarding
server {
    listen       80;
    server_name  movie.chanshuyi.com;
    access_log logs/movie.log;
    error_log logs/movie.error;
    
    #Forward all requests to the application processing of the demo_pool pool
    location / {
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_pass http://movie_pool;
    }
}
copy code

 

The above configuration achieves:

1. When the domain name accessed by the user is: http://book.chanshuyi.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://movie.chanshuyi.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 uniformly monitors a certain port (usually port 80) on a certain domain name. When the domain name and port of the access server meet the requirements, it will be forwarded to the specified Tomcat server for processing according to the configuration. Our commonly used Nginx also has port forwarding function.

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324960443&siteId=291194637