Nginx implement port forwarding

What is port forwarding

When applying a book and a movie that we set up on the server, where the Books app started the 8001 port, started the 8002 movie application port. At this point if we can

localhost:8001    //图书
localhost:8002    //电影

But we all want general access to applications without access to the port on the domain name, that both applications are accessed through port 80. But we know that a port on the server can only be used by a program, how this time how to do it? One popular method is to use port forwarding Nginx. The principle of Nginx is: Nginx listening on port 80, when a HTTP request comes in, the HOST HTTP request information such as its configuration file and forward it to match the corresponding application. For example when the user accesses book.douban.com, Nginx know this profile book application is an HTTP request, then forwards the request to the application processing port 8001. When a user accesses movie.douban.com, Nginx know from the configuration file in this movie is the HTTP application request, then forwards the request to the application processing 8002 port. Nginx a simple profile (section) as shown below:

#配置负载均衡池
#Demo1负载均衡池
upstream book_pool{
    server 127.0.0.1:8001;
}
#Demo2负载均衡池
upstream movie_pool{
    server 127.0.0.1:8002;
}

#Demo1端口转发
server {
    listen       80;
    server_name  book.chanshuyi.com;
    access_log logs/book.log;
    error_log logs/book.error;
    
    #将所有请求转发给demo_pool池的应用处理
    location / {
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_pass http://book_pool;
    }
}
#Demo2端口转发
server {
    listen       80;
    server_name  movie.chanshuyi.com;
    access_log logs/movie.log;
    error_log logs/movie.error;
    
    #将所有请求转发给demo_pool池的应用处理
    location / {
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_pass http://movie_pool;
    }
}

This configuration enables the above:

1, when the user accesses the domain name is: http: when //book.chanshuyi.com, we automatically forward requests to port numbers for the Tomcat application processing 8001.

2, when the domain which is: http: when //movie.chanshuyi.com, we automatically forward requests to port numbers for the Tomcat application processing 8002.

This technology is above port forwarding . Port forwarding is union listening on a port on a domain name (usually port 80) by software, when the domain name and port access server to meet the requirements, it forwards the packet to the Tomcat server processing in accordance with the configuration. We used Nginx also have port forwarding function.

Guess you like

Origin blog.csdn.net/caidingnu/article/details/90739915