nginx implements reverse proxy and dynamic static separation

Posted by JasonO on Tue, 04 Jan 2022 09:45:45 +0100

1 install two slaver s

1.1 server configuration

java37master:1G
java37slaver1:512M
java37slaver2:512M

1.2 creating virtual machines

  • Set the memory to 512M

  • Set up network

  • Set the host name and apply: java37slaver1+java37slaver2

  • Set encoding set

  • Set sheet number password

  • Note: if an error occurs: unable to get the version of the VMCI driver: the handle is invalid

    https://blog.51cto.com/xuan97916/1909845
    

1.3 add basic configuration

  • Install ifconfig
* see ifconifg Is there: ll /sbin/ifconfig   non-existent
* install ifconfig: yum install ifconifg      Missing package
* View dependent packages:  yum search ifconifg       lack net-tools.x86_64
* Install dependent packages:  yum install net-tools.x86_64 -y
  • Install vim
 yum -y install vim*
  • Installation rz
yum install lrzsz -y

1.4 installation jdk

  • Installation environment gc++
* yum install glibc.i686 -y
* yum -y install libaio.so.1 libgcc_s.so.1 libstdc++.so.6
# If the second step is wrong: execute the following command: yum install --setopt=protected_multilib=false libgcc
* yum update libstdc++-4.4.7-4.el6.x86_64
* yum install gcc-c++	-y
  • create folder
mkdir -p /usr/soft
  • Upload the compressed file of jdk to / usr/soft
  • compress
  • Rename to java11
  • Environment variable configuration
vim /etc/profile
# Configure jdk11
export JAVA_HOME=/usr/soft/jdk11
#  export JRE_HOME=$JAVA_HOME/jre  #Do not configure jdk11 without jre
export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
export PATH=$PATH:$JAVA_HOME/bin
  • Load profile
source /etc/profile
  • verification
java
javac
java -version

1.5 installing tomcat

  • Upload the tomcat compressed file to / usr/soft
  • decompression
  • Rename to tomcat9
  • Turn off firewall
[root@java37master tomcat9]# systemctl stop firewalld.service #Turn off firewall
[root@java37master tomcat9]# firewall-cmd --state   #View firewall status
[root@java37master tomcat9]# systemctl status firewalld.service   #View firewall status
[root@java37master tomcat9]# systemctl disable firewalld.service #Turn off boot firewall
  • Enter the bin package and start tomcat
./startup.sh
  • Browser access tomcat

1.6 publishing projects on two slaver s

  • The database can use the on the master

  • Copy war to webapps of slaver1 and slaver2

  • Open tomcat

  • Browser access

2. Configure static ip (for all three)

2.1 view the names of the three servers

  • It should not need to be modified: it has been specified when installing the virtual machine
[root@java37master ~]# cat  /etc/hostname
java37master
[root@java37slaver1 ~]# cat  /etc/hostname
java37slaver1
[root@java37slaver2 ~]# cat  /etc/hostname
java37slaver2
  • Restart the virtual machine if necessary
reboot

2.2 get the gateway of the host

