image

一、代理服务

  • 什么是代理,代理是一个非常常用的概念,比如某个国外的游戏被国内某个公司代理,我们就可以通过国内公司的服务器进行游戏了。代理和这个有些类似,就是代替客户端或者服务端进行工作的作用。

1、 代理分为正向代理和反向代理。

1.1正向代理:

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

正向代理:
正向代理是常规的代理,为了减少避免用户直接访问服务器,造成服务器压力过大,内网用
户要通过代理服务器才能访问外网。

过程:用户向代理服务器发出请求,代理服务器会先查看自身的缓存,如果缓存中有用户请
求的内容,就直接将这个数据内容发给用户,就不向外网服务器请求了。如果代理服务器内
部缓存没有用户的请求内容,那么代理服务器会代替用户向外部服务器发送请求进行访问,
获得外部服务器的响应后自身留一份缓存再将内容发给用户。这样下一次有用户请求相同的
内容时,也不需要请求外部服务器了。

原理:内网用户将请求发给代理服务器,代理服务器根据用户需求,向真正的web服务器发
出请求,然后获取到网页内容之后,在本地缓存然后发给用户。

缺点:用户要在浏览器进行手动的代理配置,添加代理服务器地址和端口信息。

1.2反向代理:

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

反向代理:
反向代理是外部的客户请求要通过代理服务器才能访问内部服务器,将服务器内部化的
操作。

原理:外网服务器(客户端)访问正常的域名或者IP,其实访问的是代理服务器,代理服
务器帮助客户端请求页面,在代理服务器上缓存,然后再发送给客户端。

反向代理服务器一般用于给网站加速

ps: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

2、部署web01

[root@web01 conf.d]# cat game5.conf 
server {
    listen 80;
    server_name 192.168.15.7;
    location / {
        root /opt/Super_Marie;
	index index.html;
    }
    location ~ /images {
        root /opt/image;
    }
}

image

2.1、部署lb01

  • 部署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]# 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 
  • 部署反向代理

[root@lb01 conf.d]# cat /etc/nginx/conf.d/game.conf 
server {
    listen 80;
    server_name _;
    location / {
        proxy_pass http://172.16.1.7:80;
    }
}

2.2、Nginx代理常用参数

  • 添加发往后端服务器的请求头信息

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;

2.3、代理到后端的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;

2.4、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

2.5、配置代理优化文件

[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]# cat game.conf 
server {
    listen 80;
    server_name _;
    location / {
        proxy_pass http://172.16.1.7:80;
        include /etc/nginx/proxy_params;
    }
}

image

二、负载均衡

1、什么是负载均衡

# 1)负载均衡(Load Balance)建立在现有网络结构之上,它提供了一种廉价有效透
明的方法扩展网络设备和服务器的带宽、增加吞吐量、加强网络数据处理能力、提高网
络的灵活性和可用性。负载均衡有两方面的含义:首先,大量的并发访问或数据流量分
担到多台节点设备上分别处理,减少用户等待响应的时间;其次,单个重负载的运算分
担到多台节点设备上做并行处理,每个节点设备处理结束后,将结果汇总,返回给用户
,系统处理能力得到大幅度提高。

# 2)简单来说就是:其一是将大量的并发处理转发给后端多个节点处理,减少工作响
应时间;其二是将单个繁重的工作转发给后端多个节点处理,处理完再返回给负载均衡
中心,再返回给用户。目前负载均衡技术大多数是用于提高诸如在Web服务器、FTP服务
器和其它关键任务服务器上的Internet服务器程序的可用性和可伸缩性。

1.1、负载均衡的架构

  • 通过代理将流量按照一定的比例,转发到后端。

1.2、实现负载均衡

将后端服务打包成一个IP连接池。

1、反向代理
server {
   	listen 80;
   	server_name _;
   	location / {
        proxy_pass http://[连接池];
   	}
}

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

[root@lb01 conf.d]# cat game.conf 
upstream supermarie {
    server 172.16.1.7:80;
    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;
    }
}

1.3、负载均衡的比例

  • 轮询
# 默认情况下,Nginx负载均衡的轮询状态。
upstream supermarie {
    server 172.16.1.7:80;
    server 172.16.1.8:80;
    server 172.16.1.9:80;
}
  • 权重
# Nginx中的权重0-100,数字越大,权重越高。
upstream supermarie {
    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 supermarie {
    server 172.16.1.7:80;
    server 172.16.1.8:80;
    server 172.16.1.9:80;
    ip_hash;
}

1.4、负载均衡后端状态

状态 概述
down 当前的server暂时不参与负载均衡
backup 预留的备份服务器
max_fails 允许请求失败的次数
fail_timeout 经过max_fails失败后, 服务暂停时间
  • down
# 暂时不分配流量
upstream supermarie {
    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 supermarie {
    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、fail_timeout
# max_fails

# proxy_next_upstream 后端错误标识

[root@lb01 ~]# cat /etc/nginx/conf.d/game.conf 
upstream supermarie {
    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_500 http_502 http_503 http_404; 
        include /etc/nginx/proxy_params;
    }
}
注意:proxy_next_upstream error timeout invalid_header http_500 
http_503 http_404; 

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               # 禁用将请求传递给下一个服务器。

2、负载均衡部署BBS

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
  • 部署Django和uwsgi
1、安装Django和uwsgi
[root@web01 opt]# pip3 install django==1.11
[root@web01 opt]# pip3 install uwsgi
[root@web01 opt]# pip3 install pymysql

2、创建项目
[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': '123456',
        'HOST': '172.16.1.61',
        'PORT': 3306,
        'CHARSET': 'utf8'
    }
}

# 启动测试
[root@web01 bbs]# python3 manage.py runserver 0.0.0.0:8000
  • 配置并启动
1、编辑项目配置文件
[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

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

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

TCP 服务

3、编辑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;
    }
}

4、重启Nginx配置
systemctl restart nginx
  • 部署负载均衡
[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;
    }
}

image

posted on 2022-01-06 19:35  耿蜀黍  阅读(105)  评论(0编辑  收藏  举报