four loads and static and dynamic separation nginx

Ali cloud experiment
10.0.0.132 LB
10.0.0.133 web01
10.0.0.134 web02

Steps:
1. Installation nginx1.14

Obtain official website repo file
 yum  install -y nginx

2. configuration file, web server are the same

[root@web01 conf.d]# cat www.conf 
server {
    listen 80;
    server_name ali.xiao.com;
    
    location / {
        root /code/ali;
        index index.html;    
    }
}

mkdir -p /code/ali
echo "web01 10.0.0.133" >/code/ali/index.html

3. Detection of grammar, and start the service

nginx -t
systemctl start nginx

4. Configure seven load balancing
1) create proxy_params
2) create a load balancing configuration

[root@proxy conf.d]# cat proxy.conf 
upstream ali {
    server 10.0.0.133:80;
    server 10.0.0.134:80;
}

server {
    listen 80;
    server_name ali.xiao.com;

    location / {
        proxy_pass http://ali;
        include proxy_params;
    }
}

5. Configure the four load balancing
1) modifying the master configuration file /etc/nginx/nginx.conf, add the following information in the above http

# Add four agents
include /etc/nginx/conf.c/*.conf

2) four load configuration file

mkdir -p /etc/nginx/conf.c
vim /etc/nginx/conf.c/proxy.conf
stream {
    upstream ssh_web01{
        server 10.0.0.133:22;
    }
    upstream ssh_web02{
        server 10.0.0.134:22;
    }

    server {
        listen 5555;
        proxy_pass ssh_web01;
    }

    server {
        listen 6666;
        proxy_pass ssh_web02;
    }
}

 

Nginx load balancing TCP practice

Load balancing layer disposed nginx4 following requirements
1. balanced access load port 5555, the port 22 is actually serving the rear end of the web01
2. balanced access load port 6666, the port 3306 is actually in service mysql

Four load balancing based on MySQL

1) Experimental environment

lb01   172.16.1.5    10.0.0.5
web01  172.16.1.7
db01   172.16.1.71

2) Create a four-layer load balancing configuration directory

mkdir /etc/nginx/conf.c -p

3) Modify lb01 master profile
vim /etc/nginx/nginx.conf, add the following information in the above http

# Add four agents
include /etc/nginx/conf.c/*.conf

4) Create four load-balancing configuration file

[root@1b01 conf.c]# vim stream.conf 
stream {
    # Define a virtual resource pool
    upstream ssh {
        server 172.16.1.7:22;
    }

    upstream mysql {
        server 172.16.1.71:3306;
    }
    # Call the virtual resource pool
    server {
        listen 5555;
        proxy_connect_timeout 1s;
        proxy_timeout 300s;
        proxy_pass ssh;
    }
    
    server {
        listen 6666;
        proxy_connect_timeout 1s;
        proxy_timeout 300s;
        proxy_pass mysql;
    }
}

5). Syntax check, and restart the service

nginx -t
systemctl reload nginx

 

Nginx static and dynamic separation
movement separating the static and dynamic request through the middleware requests separation, the separation resources, reduce unnecessary consumption of a request, a request to reduce the delay.
Delicious: the static and dynamic separation, even if the dynamic service is not available, but the static resources are not affected

 

 1. Preparing the Environment

System Services address
centso7. 7    Load Balancing Proxy Nginx     10.0 . 1.5 
centso7. 7    Static resources Static Nginx    10.0 . 1.7 
centso7. 7    Dynamic Resource Server Nginx    10.0 . 1.8

2. Configure static resources on the server 10.0.1.7

[root@web01 conf.d]# cat df.conf 
server {
    listen 80;
    server_name ds.xiao.com;
    root /soft/code;
    index index.html;
    
    location ~* .*\.(jpg|png|gif)$ {
        root /soft/code/images;
    }
}

# 准备,目录已经静态相关图片
mkdir -p /soft/code/images
wget -O /soft/code/images/nginx.png http://nginx.org/nginx.png
systemctl reload nginx

3.在10.0.1.8服务器上配置动态资源

yum install -y tomcat
systemctl start tomcat
mkdir /usr/share/tomcat/webapps/ROOT
cd /usr/share/tomcat/webapps/ROOT/
[root@web02 ROOT]# cat java_test.jsp 
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<HTML>
    <HEAD>
        <TITLE> jsp </TITLE>
    </HEAD>
    <BODY>
        <%
        Random rand = new Random();
        out.println("<h1>Random number:</h1>");
        out.println(rand.nextInt(99)+100);
        %>
    </BODY>
</HTML>

4.在负载均衡10.0.1.5上配置调度,实现访问jsp和png

[root@1b01 conf.d]# cat ds.conf 
upstream static {
    server 172.16.1.7:80;
}

upstream java {
    server 172.16.1.8:8080;
}

server {
    listen 80;
    server_name ds.xiao.com;
    
    location / {
        root /code/soft;
        index index.html;
    }


    location ~ .*\.(jpg|png|gif)$ {
        proxy_pass http://static;
        include proxy_params;
    }

    location ~ .*\.jsp$ {
        proxy_pass http://java;
        include proxy_params;
    }
}

5.通过负载测试访问静态资源
6.通过负载测试访问动态资源
7.在负载均衡10.0.1.5整合动态和静态资源的html文件

mkdir -p /code/soft
[root@1b01 conf.d]# cat /code/soft/index.html 
<html lang="en">
<head>
    <meta charset="UTF-8" />
    <title>测试ajax和跨域访问</title>
    <script src="http://libs.baidu.com/jquery/2.1.4/jquery.min.js"></script>
</head>
<script type="text/javascript">
$(document).ready(function(){
    $.ajax({
    type:"GET",
    url:"http://ds.xiao.com/java_test.jsp",
    success: function(data) {
        $("#get_data").html(data)
    },
    error: function() {
        alert("fail!!,请刷新重试");

    }
    });
});
</script>
    <body>
        <h1>测试动静分离</>
        <img src="http://ds.xiao.com/nginx.png">
        <div id="get_data"></data>
    </body>
</html>

8.测试动态和静态资源是否能正常加载在一个html文件中
9.当使用systemctl stop nginx停止Nginx后,会发现静态内容无法访问,动态内容依旧运行正常
10.当使用systemctl stop tomcat停止tomcat后,静态内容依旧能正常访问,动态内容将不会被请求到

 

Guess you like

Origin www.cnblogs.com/xmtxh/p/12343853.html