DNS Domain name system (English: Domain Name System,abbreviation: DNS)Is a service of the Internet. It serves as a domain name and IP A distributed database with addresses mapped to each other can make it easier for people to access the Internet
[root@java37slaver2 ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.56.2    0.0.0.0         UG    100    0        0 ens33
192.168.56.0    0.0.0.0         255.255.255.0   U     100    0        0 ens33

2.3 set the static ip address of each host

[root@java37slaver2 ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens33
[root@java37slaver2 ~]# cat /etc/sysconfig/network-scripts/ifcfg-ens33
TYPE="Ethernet"
PROXY_METHOD="none"
BROWSER_ONLY="no"
BOOTPROTO="static" #Configure static ip:: 1 to static
DEFROUTE="yes"
IPV4_FAILURE_FATAL="no"
IPV6INIT="yes"
IPV6_AUTOCONF="yes"
IPV6_DEFROUTE="yes"
IPV6_FAILURE_FATAL="no"
IPV6_ADDR_GEN_MODE="stable-privacy"
NAME="ens33"
UUID="d0371869-480f-4966-b578-f4c19877545a"
DEVICE="ens33"
ONBOOT="yes"

#Configure static ip:: 2 add the following five items
# Add the following five items to configure static ip
IPADDR=192.168.56.130   #Current host ip  #The ip address of each host is different:
NETMASK=255.255.255.0
GATEWAY=192.168.56.2   #gateway
DNS1=8.8.8.8   
DNS2=114.114.114.114
  • Restart the network
systemctl restart network

3. Set dns of three servers (all three do)

3.1 concept

The three servers can access each other through the host name

3.2 edit / etc/hosts:

[root@java37slaver2 ~]# vim /etc/hosts
[root@java37slaver2 ~]# cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6

#Configure dns for three servers
192.168.56.128 java37master
192.168.56.129 java37slaver1
192.168.56.130 java37slaver2

3.3 ping test whether communication is possible

[root@java37slaver2 ~]# ping java37slaver2
[root@java37slaver2 ~]# ping java37slaver1
[root@java37slaver2 ~]# ping java37master1
PING java37slaver2 (192.168.56.130) 56(84) bytes of data.
64 bytes from java37slaver2 (192.168.56.130): icmp_seq=1 ttl=64 time=0.135 ms
64 bytes from java37slaver2 (192.168.56.130): icmp_seq=2 ttl=64 time=0.038 ms
64 bytes from java37slaver2 (192.168.56.130): icmp_seq=3 ttl=64 time=0.036 ms
64 bytes from java37slaver2 (192.168.56.130): icmp_seq=4 ttl=64 time=0.059 ms
...
^C
--- java37slaver2 ping statistics ---
19 packets transmitted, 19 received, 0% packet loss, time 18027ms
rtt min/avg/max/mdev = 0.032/0.056/0.135/0.029 ms

4 ssh password free login

SSH by [Secure Shell]Abbreviation for IETF Network group( Network Working Group)Developed; SSH It is a security protocol based on the application layer. SSH It is more reliable and designed for[Remote login]A protocol that provides security for sessions and other network services. utilize SSH The protocol can effectively prevent information disclosure in the process of remote management. 
Purpose: switch back and forth between three servers in a link

4.1 generate public key and secret key (for all three servers)

  • ssh-keygen
[root@java37slaver2 ~]# ssh-keygen  #Generate public and secret keys
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): 
Created directory '/root/.ssh'.
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:rg+D53FfFk6WXKuVR1oW1lAYWT3Q03zwnecCl5VZRp0 root@java37slaver2
The key's randomart image is:
+---[RSA 2048]----+
|             .*&^|
|              +E@|
|            . +.@|
|           . = O.|
|        S   * * o|
|     . .   + + o |
|    . = o   =    |
|     o * . o     |
|      o.. .      |
+----[SHA256]-----+
# Click enter or enter yes at the prompt

4.2 send the public key and secret key to the master (all three do it)

  • ssh-copy-id java37master
[root@java37slaver2 ~]# ssh-copy-id java37master
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host 'java37master (192.168.56.128)' can't be established.
ECDSA key fingerprint is SHA256:nbd7BL+4vc6QScOYqafO9p7KkxDkPb+On1BEOSRgdpc.
ECDSA key fingerprint is MD5:01:22:0b:dc:39:de:ac:6e:8a:c8:a4:00:09:71:e9:3b.
Are you sure you want to continue connecting (yes/no)? yes   #Enter yes
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@java37master's password:   #Input password

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh 'java37master'"
and check to make sure that only the key(s) you wanted were added.

4.3 view the file on the master / root / ssh/authorized_ keys

[root@java37master ~]# cat /root/.ssh/authorized_keys
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDHwdT7gfnM1szAO3iEbtwIv2UicU99N+oqHYSHoEuz8O+4QL1WRYmal1UoeF6AKTeO2FFJR60+mRfIH3orl8euLyh0cVkl4P9lN+SYoRFhnX2qfs5OmN/3p/GRt1g6EZCTGn/JPBAbCn3ueQefABq09DnduMYaFo5x5+Bc/KilRgdF6Eb7n4ha/WoZP530FdLUi1oAHMX4sobu2hHj7//ciptdFPje6VxMRPXHx2kdddL4FqxsYH1WlSZhuNC0ZlXZ7raV5dWwIxeSjw8293PRXuq3dkGRUF9VYD74onyN/U4msIBQ5ZNEf9l+28Hp6R65MJWSAirXWIoT3JEnS65n root@java37slaver2
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC/HW7pqOk4lSVbO36hn59k9FCa+I2iWc0D6Zb5VtPLxEqvyVXpd+bVLrDdqLyPimET3P4p9AsDQ+dp5EGaBQMNOyjlD0SHfFHvhF8GR+XPa9w7K6mnuniQZq1bylt0Kh4bXVaLAsyvIKUJLiMnElb0L2bFmuO6gBzze1JPAJ+aEwUBcxPVI+wr1M9IMMfOWknBf3dzLY3CBni+e9g+YacsimgGG6A6lFveuF5Wb19Z2mEn+Ep7Qt108FJoTIjxqzfCNlRChA1o4fdJx52sIHT8xtFT8uuQo2UekSScdVVROE1mrJj/Bco28K/cXyyQu3D5dIsLclN/Dthw61weiK47 root@java37slaver1
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC1zZxKdO/kYyOFBQJdkSADm6oMQXxQxxCHQXUitaPB3ckD0XI9awkUvuzUuaGr2ApHpaAXd6gZGBexbsWfWBBwZB6lRDzJZEajEblp5KGvcHJdDLzG1sMNfs4oxFOh0Gf2NeG0to/tbFnMOZ/hHUyy6KQv9BmPzIH6gEaMoNYxms/mr0NzEIVNNtXp9dke89x4nNkHsILXMKDhkvbXAjIieX9p2zCZEhTWrwuFS+AabnewjV3lCEAxxVQ4OlPTnWjdHlEmAtLq2ZiHL7YGPpSjdBqDINE9ams1EZZcQY+Qv2cZgmAeyXo1LePEtH/2XcqEUnbYEkSGRNhLQgiBm4QN root@java37master

4.4 send this file on the master to two slavers (only on the master)

  • scp local file remote login user name @ remote host: directory of remote host
[root@java37master ~]# scp /root/.ssh/authorized_keys  root@java37slaver1:/root/.ssh
The authenticity of host 'java37slaver1 (192.168.56.129)' can't be established.
ECDSA key fingerprint is SHA256:S8/WIYoiYeX3yEKdkcW7blvb6VvY5K7HLdIgKgvE4FI.
ECDSA key fingerprint is MD5:62:6b:14:c3:14:cb:de:8c:a9:2b:6a:07:88:dd:07:18.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'java37slaver1,192.168.56.129' (ECDSA) to the list of known hosts.
root@java37slaver1's password: 
authorized_keys                                                                       100% 1199     1.0MB/s   00:00    
[root@java37master ~]# scp /root/.ssh/authorized_keys  root@java37slaver2:/root/.ssh
The authenticity of host 'java37slaver2 (192.168.56.130)' can't be established.
ECDSA key fingerprint is SHA256:YVz8ctecowRD4iP5833WxJRi1gd4O4vm1piMKh5Xrls.
ECDSA key fingerprint is MD5:93:96:8e:81:79:43:92:13:46:86:6b:53:31:80:58:5b.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'java37slaver2,192.168.56.130' (ECDSA) to the list of known hosts.
root@java37slaver2's password: 
authorized_keys                                                                       100% 1199   930.5KB/s   00:00 

4.5 realize the secret free login of three servers through ssh (all three do)

  • Test 9 times
[root@java37master ~]# ssh java37master
[root@java37master ~]# ssh java37slaver1
[root@java37master ~]# ssh java37slaver2
  • Exit current account: exit
[root@java37master ~]# exit
 Logout
Connection to java37master closed.

5 all three servers publish projects

  • mysql using master
  • Package the project as a war package
  • Put the war package under / tomcat9/webapps
  • Start tomcat for three servers
  • Monitor the log output information of tomcat: tail -f xxx
  • Access via browser:

1 nginx

1.1 concept

Nginx (engine x) Is a high-performance HTTP Reverse proxy web The server also provides IMAP/POP3/SMTP Service.
Nginx By Igor·Sesoyev is the second most visited Russian Rambler.ru Site (Russian):Рамблер)Developed, first public version 0.1.0 Issued on October 4, 2004.
It is famous for its stability, rich feature set, simple configuration file and low consumption of system resources

