nginx负载均衡

1.理解集群

1.一堆机器,做一件事,这就是服务器集群。
2.只有基于服务器集群的环境,才能实现负载均衡。

2.负载均衡集群



2.1 图解负载均衡理念

集群的意思就是,一堆机器,一群机器做一件事
具体做什么事,就是来划分集群的作用,分类了。




3.0 先得理解什么是代理的概念,才能理解什么是负载均衡

代理,说白了就是有一个 代理机器,去分别代理不同的角色
关于代理 proxy,分为正向代理,和反向代理两种,分别应用在不同的环境下。

3.正向代理


正向代理的具体用途,

通过代理服务器,突破client本身的ip访问限制,访问国外资源。
你在国内,是无法访问到国外的数据的,因为你的ip地址都被运营商检测了
你的ip无法直接访问到国外的数据,根本ip就不同。。

客户端 115.171.244.219北京市朝阳 电信
↓ (无法通信,中间有一堵墙)
youtube的服务器,ip


如何通信,基于正向代理,通过另一个ip的伪装去访问

客户端 115.171.244.219北京市朝阳 电信
↓(正向代理,连接上VPN,此时我client的ip表现,就不是115.171.244.219这个ip地址,而是代理服务器的ip地址,比如 )
是你的VPN服务器的ip地址(123.206.16.61)
↓
youtube的服务器ip(会看到请求是来自于谁?来自于 123.206.16.61)



正向代理是什么,就是 proxy代理的是client。


隐藏client真实信息,可以保护client服务器,隐藏自己的ip,免受攻击。


正向代理,proxy代理的是client

4.反向代理


反向代理,proxy 代理的就是 server服务端。

核心参数解释

proxy_set_header Host       $http_host; 
lb服务器将用户访问网站的hosts信息转发给后端节点

proxy_set_header  X-Forwarded-For   $proxy_add_x_forwarded_for ;
将用户真实的ip传递给后端的节点

proxy_connect_timeout 60s;
peoxy和server的连接超时,要求不超过75s;

proxy_send_timeout 60s;
proxy等待server回传数据的超时时间

proxy_read_timeout 60s;
proxy等待server响应的超时;

proxy_buffering on | off;
把server返回的数据先放入缓冲区,然后再返回给client,一边收数据,一边传递,而不是全部接收完再传递。

proxy_buffers  4 128k;
缓冲区的容量参数;

正向代理实验

正向代理,指的是通过代理服务器 代理浏览器/客户端去重定向请求访问到目标服务器 的一种代理服务。

正向代理服务的特点是代理服务器 代理的对象是浏览器/客户端,也就是对于目标服务器 来说浏览器/客户端是隐藏的。

你只需要,基于nginx的 proxy_pass 转发给一个后端节点就行。就可以实现正向代理。

lb-5机器

