代理的应用和负载均衡

代理

代理的方式

1.正向代理

找完代理之后,还需要找服务器。
应用:VPN

2、反向代理

只需要找代理,不需要找服务器。
应用:负载均衡

具体的正反代理查看https://www.cnblogs.com/zonghan/p/15777841.html

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

Nginx代理实践

准备工作

机器 ip
lb01 172.16.1.31
web01 172.16.1.8

部署lb01

1.编译安装nginx

# 下载Nginx源代码包
[root@lb01 ~]# wget https://nginx.org/download/nginx-1.20.2.tar.gz

# 解压
[root@lb01 ~]# tar -xf nginx-1.20.2.tar.gz

# 进入源代码目录
[root@lb01 ~]# cd nginx-1.20.2

# 安装依赖包
[root@lb01 nginx-1.20.2]# yum install openssl openssl-devel zlib zlib-devel -y

# 设置编译参数
[root@lb01 nginx-1.20.2]# ./configure  --with-http_gzip_static_module --with-stream --with-http_ssl_module

# 编译
[root@lb01 nginx-1.20.2]# make

# 安装
[root@lb01 nginx-1.20.2]# make install

# 优化
[root@lb01 nginx]# mkdir /etc/nginx
[root@lb01 nginx]# mv /usr/local/nginx/conf/* /etc/nginx/
[root@lb01 nginx]# mkdir /etc/nginx/conf.d
[root@lb01 nginx]# vi /etc/nginx/nginx.conf  # 把之前里面的全部删除,添加下面内容.
user  www;
worker_processes  auto;

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


events {
    worker_connections  1024;
}


http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

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



[root@lb01 nginx]# groupadd www -g 666
[root@lb01 nginx]# useradd www -u 666 -g 666 -M -r -s /sbin/nologin

[root@lb01 nginx]# 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

[root@lb01 sbin]# ln -s /etc/nginx/nginx.conf /usr/local/nginx/conf/nginx.conf
[root@lb01 sbin]# mv /usr/local/nginx/sbin/nginx /usr/sbin/
[root@lb01 sbin]# mkdir /var/log/nginx
[root@lb01 sbin]# systemctl start nginx

2.部署反向代理

[root@lb01 conf.d]# vim /etc/nginx/conf.d/supermary.conf 
server {
    listen 80;
    server_name youxi.com;
    location / {
        proxy_pass http://172.16.1.8:80;
    }
}

# 在把172.16.1.8:80   youxi.com  添加到window上的hosts文件里  在浏览器里访问youxi.com

image

Nginx代理常用参数

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;

案例

[root@lb01 conf.d]# vim /etc/nginx/conf.d/supermary.conf 
server {
    listen 80;
    server_name youxi.com;
    location / {
        proxy_pass http://172.16.1.8:80;
    }
}

1.访问超级玛丽小游戏后查看web01的日志文件

[root@lb01 conf.d]# cat /var/log/nginx/accees.log

image

2.修改配置文件

[root@lb01 conf.d]# vim /etc/nginx/conf.d/supermary.conf
server {
    listen 80;
    server_name youxi.com;
    location / {
        proxy_pass http://172.16.1.8:80;
        # 用户请求的时候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;
    }
}

3.再次访问小游戏,查看日志

image

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;
[root@lb01 conf.d]# vim /etc/nginx/conf.d/supermary.confserver {
    listen 80;
    server_name youxi.com;

    location / {
        proxy_pass http://172.16.1.8:80;
        
        proxy_connect_timeout 1s;
        proxy_read_timeout 3s;
        proxy_send_timeout 3s;

    }
}

3.proxy_buffer代理缓冲区

#nignx会把后端返回的内容先放到缓冲区当中,然后再返回给客户端,边收边传, 不是全部接收完再传给客户端
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;
[root@lb01 conf.d]# vim /etc/nginx/conf.d/supermary.conf
server {
    listen 80;
    server_name youxi.com;

    location / {
        proxy_pass http://172.16.1.8:80;

        proxy_buffering on;
        proxy_buffer_size 8k;
        proxy_buffers 8 8k;

    }
}                                                                     

4.配置nginx代理的优化文件

