linux-nginx负载均衡

nginx代理
一、代理的方式
1、正向代理
找完代理之后,还需要找服务器 应用:VPN
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
三、代理实践
🌹 第一部分:部署web03
[root@web03 /etc/nginx/conf.d]# cd /etc/nginx/conf.d/
[root@web03 /etc/nginx/conf.d]# vim game3.conf
server {
listen 80;
server_name 192.168.15.9;
location / {
root /opt/superM;
index index.html;
}
}
🌹 第二部分:部署lb01
🌼 部署nginx
1、下载nginx源代码包
[root@lb01 ~]# wget https://nginx.org/download/nginx-1.20.2.tar.gz
2、解压
[root@lb01 ~]# tar -xf nginx-1.20.2.tar.gz
3、进入源代码目录
[root@lb01 ~]# cd nginx-1.20.2
4、安装依赖包
[root@lb01 ~/nginx-1.20.2]# yum install openssl openssl-devel zlib zlib-devel
5、设置编译参数
[root@lb01 ~/nginx-1.20.2]# ./configure --with-http_gzip_static_module --with-stream --with-http_ssl_module
6、编译,安装
[root@lb01 ~/nginx-1.20.2]# make && make install
7、优化
[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
⚠️:根据nginx.service里面我们需要进入nginx.conf里面
改如图:

<不然真是真是开不起来,开不起来,开不起来,烦死了!!!!!!>
<如果找不到文件的话,可以建立软连接,建立软连接!啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊这东西好烦啊!!!!!!!!!!!>
🌼部署反向代理
[root@lb01 conf.d]# cd /etc/nginx/nginx.conf
[root@lb01 conf.d]# vim game.conf
server{
listen 80;
server_name _;
location / {
proxy_pass http://172.16.1.9:80;
}
}
四、nginx代理常用参数
1、添加发往后端服务器的请求头信息
Syntax: proxy_set_header field value;
Default: proxy_set_header Host $http_host;
proxy_set_header Connection close;
Context: http, server, location
1.1 用户请求的时候host的值是linux.proxy.com,那么代理服务会像后端传递请求的还是linux.proxy.com
proxy_set_header Host $http_host;
1.2 将$remote_addr的值放进X-Real-IP中,$remote_addr的值为客户端的ip
proxy_set_header X-Real-IP $remote_addr;
1.3 客户端通过代理服务访问后段服务,后段服务通过该变量会记录客户真是的地址
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
2、代理到后端的TCP链接,响应,返回等超时时间
2.1#nginx代理与后端服务器连接超时时间(代理连接超时)
Syntax: proxy_connect_timeout time;
Default: proxy_connect_timeout 60s;
Context: http, server, location
2.2#nginx代理等待后端服务器的响应时间
Syntax: proxy_read_timeout time;
Default: proxy_read_timeout 60s;
Context: http, server, location
2.3#后端服务器数据回传给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;
3、proxy_buffer代理缓冲区
3.1#nignx会把后端返回的内容先放到缓冲区当中,然后再返回给客户端,边收边传, 不是全部接收完再传给客户端
Syntax: proxy_buffering on | off;
Default: proxy_buffering on;
Context: http, server, location
3.2#设置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
4、配置优化文件
[root@lb01 conf.d]# 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]# vi /etc/nginx/conf.d/game.conf
server {
listen 80;
server_name _;
location / {
proxy_pass http://172.16.1.7:80;
include /etc/nginx/proxy_params;
}
}
nginx 负载均衡
一、负载均衡架构
通过代理将流量按照一定的比例,转发到后端
二、负载均衡的实现-->将后端服务打包成一个ip链接池
1、ip链接池
upstream 链接池名字 {
server ip:port;
server ip:port;
server ip:port;
}
2、反向代理
server {
listen 80;
server_name _;
location / {
proxy_pass http://[连接池];
}
}
3、编辑
[root@lb01 conf.d]# vim game.conf

三、负载均衡的比例
1、轮询
默认情况下,nginx负载均衡的比例是轮询状态
upstream judyjudy {
server 172.16.1.9:80;
server 172.16.1.8:80;
}
2、权重
nginx中的权重0-100,数字越大,权重越高
upstream judyjudy {
server 172.16.1.9:80 weight=10;
server 172.16.1.8:80 weight=8;
}
3、ip_hash
每一个ip固定访问某一个后端
upstream judyjudy {
server 172.16.1.9:80;
server 172.16.1.8:80;
ip_hash
}
四、负载均衡后端状态
| 状态 | 概述 |
|---|---|
| down | 当前的server暂时不参与负载均衡 |
| backup | 预备的备份服务器 |
| max_fails | 允许请求失败的次数 |
| fail_timeout | 经过max_fails失败后服务暂停时间 |
1、down
upstream judyjudy {
server 172.16.1.9:80 down;
server 172.16.1.8:80;
}
2、backup 只有当所有的机器全部宕机,才会启动
upstream judyjudy {
server 172.16.1.9:80 backup;
server 172.16.1.8:80;
}
3、max_fails、fail_timeout
upstream judyjudy {
server 172.16.1.9:80 max_fails=3 fail_timeout=3s;
server 172.16.1.8:80 max_fails=3 fail_timeout=3s;
}
server{
listen 80;
server_name _;
location / {
proxy_pass http://judyjudy;
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 # 禁用将请求传递给下一个服务器。
五、负载均衡状态部署bbs
1、部署python
1.1 创建用户
[root@web02 /opt]# groupadd django -g 888
[root@web02 /opt]# useradd django -u 888 -g 888 -r -M -s /bin/sh
1.2 安装依赖软件
[root@web02 /opt]# yum install python3 libxml* python-devel gcc* pcre-devel openssl-devel python3-devel -y
2、部署Django和uwsgi
2.1 安装Django和uswgi
[root@web02 /opt]# pip3 install django==1.11
[root@web02 /opt]# pip3 install uwsgi
[root@web02 /opt]# pip3 install pymysql
2.2 创建项目
[root@web02 /opt]# scp 172.16.1.9:/opt/bbs.zip /opt
[root@web02 /opt]# cd bbs
[root@web02 /opt/bbs]# vim bbs/settings.py
ALLOWED_HOSTS = ['*']
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'bbs',
'USER': 'root',
'PASSWORD': '123456',
'HOST': '172.16.1.51',
'PORT': 3306,
'CHARSET': 'utf8'
}
}
3、配置并且启动
3.1 编辑项目配置文件
[root@web02 /etc/nginx/conf.d]# 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
3.2创建数据库迁移文件
[root@web03 /opt/bbs]# python3 manage.py makemigrations
3.3数据库迁移
[root@web03 /opt/bbs]# python3 manage.py migrate
3.4配置uwsgi
[root@web03 /opt/bbs]# vim /opt/bbs/myweb_uwsgi.ini
[uwsgi]
# 端口号
socket = :8002
# 指定项目的目录
chdir = /opt/bbs
# wsgi文件路径
wsgi-file = bbs/wsgi.py
# 模块wsgi路径
module = bbs.wsgi
# 是否开启master进程
master = true
# 工作进程的最大数目
processes = 4
# 结束后是否清理文件
vacuum = true
3.5启动uwsgi
[root@web0 /opt/bbs]# uwsgi -d --ini myweb_uwsgi.ini --uid 666
3.6配置nginx
[root@web02 /opt/bbs]# cd /etc/nginx/conf.d/
[root@web02 /etc/nginx/conf.d]# vim bbs.conf
server {
listen 80;
server_name bbs.test.com;
location / {
include uwsgi_params;
uwsgi_pass 127.0.0.1:8002;
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;
}
}
3.7重启nginx
[root@web02 /etc/nginx/conf.d]# systemctl restart nginx
3.8在终端修改hosts文件
4、部署负载均衡
4.1 [root@lb01 conf.d]# vim bbs.conf
upstream bbs {
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 bbs.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;
}
}
5、测试

浙公网安备 33010602011771号