1.2 installation: only installed on the master

http://nginx.org/en/download.html

  • Installing nginx dependencies
[root@java37master ~]# yum install yum-utils
...
  • Create configuration information for yum
[root@java37master ~]# touch /etc/yum.repos.d/nginx.repo #Create yum profile
[root@java37master ~]# vim /etc/yum.repos.d/nginx.repo 
[root@java37master ~]# cat /etc/yum.repos.d/nginx.repo  #Add the following code to specify the download stable version
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true

[nginx-mainline]
name=nginx mainline repo
baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/
gpgcheck=1
enabled=0
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true

  • Installing nginx
[root@java37master ~]# yum install nginx  -y

1.3 close selinux, the anti-virus software of linux

  • View the status of selinux
[root@java37master ~]# getenforce
Enforcing  #In a defensive state
  • Modify configuration file to cancel startup
[root@java37master ~]# vim /etc/selinux/config
[root@java37master ~]# cat /etc/selinux/config
# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
#     enforcing - SELinux security policy is enforced.
#     permissive - SELinux prints warnings instead of enforcing.
#     disabled - No SELinux policy is loaded.
SELINUX=disabled #Cancel startup
# SELINUXTYPE= can take one of three values:
#     targeted - Targeted processes are protected,
#     minimum - Modification of targeted policy. Only selected processes are protected. 
#     mls - Multi Level Security protection.
SELINUXTYPE=targeted 
  • Restart the master
