Nginx virtual hosts do

Nginx there is a main function is to do web hosting, is to configure multiple server, each server a proxy upstream
when an application we built a library and a movie on the server, which launched the 8001 port Books app, launched the 8002 movie application port. 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

Configure load balancing pool

Demo1 load balancing pool

upstream book_pool{
server 127.0.0.1:8001;
}

Demo2 load balancing pool

upstream movie_pool{
server 127.0.0.1:8002;
}

Demo1 port forwarding

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 port forwarding

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://book.chanshuyi.com , we automatically forward their requests to port numbers for the Tomcat application processing 8001.

2, when the user accesses the domain name is: http://movie.chanshuyi.com , we automatically forward their 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 www.cnblogs.com/codlover/p/12158438.html