这9个参数是常用的,所以我们每次配置项目的时候都要加这9个参数,所以我们把它们写在一个配置文件里,写项目的时候直接用include调用

[root@lb01 ~]# vim /etc/nginx/proxy_params 
proxy_set_header Host $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;

[root@lb01 conf.d]# vim game.conf 
server {
    listen 80;
    server_name youxi.com;
    location / {
        proxy_pass http://172.16.1.8:80;
        include /etc/nginx/proxy_params;  # 在这一步直接调用外部文件就行了
    }
}

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

# 再次访问youxi.com网站  然后查看访问日志

image

负载均衡

什么是负载均衡

将请求平均的分配给后端服务器,防止单个服务器的压力过大导致宕机。

为什么要使用负载均衡

  1. 当我们的Web服务器直接面向用户,往往要承载大量并发请求,单台服务器难以负荷,我使用多台Web服务器组成集群,前端使用Nginx负载均衡,将请求分散的打到我们的后端服务器集群中,实现负载的分发。那么会大大提升系统的吞吐率、请求性能、高容灾

  2. 往往我们接触的最多的是SLB(Server Load Balance)负载均衡,实现最多的也是SLB、那么SLB它的调度节点和服务节点通常是在一个地域里面。那么它在这个小的逻辑地域里面决定了他对部分服务的实时性、响应性是非常好的。

  3. 所以说当海量用户请求过来以后,它同样是请求调度节点,调度节点将用户的请求转发给后端对应的服务节点,服务节点处理完请求后在转发给调度节点,调度节点最后响应给用户节点。这样也能实现一个均衡的作用,那么Nginx则是一个典型的SLB!
    image

负载均衡语法

# ngx_http_upstream_module
 
#语法
Syntax: upstream name { ... }
Default:    —
Context:    http
 
#例子  
# 在配置文件内
upstream supermarie {
    server 172.16.1.7:80;
    server 172.16.1.8:80;
    server 172.16.1.9:80;
}
 
server {
    ... ...
    location / {
        proxy_pass http://supermarie;
    }
}
# 这样就实现了负载均衡.

负载均衡的实现

1.准备工作

服务器 ip
lb01 172.16.1.31
web01 172.16.1.7
web02 172.16.1.8
web03 172.16.1.9

并且每台机器上都配置好超级玛丽小游戏

2.服务器

将后端服务打包成一个IP连接池。
[root@lb01 conf.d]# vim supermary.conf
# 定义一个IP连接池
upstream supermary {
    server 172.16.1.7:80;
    server 172.16.1.8:80;
    server 172.16.1.9:80;
}

server {
    listen 80;
    server_name youxi.com;
    location / {
        proxy_pass http://supermary;
        include /etc/nginx/proxy_params;
    }
}

3.验证

image

轮询

默认情况下,Nginx负载均衡的轮询状态,即接收的请求按照顺序依次分配

1.负载均衡服务器

[root@lb01 conf.d]# vim supermary.conf
# 定义一个IP连接池
upstream supermary {
    server 172.16.1.7:80;
    server 172.16.1.8:80;
    server 172.16.1.9:80;
}

server {
    listen 80;
    server_name youxi.com;
    location / {
        proxy_pass http://supermary;
        include /etc/nginx/proxy_params;
    }
}

2.后端服务器

# 在web01服务器
[root@web01 Super_Mary]# pwd
/opt/Super_Mary
[root@web01 Super_Mary]# echo web01 > web.html

# 在web02服务器
[root@web02 Super_Mary]# pwd
/opt/Super_Mary
[root@web02 Super_Mary]# echo web02 > web.html

# 在web03服务器
[root@web03 Super_Mary]# pwd
/opt/Super_Mary
[root@web03 Super_Mary]# echo web03 > web.html

3.验证

在浏览器输入http://youxi.com/web.html,显示的结果为

image

结果确实是平均分配的

权重

1.权重

Nginx中的权重0-100,数字越大,权重越高。

2.语法

upstream supermary {
    server 172.16.1.7:80 weight=9;
    server 172.16.1.8:80 weight=5;
    server 172.16.1.9:80 weight=1;
}

3.验证

在浏览器输入http://youxi.com/web.html,显示的结果为

image

ip_hash(固定ip)

每一个IP固定访问某一个后端。