reboot

1.4 start up and test ngixn

[root@java37master ~]# systemctl start nginx    #Turn on nginx
[root@java37master ~]# systemctl restart nginx  #Restart nginx
[root@java37master ~]# systemctl stop nginx     #Close nginx
[root@java37master ~]# systemctl status nginx   #View nginx status
  • Access nginx through the browser: the default port of nginx is port 80 like http

2. Nginx implements reverse proxy

2.1 concept

adopt nginx Provide a unified access portal for multiple servers in the server cluster

2.2 realize its function: only execute on the master

  • Create file:
[root@java37master conf.d]# touch /etc/nginx/conf.d/cloud37.conf
  • Edit file: specify the resolution rules for the url
[root@java37master conf.d]# vim  /etc/nginx/conf.d/cloud37.conf
[root@java37master conf.d]# cat /etc/nginx/conf.d/cloud37.conf
#Name the cluster corresponding to the three servers
upstream cloudserver
{
  server 192.168.56.128:8080;
  server 192.168.56.129:8080;
  server 192.168.56.130:8080;
}
#Provide server configuration to implement reverse proxy url conversion rules
server
{
  #Specifies the port on which nginx listens
  listen  80;
  #Specify the ip address of the nginx host and the ip address of the master
  server_name  192.168.56.128;
  #Specifies the conversion rule for the url
  location  /cloud37{
      proxy_pass  http://cloudserver/java37_08_cloud;
  }
  #Browser request: http://192.168.56.128:80/cloud37 Will be parsed by nginx as
  #http://192.168.56.128:8080/java37_08_cloud
  #http://192.168.56.129:8080/java37_08_cloud
  #http://192.168.56.130:8080/java37_08_cloud
}
  • Restart ngixn
[root@java37master conf.d]# systemctl restart nginx.service
  • Modify project: all requests are changed to http://192.168.56.128:80/cloud37
