Jingtao day08- Jingtao project agent and Nginx

1. Reverse proxy

1.1 Reverse proxy concept

The reverse proxy server is located between the user and the target server, but for the user, the reverse proxy server is equivalent to the target server, that is, the user can obtain the resources of the target server by directly accessing the reverse proxy server. At the same time, the user does not need to know the address of the target server and does not need to make any settings on the client side. A reverse proxy server can usually be used as a Web acceleration, that is, a reverse proxy is used as the front end of a Web server to reduce the load of the network and the server and improve the access efficiency.
Features:
1. The reverse proxy server is located between the user and the target server.
2. The user thinks that the reverse proxy server is the real server. The user does not know who the real server is.
3. The reverse proxy server protects server-side information, which is called server-side proxy.
The reason for the existence of the proxy: The user cannot directly access the target server for some reason to complete the specified function.

1.2 Forward proxy

1.2.1 Introduction to forward proxy

Forward proxy, which means a server between the client and the origin server. In order to get the content from the origin server, the client sends a request to the proxy and specifies the target (origin server), and then the proxy forwards to the origin server. Request and return the obtained content to the client. The client can use the forward proxy.
Features:
1. The proxy server is located between the user and the server
2. When the user requests, it is very clear who the target server is. The server does not know who is accessing me. I thought it was a request directly initiated by the proxy server.
3. The forward proxy server protects the user's information, so it is called a client proxy.

1.2.2 About the agency summary

1. The reverse proxy is a server-side proxy. As long as the user accesses the server, it is actually a reverse proxy mechanism. Realize business calls
2. The forward proxy is a client-side proxy. The main user uses the forward proxy when they go online. What is implemented is the network communication header, the network penetration tool, the night god simulator/peanut shell

2.Nginx

2.1 Introduction to Nginx Server

2.2 Introduction to nginx

Nginx is a lightweight Web server/reverse proxy server and email (IMAP/POP3) proxy server, released under the BSD-like protocol. Its characteristics are that it occupies less memory and has strong concurrency capability. In fact, the concurrency capability of nginx is better among web servers of the same type. Users of nginx websites in mainland China include: Baidu, JD.com, Sina, NetEase, Tencent, Taobao, etc.
Features:
1. Occupy less memory, 2M tomcat startup, about 200M
2. Strong concurrency capability 50,000/second, actual 20,000-40,000/second

2.3 Nginx installation and use

1). Start Nginx nginx startup will generate 2 process items
1. The main function of the main process is to provide reverse proxy services. After closing the main process, the memory is large
2. The daemon process prevents the main process from closing unexpectedly. Close the daemon first

2.4 Nginx commands

Working directory description: It is required to be executed in the root directory where nginx.exe is located
1. Start command
start nginx Linux ./nginx
2. Rename command
nginx -s reload Linux: ./nginx -s reload
3. Close command
nginx -s stop Linux: ./nginx -s stop

2.5 The principle of Nginx reverse proxy

Introductory case description: http://localhost:80

3. Realize image echo

3.1 Requirements

Implement the proxy of the image address, and redirect the image address to the specific disk path.
URL address: http://image.jt.com/2020/12/0...
Local Disk Address:
E:images/2020/12/02/7d7179100d1e423abc2546e77743947c.png

3.2 Configure nginx

 #1. Configure image proxy
    server {
        listen 80;
        server_name image.jt.com;

        location / {
            root E:\images;
        }
    }

3.3 Edit the HOSTS file

Function: Realize the mapping of local domain name and IP address
path:

content:

#@SwitchHosts! {"url": null, "icon_idx": 0, "title": "u5f53u524du7cfbu7edf hosts"}
# Jingtao configuration  
#192.168.126.129  image.jt.com
#192.168.126.129  manage.jt.com
#IP domain name Mapping relationship
127.0.0.1  image.jt.com
127.0.0.1  manage.jt.com
127.0.0.1  www.jt.com
127.0.0.1  sso.jt.com
127.0.0.1  localhost
#bug lost the last letter problem 

3.4 The principle of image echo

4. Realize domain name proxy

4.1 Requirements Description

