Nginx+Tomcat load balancing + dynamic and static separation

Tomcat overview

Tomcat server is a free and open-source Web application server, which is a lightweight application server. It is commonly used in small and medium-sized systems and where there are not many concurrent users. It is the first choice for developing and debugging JSP programs. For a beginner, it can be thought that when an Apache server is configured on a machine, it can be used to respond to HTML (an application under the standard universal markup language) page access request. In fact, Tomcat is an extension of the Apache server, but it runs independently at runtime, so when you run tomcat, it actually runs as a separate process from Apache.

Ngiinx overview

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 capabilities. In fact, nginx's concurrency capabilities perform better in the same type of web server. Mainland Chinese users of nginx websites include: Baidu, JD, Sina, NetEase, Tencent, Taobao, etc.

Features

  • Support up to 50,000 concurrent connection responses
  • Possess powerful static resource processing capabilities
  • run smoothly
  • Low hardware resource usage

Nginx load balancing implementation principle

Schematic diagram of
Insert picture description hereNgxin configuration parameters

upstream server pool {}

  • After configuring the back-end server, let it provide itself with response data
    proxy_pass http://server address
  • Configure the server processing that forwards the access request to the back-end server pool

Nginx dynamic and static separation implementation principle

The service fee receives the request from the client. The static resources of the base friends also have dynamic resources. The static resources are provided by nginx, and the dynamic resources are forwarded by nginx to the back-end server to provide services.

Advantages of nginx processing static pages compared to tomcat

  • Nginx is much more efficient in processing static pages than Tomcat
  • If Tomcat requests 1000 times, Nginx requests 6000 times
  • Tomcat has a throughput of 0.6M per second, and Nginx has a throughput of 3.6M per second
  • Nginx's ability to handle static resources is 6 times that of Tomcat

bring it on! Show! !

nginx service ip: 20.0.0.11
cat01: 20.0.0.3
cat02: 20.0.0.4

Experiment①

Build Nginx server

安装环境依赖包
[root@nginx bao]# yum -y install \
> pcre-devel \
> zlib-devel \
> gcc gcc-c++ \
> make
[root@nginx bao]# useradd -M -s /sbin/nologin nginx
[root@nginx bao]# tar zxvf nginx-1.12.2.tar.gz
[root@nginx bao]# cd nginx-1.12.2/
[root@nginx nginx-1.12.2]# ./configure \
> --prefix=/usr/local/nginx \
> --user=nginx \
> --group=nginx \
> --with-http_stub_status_module \  ##统计模块
> --with-http_gzip_static_module \  ##压缩模块
> --with-http_flv_module			##视频模块
[root@nginx nginx-1.12.2]# make && make install
[root@nginx nginx-1.12.2]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/

Edit configuration file

[root@nginx nginx-1.12.2]# vim /usr/local/nginx/conf/nginx.conf
    #gzip  on;   ##找到这行,在下面添加
        upstream catserver {
    
              ##设置节点服务器,你们做的时候名字起的正规点啊哈哈哈
                        server 20.0.0.3:8080 weight=1;  ##指向cat01 IP,weight指的是权重
                        server 20.0.0.4:8080 weight=1;  ##看上面自己理解吧
                    }
    server {
    
    
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
    
    
            root   html;
            index  index.html index.htm;
        }
        location ~.*.jsp$ {
    
      ##往下加,匹配到以 jsp 结尾的网页再两台节点服务器中轮询          
          proxy_pass http://catserver;
        }
[root@nginx nginx-1.12.2]# systemctl stop firewalld
[root@nginx nginx-1.12.2]# setenforce 0

Build Tomcat server

Install and configure
two tomcat general operations

[root@cat01 bao]# tar zxvf jdk-8u91-linux-x64.tar.gz
[root@cat01 bao]# mv jdk1.8.0_91/ /usr/local/
[root@cat01 bao]# vim /etc/profile
export JAVA_HOME=/usr/local/jdk1.8.0_91
export JRE_HOME=${JAVA_HOME}/jre
export CLASSPATH=.:${JAVA_HOME}/lib:${JRE_HOME}/lib
export PATH=${JAVA_HOME}/bin:$PATH
[root@cat01 bao]# source /etc/profile
[root@cat01 bao]# java -version
java version "1.8.0_91"
Java(TM) SE Runtime Environment (build 1.8.0_91-b14)
Java HotSpot(TM) 64-Bit Server VM (build 25.91-b14, mixed mode)

[root@cat01 bao]# tar zxvf apache-tomcat-8.5.16.tar.gz
[root@cat01 bao]# mv apache-tomcat-8.5.16 /usr/local/tomcat
[root@cat01 bao]# cd /usr/local/tomcat/
[root@cat01 tomcat]# ln -s /usr/local/tomcat/bin/shutdown.sh /usr/local/bin
[root@cat01 tomcat]# ln -s /usr/local/tomcat/bin/startup.sh /usr/local/bin
[root@cat01 bao]# mkdir -p /web/cat01 
[root@cat01 bao]# vim /web/cat01/index.jsp   ##cat02 改成 /web/cat02
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<html>
 <head>
    <title>CAT01 web</title>
 </head>
 <body>
    <% out.println("This is CAT01.");%>   ##另一台配置 This is CAT02.
 </body>
</html>

[root@cat01 bao]# vim /usr/local/tomcat/conf/server.xml
148       <Host name="localhost"  appBase="webapps"   
149             unpackWARs="true" autoDeploy="true">  ##主配置文件里找到这个服字段,在下面添加,意思差不多是重新定义主页位置
150       <Context docBase="/web/cat01" path="" reloadable="false">
151       </Context>
[root@cat01 bao]# startup.sh 
Using CATALINA_BASE:   /usr/local/tomcat
Using CATALINA_HOME:   /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME:        /usr/local/jdk1.8.0_91/jre
Using CLASSPATH:       /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
Tomcat started.
[root@cat01 bao]# netstat -ntap |grep 8080
tcp6       0      0 :::8080                 :::*                    LISTEN      74552/java   

Verification experiment

Insert picture description here
Insert picture description here
The experiment is successful. When a dynamic web page is matched, the polling points to two web servers, but note that the pages of the two hosts must be the same during work

Experiment ②

Experimental requirements: nginx processes static images, Tomcat processes dynamic pages, configures
Tomcat to refer to the path, and nginx puts images. The
directory name needs to be the same as the Java project name

Tomcat01 configuration

02I won't change it, trouble

[root@cat01 bao]# vim /web/cat01/index.jsp 
<!DOCTYPE html>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="java.util.Date" %>
<%@ page import="java.text.SimpleDateFormat" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="nginx/html; charset=UTF-8">  ##位置和图的位置一样,nginx/html
<title>CAT01 web</title>
</head>
<body>
<div>This is CAT01.</div><br>
<img src="FDL.jpg">
</body>
</html>

Nginx configuration

[root@nginx /]# cd /usr/local/nginx/html/
[root@nginx html]# rz -E
rz waiting to receive.
[root@nginx html]# ls
50x.html  FDL.jpg  index.html

[root@nginx html]# vim /usr/local/nginx/conf/nginx.conf
    server {
    
    
        listen       80;
        server_name  localhost;
        ##添加下面
        location ~.*\.(gif|jpg|jpeg|png|bmp|swf|css)$ {
    
    
          root html;
          expires 30s;

Test experiment

Insert picture description hereThe experiment was successful

Guess you like

Origin blog.csdn.net/Ora_G/article/details/108385796