Day 56 Nginx负载均衡-

1.1 负载均衡

    http OSI七层) 协议  https

     TCP OSI四层) 端口

http{

    upstream name {

        ip_hash;

        server 172.16.1.7 weight=1;

        server 172.16.1.8 max_fail=2 fail_timeout=10s max_conns=1000;

    }

}

proxy_pass http://name;

1.2 session 会话(服务端)

    代码默认写的session存储在WEB服务器本地,解决session会话

    会话保持        ->  ip_hash

    会话共享        ->  redisMySQL

    存客户端        ->  浏览器

cookie(客户端)

1.3 负载均衡

    F5   硬件

    LVS  软件

    Haproxy  支持四层和七层

    Nginx 仅支持7

1.4     如何配置Nginx4层负载均衡

    通过访问负载均衡的5555端口,实际是后端的web0122端口在提供服务。

    通过访问负载均衡的6666端口,实际是后端的mysql3306端口在提供服务。

   The ngx_stream_core_module module is available since version 1.9.0. This module is not built by default, it should be enabled with the --with-stream configuration parameter.

worker_processes auto;

error_log /var/log/nginx/error.log info;

events {

    worker_connections  1024;

}

stream {

    upstream backend {

        hash $remote_addr consistent;

        server backend1.example.com:12345 weight=5;

        server 127.0.0.1:12345            max_fails=3 fail_timeout=30s;

        server unix:/tmp/backend3;

    }

    server {

        listen 12345;

        proxy_connect_timeout 1s;

        proxy_timeout 3s;

        proxy_pass backend;

    }

}

 

[root@lb01 ~]# mkdir -p /etc/nginx/conf.c

[root@lb01 ~]# vim /etc/nginx/nginx.conf

----在events层下面,http层上面

include  /etc/nginx/conf.c/*.conf;

 

[root@lb01 ~]# cd /etc/nginx/conf.c/

[root@lb01 conf.c]# vim stream.conf

[root@lb01 conf.c]# cat stream.conf

stream {

    #1.定义虚拟资源池

    upstream ssh {

       server 172.16.1.7:22;

    }

 

    upstream mysql {

       server 172.16.1.51:3306;

    }

    #2.调用虚拟资源池

    server {

       listen 5555;

       proxy_pass ssh;

    }

    server {

       listen 6666;

       proxy_pass mysql;

    }

}

[root@lb01 conf.c]# systemctl restart nginx

flag-------------------------------

系统          服务         地址

CentOS7.5   proxy         10.0.0.5

CentOS7.5   Nginx        10.0.0.7

CentOS7.5   TOmcat     10.0.0.7

1.4.1 10.0.0.7准备静态资源

[root@web01 conf.d]# cat images.conf

server{

        listen 80;

        server_name ds.oldboy.com;

        root /soft/code;

        index index.html;

 

        location ~* .*\.(png|jpg|gif)$ {

                root /soft/code/images;

        }

}

1.4.2 准备目录, 以及静态相关图片

[root@web01 ~ ]# mkdir -p /soft/code/images/

[root@web01 ~ ]# wget -O /soft/code/images/nginx.png http://nginx.org/nginx.png

[root@web01 ~ ]# systemctl restart nginx

1.4.3 10.0.0.7准备java动态资源,记得移除本地的8080端口

[root@web01 ~]# yum install -y tomcat

[root@web01 ~]# systemctl start tomcat

[root@web01 ~]# mkdir /usr/share/tomcat/webapps/ROOT

[root@web01 ~]# vim /usr/share/tomcat/webapps/ROOT/java_test.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>

<HTML>

    <HEAD>

        <TITLE>JSP Test Page</TITLE>

    </HEAD>

    <BODY>

      <%

        Random rand = new Random();

        out.println("<h1>Random number:</h1>");

        out.println(rand.nextInt(99)+100);

      %>

    </BODY>

</HTML>

1.4.4 配置proxy服务,10.0.0.5,在192.168.69.112配置负载均衡代理调度, 实现访问jsppng

[root@lb01 conf.d]# cat ds_proxy.conf

upstream static {

        server 10.0.0.7:80;

}

upstream java {

        server 10.0.0.7:8080;

}

server {

        listen 80;

        server_name ds.oldboy.com;

        location / {

                root /soft/code;

                index index.html;

        }

        location ~ .*\.(png|jpg|gif)$ {

                proxy_pass http://static;

                include proxy_params;

        }

        location  ~ .*\.jsp$ {

                proxy_pass http://java;

                include proxy_params;

        }

}

[root@lb01 conf.d]# systemctl restart nginx

1.4.5 配置Hosts文件

1.4.6 在负载均衡上面添加一个html整合动态资源和静态资源

[root@lb01 ~]# mkdir /soft/code -p

[root@lb01 ~]# cat /soft/code/index.html

<html>

<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.oldboy.com/java_test.jsp",

        success: function(data) {

                $("#get_data").html(data)

        },

        error: function() {

                alert("fail!!,请刷新再试!");

        }

        });

});

</script>

        <body>

                <h1>测试动静分离</h1>

                <img src="http://ds.oldboy.com/nginx.png">

                <div id="get_data"></div>

        </body>

</html>

 

[root@lb01 ~]# systemctl restart nginx