nginx configures domain name, reverse agent and load balancing

Listening to different ports

The default commodity of nginx is port 80. If there is a server service in the configuration file, you can copy multiple servers below if you need more than one. Listen to different ports to change the corresponding configuration

server {
        listen       80;  # Listening port number, 80 by default
        server_name  localhost;   # Monitoring IP

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {     # Return to root
            root   html;    # Return the html directory under the root directory
            index  index.html index.htm;  # Display the corresponding html
        }
        
        error_page   500 502 503 504  /50x.html;  # Listen for error messages
        location = /50x.html {
            root   html;
        }
        
    }

Listen to different domain names

Suppose now www.test.com and www.test1.com We need to access the same html. We need to map two domain names to the same html file.
You need to add at the bottom of the local host file:

IP address: www.test1.com
 IP address: www.test.com

Www.test.com name domain name. When you enter www.test.com in browsing, the corresponding IP address will be found automatically.

Modify the nginx configuration file, copy the html in the nginx installation directory, and modify the html display content.

# Listen to www.test.com domain name
server {
        listen       80;  # Listening port number, 80 by default
        server_name  www.test.com;   # Change to listening domain name

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {     # Return to root
            root   html-test;    # Return the html directory under the root directory
            index  index.html index.htm;  # Display the corresponding html
        }
        
        error_page   500 502 503 504  /50x.html;  # Listen for error messages
        location = /50x.html {
            root   html;
        }
        
    }
    
# Listen to www.test1.com domain name
server {
        listen       80;  # Listening port number, 80 by default
        server_name  www.test1.com;   # Change to listening domain name

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {     # Return to root
            root   html-test1;    # Return the html directory under the root directory
            index  index.html index.htm;  # Display the corresponding html
        }
        
        error_page   500 502 503 504  /50x.html;  # Listen for error messages
        location = /50x.html {
            root   html;
        }
        
    }

Enter www.test.com and www.test1.com to see two different html.

Reverse proxy

Also configure the domain name and IP mapping relationship in the host file, for example:
IP address www.sina.com
Starting tomcat port is the default 8080

upstream sina{ # Address is the address in tomcat
		server IP:8080;
	}
	server {
        listen       80;
        server_name  www.sina.com;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            proxy_pass   http://Sina; he will find the address in the sina of the upstream
            index  index.html index.htm;
        }
        
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
        
    }

When we type www.sina.com, it looks for "proxy? Pass http://sina ; ", and then go to the sina of upstream to find the final address, and the page will jump to the specified page under tomcat.

load balancing

Under the premise that the reverse proxy has been completed, now sina has two servers, how can we jump to enter the domain name?
Just add another server to the upstream

upstream sina{ # Address is the address in tomcat
		server IP:8080 weight=2; # Weight (1 by default, each server accesses in turn), the greater the weight, the more accesses
		server IP:8081; # load balancing
	}
	server {
        listen       80;
        server_name  www.sina.com;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            proxy_pass   http://Sina; he will find the address in the sina of the upstream
            index  index.html index.htm;
        }
        
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
        
    }

Keywords: Operation & Maintenance Tomcat Nginx

Added by dennisflash on Thu, 12 Dec 2019 20:59:55 +0200