四层负载均衡和http协议

四层负载均衡和七层负载均衡的区别

四层和七层负载均衡的区别
    四层负载均衡数据包在底层就进行了分发,而七层负载均衡数据包则是在最顶层进行分发、由此可以看出,七层负载均衡效率没有四负载均衡高。
    但七层负载均衡更贴近于服务,如:http协议就是七层协议,我们可以用Nginx可以作会话保持,URL路径规则匹配、head头改写等等,这些是四层负载均衡无法实现的。
    
    1、四层负载均衡不识别域名,七层负载均衡识别域名
    2、七层负载均衡是基于HTTP协议,四层是基于TCP/ip协议。# HTTP协议也是基于TCP/IP协议的

四层的使用场景

1、MySQL服务
2、SSH代理端口
3、网站的负载均衡代理
4、四层+七层来做负载均衡,四层可以保证七层的负载均衡的高可用性;

![](C:\Users\Lenovo\Pictures\Camera Roll\四层负载均衡.jpg)

四层负载均衡特点

1、四层负载均衡仅能转发TCP/IP协议、UDP协议、通常用来转发端口,如:tcp/22、udp/53;
2、四层负载均衡可以用来解决七层负载均衡端口限制问题;(七层负载均衡最大使用65535个端口号)
3、四层负载均衡可以解决七层负载均衡高可用问题;(多台后端七层负载均衡能同时的使用)
4、四层的转发效率比七层的高得多,但仅支持tcp/ip协议,不支持http和https协议;
5、通常大并发场景通常会选择使用在七层负载前面增加四层负载均衡。

四层负载均衡语法

Syntax: stream { ... }
Default:    —
Context:    main

1、创建一个IP连接池(存放web服务器的IP的)
	upstream 
	
	格式:
        stream {
            upstream [连接池的名称] {
            [存放的IP]
            server [ip1];
            server [ip2];
            }
        }
2、往IP连接池中丢请求。
	proxy_pass [连接池的名称];
    
# 四层负载均衡stream模块跟http模块在同一级别,不能配置在http里面

四层负载均衡实践

四层负载均衡实践之前提准备

1、必须是官网编译安装
	里面包含:--with-stream参数   # 一定要有这个参数才能使用四层负载均衡

2、使用四层负载均衡必须是官网镜像安装的,必要参数为:--with-stream

3、编辑镜像源
	进入nginx.org  ---> documentation ---> RHEL/CentOS
	
4、编译
	[root@lb02 ~]# vim /etc/yum.repos.d/nginx.repo
