Linux架构nginx之负载均衡

1. 代理

将流量分配

1.1 代理的方式

# 1. 正向代理
客户找代理,代理给你ip,客户通过ip去找服务器
典型应用: VPN
# 找完代理还要找服务器


# 2. 反向代理
客户只需要找代理,不需要找服务器,
后台服务器是由代理去找的
典型应用: 负载均衡

1.2 nginx代理服务支持的协议

ngx_http_uwsgi_module    python的协议
ngx_http_fastcgi_module  php的协议
ngx_http_scgi_module     java的服务协议
ngx_http_v2_module       golang的
ngx_http_proxy_module    http协议的

1.3 nginx代理实践

"lb01代理web01"
lb01 --->  web01

1.3.1 部署web01
cd /etc/ngxin/conf.d
找到马里奥,换一下ip
vim game5.conf
systemctl restart ngxin
server {
    listen 80;
    server_name 192.168.15.7;
    location / {
        root /opt/super_marie;
        index index.html;
    }
    location / {
        
    }
}
挂载。。。。。。
1.3.2 部署lb01
  • 部署nginx
不要用yum安装nginx

# 1. 下载nginx源代码包
wget 压缩包地址
http://nginx.org/download/nginx-1.20.2.tar.gz
# 2. 解压
tar -xf nginx-1.20.2.tar.gz

# 3. 进入源代码目录
cd nginx-1.20.2

# 4. 安装依赖包
yum install openssl openssl-devel zlib zlib-devel -y

# 5. 设置编译参数
./configure --with-stream --with-http_ssl_module --with-http_gzip_static_module

# 6. 编译
make

# 7. 安装
make install