<!-- There are three main areas that need to be improved: 1 style and script label-->
<!-- There are three main areas that need to be improved: 2 ajax of url-->
<!-- There are three main areas that need to be improved: 3 img of src-->
<html>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="c"  uri="http://java.sun.com/jsp/jstl/core"%>
<%@taglib prefix="fmt"  uri="http://java.sun.com/jsp/jstl/fmt"%>
<head>
        <!--#Add and define the address of a pagecore domain attribute record nginx -- >
        <c:set  var="path" value="http://192.168.56.128:80/cloud37"/>
        <title>Cloud disk project</title>
        <!--1.1 introduce min.css-->
        <link type="text/css" rel="stylesheet" href="${pageScope.path}/bootstrap-3.3.7-dist/css/bootstrap.min.css"/>
        <!--1.2 introduce bootstrap-theme.min.css-->
        <link type="text/css" rel="stylesheet" href="${pageScope.path}/bootstrap-3.3.7-dist/css/bootstrap-theme.min.css"/>

        <!--2.1 introduce bootstrap-table.min.css-->
        <link type="text/css" rel="stylesheet" href="${pageScope.path}/bootstrap-3.3.7-dist/css/bootstrap-table.min.css"/>

        <!-- 3.1 bootstarp-editable Basic usage: 1 Introduction bootstrap-editable.css -->
        <link type="text/css" rel="stylesheet" href="${pageScope.path}/bootstrap-3.3.7-dist/css/bootstrap-editable.css" />

        <!--1.3 introduce jquery-->
        <script type="text/javascript" src="${pageScope.path}/js/jquery-3.3.1.min.js"></script>
        <!--1.4 introduce bootstrap.min.js-->
        <script type="text/javascript" src="${pageScope.path}/bootstrap-3.3.7-dist/js/bootstrap.min.js"></script>

        <!--2.2 introduce bootstrap-table.min.js-->
        <script type="text/javascript" src="${pageScope.path}/bootstrap-3.3.7-dist/js/bootstrap-table.min.js"></script>
        <!--2.3 introduce bootstrap-table-editable.min.js-->
        <script type="text/javascript" src="${pageScope.path}/bootstrap-3.3.7-dist/js/bootstrap-table-editable.min.js"></script>
        <!--2.4 introduce bootstrap-table-zh-CN.min.js-->
        <script type="text/javascript" src="${pageScope.path}/bootstrap-3.3.7-dist/js/bootstrap-table-zh-CN.min.js"></script>

        <!--3.1 introduce jquery.serializejson.min.js use serializeJSON method-->
        <script type="text/javascript" src="${pageScope.path}/js/jquery.serializejson.min.js"></script>

        <!--3.3 bootstarp-editable Basic usage: 2 Introduction bootstrap-editable.min.js -->
        <script type="text/javascript"
                src="${pageScope.path}/bootstrap-3.3.7-dist/js/bootstrap-editable.min.js"></script>
</head>

<body>
        <!--0:Define global variables-->
        <script type="text/javascript">
            var uid;
            var dfu="0";
            var path="${pageScope.path}/"; //Change the way the path is written so that the core tag library does not appear
        </script>
 
        <!--Login mode box: similar to pop-up box-->
        <div class="modal fade" id="userLoginModal" >
            <div class="modal-dialog" role="document">
                <div class="modal-content">
                           ...
                            <div class="form-group">
                                <label class="control-label">Check&nbsp;card&nbsp;Code:</label>
                                <img src="${pageScope.path}/user/yzmImg.action"  id="img_yzm" />
                                <input type="text" class="form-control"  name="uyzm" value="1234">
                            </div>
                        </form>
                    </div>
                         ...
                </div>
            </div>
        </div>
        <script type="text/javascript">
                 .....
                //Change verification code
                $("#img_yzm").bind("click",function(){
                     $("#img_yzm").attr("src","${pageScope.path}/user/yzmImg.action?n="+Math.random());
                });
            });
        </script>
</body>
</html>
  • Project clean package
  • Close tomcat of the three servers and clear java37 under / webapps*
  • Add the war to webapps
  • Open tomcat
  • Enable log monitoring
  • Access via browser: http://nginxip:nginxport/cloud37/index.jsp
Note: the resource name must be added when requesting

3 nginx to achieve load balancing

3.1 concept