1.负载均衡服务器

[root@lb01 conf.d]# vim supermary.conf
upstream supermary {
    server 172.16.1.7:80;
    server 172.16.1.8:80;
    server 172.16.1.9:80;
    ip_hash;  # 在这定义
}

server {
    listen 80;
    server_name youxi.com;

    location / {
        proxy_pass http://supermary;
        include /etc/nginx/proxy_params;
    }
}

2.验证

在浏览器输入http://youxi.com/web.html,显示的结果为

image

由此可得ip已经被固定访问一台服务器

负载均衡后端的状态

状态 概述
down 当前的server暂时不参与负载均衡
backup 预留的备份服务器
max_fails 允许请求失败的次数
fail_timeout 经过max_fails失败后, 服务暂停时间

down

指定服务器暂时不分配流量

[root@lb01 conf.d]# vim supermary.conf
upstream supermary {
    server 172.16.1.7:80 down;  # 指定这台服务器暂不分配流量
    server 172.16.1.8:80;
    server 172.16.1.9:80;
}

server {
    listen 80;
    server_name youxi.com;
    location / {
        proxy_pass http://supermary;
        include /etc/nginx/proxy_params;
    }
}

image

所以不访问web01了

backup

只有当所有的机器全部宕机,才能启动。

[root@lb01 conf.d]# vim supermary.conf
upstream supermary {
    server 172.16.1.7:80 backup;  # 在这调用
    server 172.16.1.8:80;
    server 172.16.1.9:80;
}

server {
    listen 80;
    server_name youxi.com;
    location / {
        proxy_pass http://supermary;
        include /etc/nginx/proxy_params;
    }
}

只有web02和web03都宕机web01才会被分配流量,类似于启动备用隐藏能源

max_fails、fail_timeout

max_fails表示允许请求失败的次数,fail_timeout表示经过max_fails失败后, 服务暂停的时间。

需要与proxy_next_upstream配合使用,由proxy_next_upstream标识错误

作用: 如果其中有一台服务器出了问题,会把它标识然后会把它排除,过会再把流量分配给它

[root@lb01 ~]# vim /etc/nginx/conf.d/game.conf 
upstream supermary {
    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 youxi.com;
    location / {
        proxy_pass http://supermary;
        proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_404; 
        include /etc/nginx/proxy_params;
    }
}
error             # 与服务器建立连接,向其传递请求或读取响应头时发生错误;
timeout           # 在与服务器建立连接,向其传递请求或读取响应头时发生超时;
invalid_header    # 服务器返回空的或无效的响应;
http_500          # 服务器返回代码为500的响应;
http_502          # 服务器返回代码为502的响应;
http_503          # 服务器返回代码为503的响应;
http_504          # 服务器返回代码504的响应;
http_403          # 服务器返回代码为403的响应;
http_404          # 服务器返回代码为404的响应;
http_429          # 服务器返回代码为429的响应(1.11.13);
non_idempotent    # 通常,请求与 非幂等 方法(POST,LOCK,PATCH)不传递到请求是否已被发送到上游服务器(1.9.13)的下一个服务器; 启用此选项显式允许重试此类请求;
off               # 禁用将请求传递给下一个服务器。

负载均衡部署BBS

部署后端服务

因为之前我们把web01里BBS部署成功了,所以只需要部署web02和web03上的BBS

1.部署Python

在web02、03上
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.部署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

# 因为之前在web01里已经进行了数据库迁移,所以现在db01上已经有bbs的数据库信息了,就不需要再次做迁移

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': 'root',
        'PASSWORD': '123',
        'HOST': '172.16.1.61',
        'PORT': 3306,
        'CHARSET': 'utf8'
    }
}

# 启动测试
[root@web01 bbs]# python3 manage.py runserver 0.0.0.0:8000

3.配置并启动

5、编辑项目配置文件
[root@localhost ~]# vim /opt/bbs/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

7、编辑Nginx配置文件
[root@localhost ~]# vim /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

部署负载均衡

[root@lb01 conf.d]# vim 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;
    }
}

在window上的hosts文件加入172.16.1.5 bbs.test.com 再访问bbs.test.com
image

posted @   zong涵  阅读(51)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
点击右上角即可分享
微信分享提示