-----------------------------------------------------------------------------------------------------
[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
-----------------------------------------------------------------------------------------------------

5、安装
	[root@lb02 yum.repos.d]# yum clean all
	[root@lb02 yum.repos.d]# yum makecache
	[root@lb02 yum.repos.d]# yum install nginx
	
6、修改nginx配置文件
	把http模块全部删掉,因为四层负载均衡不用http
	#添加
	[root@lb02 yum.repos.d]# vim /etc/nginx/nginx.conf
        stream {

       include /etc/nginx/stream.conf/*.conf;
    }

四层负载均衡实践

1、修改nginx配置文件
	[root@lb02 yum.repos.d]# vim /etc/nginx/nginx.conf 
-----------------------------------------------------------------------------------------------------
user  nginx;
worker_processes  auto;

error_log  /var/log/nginx/error.log notice;
pid        /var/run/nginx.pid;

events {
    worker_connections  1024;
}

stream {
   # 指定文件
   include /etc/nginx/stream.conf/*.conf;
}
-----------------------------------------------------------------------------------------------------

2、创建文件并编写配置文件
	[root@lb02 yum.repos.d]# mkdir /etc/nginx/stream.conf
    [root@lb02 yum.repos.d]# cd /etc/nginx/stream.conf
    [root@lb02 stream.conf]# vim nginx.conf
-----------------------------------------------------------------------------------------------------
upstream nginx {
    server 172.16.1.5:80;
}

server {
    listen 80;
    proxy_pass nginx;
}
-----------------------------------------------------------------------------------------------------
    [root@lb02 stream.conf]# nginx -t
    [root@lb02 stream.conf]# systemctl restart nginx
    
3、浏览器测试
	192.168.15.6  game.test.com

四层负载均衡案例

案例1:要求使用192.168.15.6的1234端口链接192.168.15.5的22端口
   1、编写配置文件
       [root@lb02 stream.conf]# pwd
       /etc/nginx/stream.conf
       [root@lb02 stream.conf]# vim ssh.conf
----------------------------------------------------------------------------------------------------- 
upstream ssh {
    server 172.16.1.5:22;
}

server {
    listen 1234;
    proxy_pass ssh;
}
----------------------------------------------------------------------------------------------------- 
        [root@lb02 stream.conf]# nginx -t
        [root@lb02 stream.conf]# systemctl restart nginx
        
	2、测试
		[root@lb02 stream.conf]# ssh 192.168.15.6 -p 1234
案例2:要求使用192.168.15.6的33060端口代理192.168.15.51的3306端口
1、编写配置文件
    [root@lb02 stream.conf]# pwd
    /etc/nginx/stream.conf
    [root@lb02 stream.conf]# vim mysql.conf
-----------------------------------------------------------------------------------------------------
upstream mysql {
    server 172.16.1.51:3306;
}

server {
    listen 33060;
    proxy_pass mysql;
    proxy_connect_timeout 1s;
    proxy_timeout 3s;
}
-----------------------------------------------------------------------------------------------------
        [root@lb02 stream.conf]# nginx -t
        [root@lb02 stream.conf]# systemctl restart nginx
        
    2、测试
    	[root@db01 ~]# systemctl restart mariadb   # 启动数据库
    	[root@db01 ~]# mysql -uroot -p123456 -h192.168.15.6 -P33060   # 测试连接数据库

HTTPS

HTTPS简介

1、什么是HTTPS: # HTTPS是主要用来加密传输
    为什么需要使用HTTPS,因为HTTP不安全,当我们使用http网站时,会遭到劫持和篡改,如果采用https协议,那么数据在传输过程中是加密的,所以黑客无法窃取或者篡改数据报文信息,同时也避免网站传输时信息泄露。
 
	那么我们在实现https时,需要了解ssl协议,但我们现在使用的更多的是TLS加密协议。
 
    那么TLS是怎么保证明文消息被加密的呢?在OSI七层模型中,应用层是http协议,那么在应用层协议之下,我们的表示层,是ssl协议所发挥作用的一层,他通过(握手、交换秘钥、告警、加密)等方式,是应用层http协议没有感知的情况下做到了数据的安全加密

模拟网站劫持

1、检查nginx

[root@web01 conf.d]# nginx -V
--with-stream

2、创建证书存放目录

[root@web01 ~]# mkdir /etc/nginx/ssl_key
[root@web01 ~]# cd /etc/nginx/ssl_key/

3、生成证书

# 使用openssl命令充当CA权威机构创建证书(生产不使用此方式生成证书,不被互联网认可的黑户证书)
[root@web01 ssl_key]# openssl genrsa -idea -out server.key 2048

#生成自签证书(公钥),同时去掉私钥的密码
[root@web01 ssl_key]# openssl req -days 36500 -x509 -sha256 -nodes -newkey rsa:2048 -keyout server.key -out server.crt

4、配置nginx证书

[root@web01 nginx]# pwd
/etc/nginx/conf.d
# 注:ssh端口:443

1、编写配置文件
	[root@web01 conf.d]# vim game.conf
-----------------------------------------------------------------------------------------------------
server{
    server_name game.test.com;
    listen 443 ssl;  # ssl开启
    # 指定证书文件
    ssl_certificate /etc/nginx/ssl_key/server.crt;
    # 指定私钥文件
    ssl_certificate_key /etc/nginx/ssl_key/server.key;
    location /{
        root /usr/share/nginx/html5-mario;
        index index.html;
    }
}
-----------------------------------------------------------------------------------------------------

2、检测并重启nginx服务
    [root@web01 conf.d]# nginx -t
    [root@web01 conf.d]# systemctl restart nginx
    [root@web01 conf.d]# netstat -nutlp
    
3、浏览器测试
	输入:https://192.168.15.7/

全栈HTTPS

1、配置一个网站

1、修改配置	
	[root@web01 ~]# vim /etc/nginx/conf.d/html.conf
	[root@web01 ~]# nginx -t
	[root@web01 ~]# systemctl restart nginx
-----------------------------------------------------------------------------------------------------
server {
    listen 80;
    server_name html.test.com;

    location / {
        root /code;
        index index.html;
    }
}
-----------------------------------------------------------------------------------------------------
2、创建目录和用户
    [root@web01 ~]# mkdir /code
    [root@web01 ~]# useradd www -r -M -s /sbin/nologin 
    [root@web01 ~]# chown -R www.www /code

2、配置一个网页

[root@web01 ~]# vim /code/index.html
-----------------------------------------------------------------------------------------------------
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>学生信息注册页面</title>
</head>
<body>
<h3 align="center">学生信息注册</h3>
<form  name="stu"action="">
<table>
  <tr><td>姓名:</td><td><input type="text"name="stuName"/></td></tr>
  <tr><td>性别:</td>
      <td><input type="radio"name="stuSex"checked="checked">男
          <input type="radio"name="stuSex">女
          </td>
          </tr>
   <tr><td>出生日期</td>
       <td><input type="text"name="stuBirthday"></td>
       <td>按格式yyyy-mm-dd</td>
       </tr>
       <tr><td>学校:</td><td><input type="text"name="stuSchool"></td></tr>
       <tr><td>专业:</td>
           <td><select name="stuSelect2">
               <option selected>计算机科学与技术</option>
               <option>网络工程</option>
               <option>物联网工程</option>
               <option>应用数学</option>
               </select>
               </td>
               </tr>
               <tr><td>体育特长:</td>
                   <td colspan="2">
                      <input type="checkbox"name="stuCheck" >篮球
                      <input type="checkbox"name="stuCheck" >足球
                      <input type="checkbox"name="stuCheck" >排球
                      <input type="checkbox"name="stuCheck" >游泳
                   </td> 
               </tr>
               <tr><td>上传照片:</td><td colspan="2"><input type="file" ></td></tr>
               <tr><td>密码:</td><td><input type="password"name="stuPwd" ></td></tr>
               <tr><td>个人介绍:</td>
                   <td colspan="2"><textarea name="Letter"rows="4"cols="40"></textarea></td>
               </tr>
               <tr>
                 <td><input type="submit"value="提交" ><input type="reset"value="取消" ></td>
                 </tr>
                 </table>
                 </form>
</body>
</html>

https强制跳转项目

1、检查nginx

[root@lb01 ~]# nginx -V
--with-stream

2、创建证书存放目录

[root@lb01 ~]# mkdir /etc/nginx/ssl_key
[root@lb01 ~]# cd /etc/nginx/ssl_key/

3、生成证书

# 使用openssl命令充当CA权威机构创建证书(生产不使用此方式生成证书,不被互联网认可的黑户证书)
[root@lb01 ssl_key]# openssl genrsa -idea -out server.key 2048

#生成自签证书(公钥),同时去掉私钥的密码
[root@lb01 ssl_key]# openssl req -days 36500 -x509 -sha256 -nodes -newkey rsa:2048 -keyout server.key -out server.crt

4、配置nginx证书

[root@web01 nginx]# pwd
/etc/nginx/conf.d
# 注:ssh端口:443

1、编写配置文件
	[root@lb01 conf.d]# vim game.conf
-----------------------------------------------------------------------------------------------------
upstream game {
    server 172.16.1.8:80;
    server 172.16.1.7:80;
    server 172.16.1.9:80;
}

# 强制跳转
server {
    listen 80;
    server_name game.test.com;
    return 302 https://game.test.com;
}

server {
    server_name game.test.com;
    listen 443 ssl;

    ssl_certificate /etc/nginx/ssl_key/server.crt;
    ssl_certificate_key /etc/nginx/ssl_key/server.key;

    location / {
        proxy_pass http://game;
        include proxy_params;
    }
}
-----------------------------------------------------------------------------------------------------

2、检测并重启nginx服务
    [root@web01 conf.d]# nginx -t
    [root@web01 conf.d]# systemctl restart nginx
    [root@web01 conf.d]# netstat -nutlp
    
3、浏览器测试
	域名解析:192.168.15.5  game.test.com
	输入:去网站输入:http://192.168.15.5/
posted @ 2021-11-08 08:41  迷恋~以成伤  阅读(249)  评论(0)    收藏  举报