Load balancing: so is load balancing Nginx It is a common function. The greater the number of visits per unit time of a server, the greater the pressure on the server. When it exceeds its capacity, the server will collapse. In order to avoid server crash and give users a better experience, we share the server pressure through load balancing.
We can build many servers to form a server cluster. When users visit the website, they first visit an intermediate server, let the intermediate server select a server with less pressure in the server cluster, and then introduce the access request into the server.
In this way, each user's access will ensure that the pressure of each server in the server cluster tends to balance, share the server pressure and avoid server collapse.
Generally, the reverse agent needs to be configured at the same time for load balancing configuration, and jump to load balancing through the reverse agent.
nginx According to the performance of the server, the proportion of processing requests is reasonably allocated according to the policy

3.2 method

change nginx Profile for:/etc/nginx/conf.d/cloud37.conf of cloudserver information

3.3 load balancing strategy

1 rotation training

Default policy:
All servers process requests in turn
 Suitable for clusters with the same server performance
upstream cloudserver
{
  #Default rotation training
  server 192.168.56.128:8080;
  server 192.168.56.129:8080;
  server 192.168.56.130:8080;
}

2 weight

Set the proportion of requests processed based on server performance
 It is suitable for clusters with large differences in server performance
upstream cloudserver
{
   #1 rotation training default
   #2 weight
   #  #weight: specify scale
   #    #fail_timeout: Specifies the maximum wait time
  server 192.168.56.128:8080 weight=1 fail_timeout=10s;
  server 192.168.56.129:8080 weight=1 fail_timeout=10s;
  server 192.168.56.130:8080 weight=4 fail_timeout=10s;
}
  • Please restart nginx
systemctl restart nginx

3 ip_hash

Set a number for each server: start from 0
 client ip of hashcode value%Number of servers =n  : Let number be n Your server processes this request
 Features: all requests from the same client are only processed by the server
upstream cloudserver
{
   #1 rotation training default
   #2 weight
   #  #weight: specify scale
   #    #fail_timeout: Specifies the maximum wait time
   #server 192.168.56.128:8080 weight=1 fail_timeout=10s;
   #server 192.168.56.129:8080 weight=1 fail_timeout=10s;
   #server 192.168.56.130:8080 weight=4 fail_timeout=10s;
   
   #3ip_hash
   ip_hash;
   server 192.168.56.128:8080 fail_timeout=10s;
   server 192.168.56.129:8080 fail_timeout=10s;
    server 192.168.56.130:8080 fail_timeout=10s;
}

4 fair

nginx After receiving the request, the whole server cluster will be asked to give the request to the first responding server
upstream cloudserver
{
  server 192.168.254.128:8080;
  server 192.168.254.129:8080;
  server 192.168.254.130:8080; 
  fair;
}
# Error in fair test:: need to add plug-in::: failed to add plug-in! yum installation does not have a corresponding configuration file
#unknown directive "fair" in /etc/nginx/conf.d/cloud37.conf:23
#Error log location of nginx: / var / log / nginx / error log

5 url_hash

nginx On request url of hashcode%Number of servers=n Give the request to the number n Server
upstream cloudserver
{
   #1 rotation training default
   #2 weight
   #  #weight: specify scale
   #    #fail_timeout: Specifies the maximum wait time
   #server 192.168.56.128:8080 weight=1 fail_timeout=10s;
   #server 192.168.56.129:8080 weight=1 fail_timeout=10s;
   #server 192.168.56.130:8080 weight=4 fail_timeout=10s;
   
   #3ip_hash
   #ip_hash;
   #server 192.168.56.128:8080 fail_timeout=10s;
   #server 192.168.56.129:8080 fail_timeout=10s;
   #server 192.168.56.130:8080 fail_timeout=10s;
   #
   
   #4 fair
   #server 192.168.56.128:8080;
   #server 192.168.56.129:8080;
   #server 192.168.56.130:8080;
   #fair;
   #
   
   #5 url_hash
   server 192.168.56.128:8080 fail_timeout=10s;
   server 192.168.56.129:8080 fail_timeout=10s;
   server 192.168.56.130:8080 fail_timeout=10s;
   hash $request_uri;
}