# 优化
mkdir /etc/nginx
mv /usr/local/nginx/conf/* /etc/nginx/
mkdir /etc/ngxin/conf.d

cd /etc/nginx/
vi /etc/nginx/nginx.conf

groupadd www -g 666
useradd www -u 666 -g 666 -M -r -s /sbin/nologin

cd /usr/lib/systemd/system
cat nginx.service 复制
vim /usr/lib/systemd/system/nginx.service 粘贴
"""
[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target

[Service]
Type=forking
PIDFile=/var/run/nginx.pid
ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf
ExecReload=/bin/sh -c "/bin/kill -s HUP $(/bin/cat /var/run/nginx.pid)"
ExecStop=/bin/sh -c "/bin/kill -s TERM $(/bin/cat /var/run/nginx.pid)"

[Install]
WantedBy=multi-user.target

"""

更改nginx路径 




# 软链接
ln -s /etc/nginx/nginx.conf /usr/local/nginx/conf/nginx.conf

mv /usr/local/nginx/sbin/ngxin /usr/sbin/

mkdir /var/log/nginx


systemctl daemon-reload
systemctl start nginx
  • 部署反向代理
lb01中

cat /etc/nginx/conf.d/game.conf

server {
    listen 80;
    server_name _;
    location /{
        proxy_pass http://172.16.1.7:80;
    }
}

1.4 nginx代理常用参数

1.4.1 添加发往后端服务器的请求头信息
Syntax:   proxy_set_header field value;
Default:  proxy_set_header Host $http_host;
          proxy_set_header Connection close;
Context:  http, server, location
   
# 用户请求的时候HOST的值是Linux.proxy.com,那么代理服务会向后端传递请求的还是linux.proxy.com
proxy_set_header Host $http_host;
# 将$remote_addr的值放进变量X-Real-IP中,$remote_addr的值为客户端的IP
proxy_set_header X-Real-IP $remote_addr;
# 客户端通过代理服务访问后端服务,后端服务通过该变量会记录真实客户端地址
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

1.4.2 代理到后端的TCP连接,响应,返回等超时时间
# nginx代理与后端服务器连接超时时间(代理连接超时)
Syntax:  proxy_connect_timeout time;
Default: proxy_connect_timeout 60s;
Context: http, server, location
    
# nginx代理等待后端服务器的响应时间
Syntax:  proxy_read_timeout time;
Default: proxy_read_timeout 60s;
Context: http, server, location
   
# 后端服务器数据回传给nginx代理超时时间
Syntax:  proxy_send_timeout time;
Default: proxy_send_timeout 60s;
Context: http, server, location
    
   
proxy_connect_timeout 1s;
proxy_read_timeout 3s;
proxy_send_timeout 3s;
1.4.3 proxy_buffer代理缓冲区
# nginx会把后端返回的内容先放到缓冲区当中,然后再返回给客户端,边收边传,部署全部接收完再传给客户端
Syntax: proxy_buffering on | off;
Default: proxy_buffering on;
Context: http, server, location
   
# 设置nginx代理保存用户头信息的缓冲区大小
Syntax: proxy_buffer_size size;
Default: proxy_buffer_size 4k|8k;
Context: http, server, location
   
# proxy_buffers 缓冲区
Syntax: proxy_buffers number size;
Default: proxy_buffers 8 4k|8k;
Context: http, server, location
   

proxy_buffering on;
proxy_buffer_size 8k;
proxy_buffers 8 8k;  # 8个缓冲区,每个8k

vim /etc/nginx/conf.d/game.conf

cd /etc/nginx
vim proxy_params
include
1.4.4 配置代理优化文件
vim /etc/nginx/proxy_params

proxy_set_header Hose $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_connect_timeout 10s;
proxy_read_timeout 10s;
proxy_send_timeout 10s;
proxy_buffering on;
proxy_buffer_size 8k;
proxy_buffers 8 8k;

cat game.conf
server {
    listen 80;
    server_name _;
    location / {
        proxy_pass http://172.16.1.7:80;
        include /etc/nginx/proxy_params;
    }
}

2. 负载均衡

为什么要用负载均衡

反向代理特点是用另外一个主机将流量转发到后端

负载均衡是将流量按照一定的比例转发后端

2.1 负载均衡的架构

负载均衡是将流量按照一定的比例转发后端

2.2 负载均衡的实现

将后端服务打包成一个IP连接池。
负载均衡两个部分

1. 反向代理

server {
    listen 80;
    server_name _;
    location / {
        proxy_pass http://[连接池];
    }
}


2. IP连接池
upstream [连接池名称] {
    server [ip]:[port];
    server [ip]:[port];
    server [ip]:[port];
    ...
}

web01
tar -czvf super_mairo.tar.gz super_mario

scp sper_mario.tar.gz 172.16.1.8:/opt/
   




2.2.2 负载均衡的比例
# 轮询
默认情况下,nginx负载均衡的轮询状态
upstream supermario{
    server 172.16.1.7:80;
    server 172.16.1.8:80;
    server 172.16.1.9:80;
}

# 权重
"nginx中的权重是0-100,数字越大权重越高"
upstream supermario{
    server 172.16.1.7:80 weight=9;
    server 172.16.1.8:80 weight=5;
    server 172.16.1.9:80 weight=1;
}

# ip_hash
# 每一个IP固定访问某一个后端。
upstream supermario{
    server 172.16.1.7:80 ;
    server 172.16.1.8:80 ;
    server 172.16.1.9:80 ;
    ip_hash;
}

2.3 负载均衡后端状态

状态 概述
down 当前的server暂时不参与负载均衡
backup 预留的备份服务器
max_fails 允许请求失败的次数
fail_timeout 经过max_fails失败后,服务暂停时间
# down 
暂时不分配流量
upstream supermario{
    server 172.16.1.7:80 down;
    server 172.16.1.8:80;
    server 172.16.1.9:80;
}
server {
    listen 80;
    server_name _;
    location / {
        proxy_pass http://supermarie;
        include /etc/nginx/proxy_params;
    }
}

# backup
# 只有当所有机器全部宕机,才能启动
upstream supermario{
    server 172.16.1.7:80 backup;
    server 172.16.1.8:80;
    server 172.16.1.9:80;
}
server {
    listen 80;
    server_name _;
    location / {
        proxy_pass http://supermarie;
        include /etc/nginx/proxy_params;
    }
}


# max_fails
# 允许请求失败的次数
upstream supermario{
    server 172.16.1.7:80 max_fails=3 fail_timeout=3s;
    server 172.16.1.8:80 max_fails=3 fail_timeout=3s;
    server 172.16.1.9:80 max_fails=3 fail_timeout=3s;
}
server {
    listen 80;
    server_name _;
    location / {
        proxy_pass http://supermarie;
        proxy_next_upstream error timeout invalid_header http_502 http_500 http_503 http_404;
        include /etc/nginx/proxy_params;
    }
}




# fail_timeout
# 经过max_fails失败后,服务暂停时间



# proxy_next_upstream 后端错误标识

2.4 负载均衡部署BBS

2.4.1 部署后端服务
2.4.1.1 部署Python
1、创建用户
[root@web01 opt]# groupadd django -g 888
[root@web01 opt]# useradd django -u 888 -g 888 -r -M -s /bin/sh

2、安装依赖软件
[root@web01 opt]# yum install python3 libxml* python-devel gcc* pcre-devel openssl-devel python3-devel -y
2.4.1.2 部署Django和uwsgi
3、安装Django和uwsgi
[root@web01 opt]# pip3 install django==1.11
[root@web01 opt]# pip3 install uwsgi
[root@web01 opt]# pip3 install pymysql

4、创建项目
[root@web01 opt]# unzip bbs.zip 
[root@web03 bbs]# pwd
/opt/bbs
[root@web03 bbs]# vim bbs/settings.py 
ALLOWED_HOSTS = ['*']
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'bbs',
        'USER': 'joshua',
        'PASSWORD': '123456',
        'HOST': '172.16.1.61',
        'PORT': 3306,
        'CHARSET': 'utf8'
    }
}

# 启动测试
[root@web01 bbs]# python3 manage.py runserver 0.0.0.0:8000
2.4.1.3 配置并启动
5、编辑项目配置文件
[root@localhost ~]# cat /opt/linux/myweb_uwsgi.ini 
[uwsgi]
# 端口号
socket            = :8000
# 指定项目的目录
chdir           = /opt/bbs
# wsgi文件路径
wsgi-file       = bbs/wsgi.py
# 模块wsgi路径
module          = bbs.wsgi
# 是否开启master进程
master          = true
# 工作进程的最大数目
processes       = 4
# 结束后是否清理文件
vacuum          = true

6、启动uwsgi
[root@web01 linux]# uwsgi -d --ini myweb_uwsgi.ini --uid 666

-d 	  : 以守护进程方式运行
--ini : 指定配置文件路径
--uid : 指定uid

TCP 服务

7、编辑Nginx配置文件
[root@localhost ~]# cat /etc/nginx/conf.d/python.conf 
server {
    listen 80;
    server_name py.test.com;
    location / { 
        include uwsgi_params;
        uwsgi_pass 127.0.0.1:8000;
        uwsgi_read_timeout 2;
        uwsgi_param UWSGI_SCRIPT bbs.wsgi;
        uwsgi_param UWSGI_CHDIR /opt/bbs;
        index  index.html index.htm;
        client_max_body_size 35m;
    }
}

8、重启Nginx配置
systemctl restart nginx
2.4.2、部署负载均衡
[root@lb01 conf.d]# cat python.conf 
upstream bbs {
    server 172.16.1.7:80 max_fails=3 fail_timeout=3s;
    server 172.16.1.8:80 max_fails=3 fail_timeout=3s;
    server 172.16.1.9:80 max_fails=3 fail_timeout=3s;
}

server {
    listen 80;
    server_name py.test.com;
    location / {
        proxy_pass http://bbs;
        proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_404; 
        include /etc/nginx/proxy_params;
    }
}
posted @ 2022-01-06 17:02  Joshua_jiaxue  阅读(100)  评论(0编辑  收藏  举报