Requirements: require users to pass http://manage.jt.com Access the server at localhost:8091.
Implementation method: use reverse proxy mechanism to achieve

4.2 Configuring nginx

#2. Configure the backend server manage.jt.com:80 localhost:8091
    server {
        listen 80;
        server_name manage.jt.com;

        location / {
            #Initiate url request address
            proxy_pass http://localhost:8091;
        }
    } 

5.Nginx implements tomcat cluster deployment

5.1 Principles of Cluster Construction

5.2 Display port number dynamically

package com.jt.controller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class PortController {
    @Value("${server.port}")
    private int port;
    @RequestMapping("/getPort")
    public String getPort(){
        return "Current access port number:"+port;
    }
}

5.3 Project Packaging

Description: Since 3 tomcat servers need to be prepared. So the port numbers are 8081/8082/8083 in sequence

cmd project running command: java -jar 8081.war

5.4 Nginx Load Balancing

5.4.1 Polling Policy

Description: According to the order of the configuration files, access the server in turn.

#2. Configure the backend server manage.jt.com:80 localhost:8091
    server {
        listen 80;
        server_name manage.jt.com;

        location / {
            #Initiate url request address
            #proxy_pass http://localhost:8091;
            proxy_pass  http://jtWindows;
        }
    }
#3. Configure the tomcat server cluster Default: polling strategy
    upstream jtWindows {
        server 127.0.0.1:8081;
        server 127.0.0.1:8082;
        server 127.0.0.1:8083;
    }

5.4.2 Weight Policy

Description: Let the server with better performance handle more user requests.

#3. Configure tomcat server cluster Default: polling strategy, 2. Weight
    upstream jtWindows {
        server 127.0.0.1:8081 weight=6;
        server 127.0.0.1:8082 weight=3;
        server 127.0.0.1:8083 weight=1;
    }

5.4.3 IPHASH strategy

Requirements: The user needs to be bound to a server.

#3. Configure tomcat server cluster Default: polling strategy, 2. weight, 3.iphash
    upstream jtWindows {
    ip_hash;
        server 127.0.0.1:8081 weight=6;
        server 127.0.0.1:8082 weight=3;
        server 127.0.0.1:8083 weight=1;
    }

shortcoming:
1. It is easy to cause uneven load phenomenon.
2. If the IP address is bound to the user, if the tomcat server goes down, it will directly affect the user.
IPhash practical scenario: generally used for stress testing.

6.Nginx Advanced Properties

6.1 down property

Note: If the server is down, it can be identified by the down attribute, and the identified server will no longer provide support for users.

#3. Configure tomcat server cluster Default: polling strategy, 2. weight, 3.iphash
    upstream jtWindows {
    #ip_hash;
        server 127.0.0.1:8081 weight=6 down;
        server 127.0.0.1:8082 weight=3;
        server 127.0.0.1:8083 weight=1;
    }

6.2 backup attribute

The setting of the standby machine. Under normal conditions, the standby machine does not work, but when the main machine is busy, or the main machine is down, the standby machine will be accessed.

#3. Configure tomcat server cluster Default: polling strategy, 2. weight, 3.iphash
    upstream jtWindows {
    #ip_hash;
        server 127.0.0.1:8081 weight=6 down;
        server 127.0.0.1:8082 weight=3;
        server 127.0.0.1:8083 weight=1 backup;
    }

6.3 tomcat server high availability

Description: If the efficiency of adding the down attribute artificially is not high, can it automatically detect whether the server is down, and if it is down, can it be automatically marked as down.

#3. Configure tomcat server cluster Default: polling strategy, 2. weight, 3.iphash
  #max_fails=1 sets the maximum number of failures. If it exceeds the maximum number of times, it will be down 
  #fail_timeout=60s Failed timeout 60s
    upstream jtWindows {
    #ip_hash;
        server 127.0.0.1:8081 max_fails=1 fail_timeout=60s;
        server 127.0.0.1:8082 max_fails=1 fail_timeout=60s;
        server 127.0.0.1:8083 max_fails=1 fail_timeout=60s;
    }

Tags: Spring Boot

Posted by mikebyrne on Wed, 04 May 2022 20:52:42 +0300