4 dynamic and static separation

4.1 concept

Static resources(html/css/js/img) No dependency required web The resolution of the application server is directly placed in the nginx Respond directly to the client when accessing
 Improve response efficiency and reduce server pressure

4.2 modify the project

1 hold jsp Convert to html
2 hold style and script All paths are changed to relative paths
3 hold<c:set delete
4 Give directly path Assignment: var path = "http://192.168.56.128:80/cloud37/";
5 Remove all ${}
  $(function () {
        //var user = "${sessionScope.user}";
        //alert(user);
        //if (user) {
        //    uid = "${sessionScope.user.uid}";
        //    setTableData();
        //} else {
            $("#Userloginmodal ". Modal (" show "); / / the login mode box pops up
        //}
    });
6 change img Connection of
    <div class="form-group">
                        <label class="control-label">Check&nbsp;card&nbsp;Code:</label>
                        <img src="user/yzmImg.action" id="img_yzm"/>
                        <input type="text" class="form-control" name="uyzm" value="1234">
    </div>
    
    ...
    //Change verification code
    $("#img_yzm").bind("click", function () {
         $("#img_yzm").attr("src", "user/yzmImg.action?n=" + Math.random());
    });

4.3 store all static resources in a folder cloud37

4.4 delete these static resources in the project

4.5 start tomcat

  • Project clean package
  • Close three tomcat
  • Clear java37 under webapps*
  • Upload war to webapps again
  • Turn on three servers

4.6 upload the static resource folder cloud37 to the / usr/share/nginx folder

[root@java37master bin]# cd /usr/share/nginx
[root@java37master nginx]# ll
 Total consumption 0
drwxr-xr-x  6 root root 102 12 June 22-17:52 cloud37
drwxr-xr-x. 2 root root  40 12 November 22:06 html

4.7 change the configuration file of nginx and restart

[root@java37master conf.d]# pwd
/etc/nginx/conf.d
[root@java37master conf.d]# vim cloud37.conf  #Change profile
[root@java37master conf.d]# systemctl restart nginx.service #Restart nginx
[root@java37master conf.d]# cat cloud37.conf 
#Name the cluster corresponding to the three servers
upstream cloudserver
{
   #1 rotation training default
   #2 weight
   #  #weight: specify scale
   #    #fail_timeout: Specifies the maximum wait time
   #server 192.168.56.128:8080 weight=1 fail_timeout=10s;
   #server 192.168.56.129:8080 weight=1 fail_timeout=10s;
   #server 192.168.56.130:8080 weight=4 fail_timeout=10s;
   
   #3ip_hash
   #ip_hash;
   #server 192.168.56.128:8080 fail_timeout=10s;
   #server 192.168.56.129:8080 fail_timeout=10s;
   #server 192.168.56.130:8080 fail_timeout=10s;
   #
   
   #4 fair
   #server 192.168.56.128:8080;
   #server 192.168.56.129:8080;
   #server 192.168.56.130:8080;
   #fair;
   #
   
   #5 url_hash
   server 192.168.56.128:8080 fail_timeout=10s;
   server 192.168.56.129:8080 fail_timeout=10s;
   server 192.168.56.130:8080 fail_timeout=10s;
   hash $request_uri;
}
#Provide server configuration to implement reverse proxy url conversion rules
server
{
  #Specifies the port on which nginx listens
  listen  80;
  #Specify the ip address of the nginx host and the ip address of the master
  server_name  192.168.56.128;

   #Dynamic and static separation
  location ~ .*\.(html|css|js|eot|svg|ttf|woff|woff2)$
  {
           root  /usr/share/nginx;
  }

  #Specifies the conversion rule for the url
  location  /cloud37{
      proxy_pass  http://cloudserver/java37_08_cloud;
  }
  #Browser request: http://192.168.56.128:80/cloud37 Will be parsed by nginx as
  #http://192.168.56.128:8080/java37_08_cloud
  #http://192.168.56.129:8080/java37_08_cloud
  #http://192.168.56.130:8080/java37_08_cloud
}

4.8 browser access: static resource index html

Topics: Linux