[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



yum clean all

yum install nginx -y

# 转发的参数文件
# 参数都在这里了,博客中也有解释
# proxy转发请求,且携带,保留客户端的真实信息。
# access_log去看
[root@lb-5 /etc/nginx/conf.d]#cat  /etc/nginx/proxy_params.conf 
proxy_set_header Host $http_host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_connect_timeout 30;
proxy_send_timeout 60;
proxy_read_timeout 60;
proxy_buffering on;
proxy_buffer_size 32k;
proxy_buffers 4 128k;


[root@lb-5 /etc/nginx/conf.d]#cat /etc/nginx/conf.d/proxy.conf 
server {
    listen 80;
    server_name wordpress.linux0224.cn;
    location / {
        proxy_pass http://172.16.1.7:8080;
        include /etc/nginx/proxy_params.conf;
    }
}

web-7机器wordpress

[root@web-7 ~]#groupadd www -g 666
[root@web-7 ~]#useradd www -s /sbin/nologin -M -u 666 -g 666


[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


yum clean all
yum install nginx -y



# 卸载旧环境
yum remove php-mysql-5.4 php php-fpm php-common -y

# 安装第三方epel源
rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm

rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm


yum install -y php71w-cli php71w-common php71w-devel php71w-embedded php71w-gd php71w-mcrypt php71w-mbstring php71w-pdo php71w-xml  php71w-fpm  php71w-mysqlnd  php71w-opcache php71w-pecl-memcached php71w-pecl-redis php71w-pecl-mongodb php71w-json php71w-pecl-apcu php71w-pecl-apcu-devel


sed -i '/^user/c user = www' /etc/php-fpm.d/www.conf 
sed -i '/^group/c group = www' /etc/php-fpm.d/www.conf 
systemctl restart php-fpm

yum install mariadb-server mariadb -y
systemctl start mariadb



# nginx设置
# 最终环境解决好了就2件事

# 1. nginx的配置文件

# 2是  php的源代码

1.注意我们是修改了后端节点的入口为 172.16.1.8:8080

2.修改配置文件
[root@web-8 ~]#cat  /etc/nginx/conf.d/wordpress.conf 
server{
    listen 8080;
    server_name wordpress.linux0224.cn;

    # 静态请求,资源存放路径
    root /code/wordpress;
    index index.php index.html;

    # 动态请求处理
    location ~ \.php$ {

        root /code/wordpress;
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
}

3.启动nginx(后端节点)
[root@web-8 ~]#systemctl start nginx

做好windows的域名解析

10.0.0.5   wordpress.linux0224.cn

4.2 nginx如何实现正向代理、反向代理

1. 正向代理,单纯的proxy_pass,转发给另一个机器,就是正向代理。

2. 反向代理,只要是结合着负载均衡的,就是反向代理,也就是使用proxy_pass加上upstream参数

反向代理(负载均衡)实验

之前的操作,都属于nginx的正向代理,只用到了nginx的proxy_pass这个参数。

实现七层负载均衡的模块 upstream

反向代理在nginx中的设置,就是基于如下2个参数

1. proxy_pass  请求转发发给一组服务器;

2.  upstream{} 这个关键字去定义一组服务器;

3.既然是请求转发给了一组服务器,并且这个upstream{}参数中可以设定,负载均衡的算法,
因此,这俩参数,实现了负载均衡的效果。。

实现四层负载均衡的模块,stream

官网文档的地址
https://nginx.org/en/docs/stream/ngx_stream_core_module.html
  1. 正向代理,是proxy 代理【多个】client

  2. 反向代理,是proxy代理【多个】server



什么是七层负载均衡

什么是四层负载均衡

就是从 1 ~ 4层,只有 基于 mac地址,网线,到ip地址,到这就结束。

实践mysql的四层转发

环境准备


172.16.1.5:3306 (nginx)

172.16.1.51:3306 (mysql)

172.16.1.52:3306 (mysql)

统一安装mysql(51,52)

yum install mariadb mariadb-server -y 

systemctl start mariadb

systemctl enable mariadb

mysqladmin password 'yuchaoit.cn'



授权一个用于远程连接的用户名 yuchao01,密码是chaoge666,允许在任何机器去登录

mysql -uroot -pwww.yuchaoit.cn -e "grant all privileges on *.* to yuchao01@'%'  identified by  'chaoge666'"

负载均衡配置 lb-5

# nginx配置文件
# 无须关于http的请求参数了

[root@lb-5 /etc/nginx]#cat /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 {
    upstream mysql_pool {

      server 172.16.1.51:3306;
      server 172.16.1.52:3306;
    }

    server {
      listen 0.0.0.0:3306;
            proxy_pass mysql_pool;
    }


}

# 启动nginx
[root@lb-5 /etc/nginx]#nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@lb-5 /etc/nginx]#systemctl restart nginx

客户端测试四层转发

yum install mariadb -y

nginx负载均衡(七层)

相比于正向代理,反向代理的定义如下:

反向代理,指的是浏览器/客户端并不知道自己要访问具体哪台目标服务器,只知道去访问代理服务器 ,代理服务器再通过反向代理 +负载均衡实现请求分发到应用服务器的一种代理服务。

反向代理服务的特点是代理服务器代理的对象是应用服务器,也就是对于浏览器/客户端 来说应用服务器是隐藏的。
七层负载均衡的模块是 http_upstream {}

https://nginx.org/en/docs/http/ngx_http_upstream_module.html
实现对http请求的转发


配置语法如下
# 2.定义地址池
upstream backend {
	# 是基于七层的地址池
	# 第七层的DNS协议进行转发,简单说就是支持域名的地址池
	# 
    server backend1.example.com      ;
    server backend2.example.com:8080;
    server unix:/tmp/backend3;

    server backup1.example.com:8080 ;
    server backup2.example.com:8080 ;
}

# 1.要定义虚拟主机

server {
    location / {
        proxy_pass http://backend;
    }
}

这个基本的 http七层负载均衡,语法能看懂 扣 1 看不懂 2

# 俩步骤 
1. 定义虚拟主机,指定请求发给一组服务器

server {
    location / {
        proxy_pass http://backend;
    }
}

2.定义一组服务器的地址信息

upstream 地址池的名字 {
	server  地址1;
	server  地址2;
}

简单点,最简化来理解http七层负载均衡的效果

lb-5 负载均衡,实现基于七层的负载均衡转发

[root@lb-5 /etc/nginx/conf.d]#cat web78.conf 


upstream myweb {

	server 172.16.1.7:9999;
	server 172.16.1.8:9999;

}


server {

listen 80;
server_name _;

location / {
	proxy_pass http://myweb;

}

}


web-7  就提供静态页面 

web-8  就提供静态页面 

配置统一即可

1. /etc/nginx/nginx.conf 默认配置即可

2. 写一个conf.d/my-web.conf 提供最简单的文本页面即可

server {
	listen 9999;
	server_name _;
	location / {
	
		root  /my-web/;
		index index.html;
	}
}

# 创建测试数据
mkdir /my-web/
分别写入两句话


echo 'web-8 ~~~~~~~~~'  > /my-web/index.html


echo 'web-7 !!!!!!!!!!!!!!!!!!!!!!!!!'  > /my-web/index.html

[root@web-8 /etc/nginx/conf.d]#curl 172.16.1.8:9999
web-8 ~~~~~~~~~

[root@web-7 /etc/nginx/conf.d]#curl 172.16.1.7:9999
web-7 !!!!!!!!!!!!!!!!!!!!!!!!!

访问lb-5机器即可
10.0.0.5:80
请求会分发给后端的两个机器

posted @ 2024-03-29 23:48  不太聪明的大鹅  阅读(17)  评论(0编辑  收藏  举报