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

Nginx要实现负载均衡需要用到proxy_pass代理模块配置
Nginx负载均衡与Nginx代理不同地方在于
Nginx代理仅代理一台服务器,而Nginx负载均衡则是将客户端请求代理转发至一组upstream虚拟服务池
Nginx可以配置代理多台服务器,当一台服务器宕机之后,仍能保持系统可用。
upstream配置
在nginx.conf > http 区域中
upstream django {
server 10.0.0.10:;
server 10.0.0.11:;
}
在nginx.conf > http 区域 > server区域 > location配置中
添加proxy_pass
location / {
root html;
index index.html index.htm;
proxy_pass http://django;
}
此时初步负载均衡已经完成,upstream默认按照轮训方式负载,每个请求按时间顺序逐一分配到后端节点。
upstream分配策略
weight 权重
upstream django {
server 10.0.0.10: weight=;
server 10.0.0.11: weight=;#这个节点访问比率是大于8000的
}
ip_hash
每个请求按访问ip的hash结果分配,这样每个访客固定访问一个后端服务器
upstream django {
ip_hash;
server 10.0.0.10:8000;
server 10.0.0.11:9000;
}
backup
在非backup机器繁忙或者宕机时,请求backup机器,因此机器默认压力最小
upstream django {
server 10.0.0.10: weight=;
server 10.0.0.11:;
server node.oldboy.com: backup;
}
负载均衡实验环境规划
角色 ip 主机名
lb01 192.168.119.10 lb01
web01 192.168.119.11 web01
web02 192.168.119.12 web02
关闭防火墙
iptables -F
sed -i 's/enforcing/disabled/' /etc/selinux/config systemctl stop firewalld
systemctl disable firewalld
一、web01服务器配置nginx,创建index.html
server {
listen ;
server_name 192.168.119.11;
location / {
root /node;
index index.html index.htm;
}
}
mkdir /node
echo 'i am web01' > /node/index.html
#启动NGINX
./sbgin/nginx
二、web01服务器配置nginx,创建index.html
server {
listen ;
server_name 192.168.119.12;
location / {
root /node;
index index.html index.htm;
}
mkdir /node
echo 'i am web02...' > /node/index.html
#启动nginx
./sbing/nginx
三、配置lb01服务器的nginx负载均衡
1.检查lb01的 nginx.conf
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout ;
upstream node {
server 192.168.119.11:80;
server 192.168.119.12:80;
}
server {
listen ;
server_name 192.168.119.10;
location / {
proxy_pass http://node;
include proxy_params; #需要手动创建
}
}
}
2.手动创建proxy_params文件,文件中存放代理的请求头相关参数
[root@lb01 conf]# cat /opt/nginx/conf/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 ;
proxy_send_timeout ;
proxy_read_timeout ; proxy_buffering on;
proxy_buffer_size 32k;
proxy_buffers 128k;
启动lb01负载均衡nginx服务 ./sbin/nginx
四、访问lb01节点nginx,反复刷新


五、nginx负载均衡调度算法
调度算法 概述
轮询 按时间顺序逐一分配到不同的后端服务器(默认)
weight 加权轮询,weight值越大,分配到的访问几率越高
ip_hash 每个请求按访问IP的hash结果分配,这样来自同一IP的固定访问一个后端服务器
url_hash 按照访问URL的hash结果来分配请求,是每个URL定向到同一个后端服务器
least_conn 最少链接数,那个机器链接数少就分发
1.轮询(不做配置,默认轮询)
2.weight权重(优先级)
3.ip_hash配置,根据客户端ip哈希分配,不能和weight一起用
六、nginx动静分离负载均衡

环境准备
系统 服务 软件 ip地址
centos7(lb01) 负载均衡 nginx proxy 192.168.119.10
centos7(web01) 静态资源 nginx静态资源 192.168.119.11
centos7(web02) 动态资源 django 192.168.119.12
一、在web01机器上,配置静态资源,图片等
cat nginx.conf
server {
listen ;
server_name 192.168.119.11;
#定义网页根目录
root /code;
#定义了静态资源
index index.html;
#域名匹配,所有的png、jpg、gif请求资源,都去/root/code/images底下找
location ~* .*\.(png|jpg|gif)$ {
root /code/images;
}
#重启nginx
./sbin/nginx
#创建目录
mkdir -p /code/images
#准备首页文件
[root@web01 /code]$cat index.html
static files...
#准备静态文件,图片
[root@web01 /code/images]$wget http://pythonav.cn/av/girlone.jpg^C
[root@web01 /code/images]$ls
girlone.jpg
二、在web02配置动态请求,准备一个flask程序和静态资源转发
cat nginx.conf #静态资源地址
upstream static {
server 192.168.119.11:;
}
#flask动态请求
upstream flask {
server 192.168.119.12:;
}
server {
listen ;
server_name 192.168.119.12;
#当请求到达192.168.119.12:80/时,转发给flask的8080应用
location / {
proxy_pass http://flask;
include proxy_params;
}
#当判断资源请求是 192.168.119.12/girl.jpg时候,转发请求给static地址池的服务器192.168.119.11/
location ~ .*\.(png|jpg|gif)$ {
proxy_pass http://static;
include proxy_params;
}
准备flask应用,flask.py
from flask import Flask
app=Flask(__name__)
@app.route('/')
def hello():
return "i am flask....from nginx"
if __name__=="__main__":
app.run(host='0.0.0.0',port=)
后台运行flask程序
python flask-web.py &
三、在负载均衡服务器lb01上测试访问192.168.119.10



11.2,nginx负载均衡实验的更多相关文章
- Linux 集群概念 , wsgi , Nginx负载均衡实验 , 部署CRM(Django+uwsgi+nginx), 部署学城项目(vue+uwsgi+nginx)
Linux 集群概念 , wsgi , Nginx负载均衡实验 , 部署CRM(Django+uwsgi+nginx), 部署学城项目(vue+uwsgi+nginx) 一丶集群和Nginx反向代理 ...
- nginx负载均衡实验
Nginx负载均衡概述 Web服务器,直接面向用户,往往要承载大量并发请求,单台服务器难以负荷,我使用多台WEB服务器组成集群,前端使用Nginx负载均衡,将请求分散的打到我们的后端服务器集群中,实现 ...
- Linux(7)- Nginx.conf主配置文件、Nginx虚拟主机/访问日志/限制访问IP/错误页面优化、Nginx反向代理、Nginx负载均衡
一.Nginx.conf主配置文件 Nginx主配置文件conf/nginx.conf是一个纯文本类型的文件,整个配置文件是以区块的形式组织的.一般,每个区块以一对大括号{}来表示开始与结束. 核心模 ...
- nginx负载均衡简单配置
nginx负载均衡简单配置准备三台虚拟机来做这个实验:192.168.232.132 web服务器192.168.232.133 web服务器192.168.232.134 ...
- Linux之nginx负载均衡
Nginx负载均衡概述 Web服务器,直接面向用户,往往要承载大量并发请求,单台服务器难以负荷,我使用多台WEB服务器组成集群,前端使用Nginx负载均衡,将请求分散的打到我们的后端服务器集群中,实现 ...
- nginx负载均衡 理解与测试
Nginx负载均衡概述 Web服务器,直接面向用户,往往要承载大量并发请求,单台服务器难以负荷,我使用多台WEB服务器组成集群,前端使用Nginx负载均衡,将请求分散的打到我们的后端服务器集群中,实现 ...
- 对比Haproxy和Nginx负载均衡效果
为了对比Hproxy和Nginx负载均衡的效果,分别在测试机上(以下实验都是在单机上测试的,即负载机器和后端机器都在一台机器上)做了这两个负载均衡环境,并各自抓包分析.下面说下这两种负载均衡环境下抓包 ...
- nginx负载均衡集群
nginx负载均衡集群 0.前言:nginx 负载均衡,属于网络7层模型中的应用层,说白了就是一个代理,要用 upstrem 模块实现,代理则用proxy模块 1.可以针对域名做转发,lvs只能针对 ...
- 烂泥:nginx负载均衡
本文由秀依林枫提供友情赞助,首发于烂泥行天下. 今天我们来学习下有关nginx的负载均衡配置.nginx的负载均衡是通过nginx的upstream模块和proxy_pass反向代理来实现的. 说明: ...
随机推荐
- Azure资源模板化部署,伦家不懒都不好意思了
如果老板让你在云平台上部署一套系统,你准备怎么做? 嗯,估计得根据具体需求开通或创建一大堆东西:虚拟机.存储.数据库.虚拟网络……别急还没完,接着还要对这些东西的规模.配置等各方面调整和优化.一系列环 ...
- Centos 7.0_64bit 下安装 Zabbix server 3.0服务器的安装
一.关闭selinux 修改配置文件/ etc / selinux / config,将SELINU置为禁用(disabled) vim /etc/selinux/config # This ...
- 《最牛B的Linux Shell命令》笔记
1.以sudo 运行上一条命令 sudo !! 大家应该都知sudo,不解释.但通常出现的情况是,敲完命令执行后报错才发现忘了sudo.如下: ➜ ~ cp ~/download/CentOS7-Ba ...
- JavaScript如何转换数据库DateTime字段类型?
Javascript一种直译式脚本语言,是一种动态类型.弱类型.基于原型的语言,内置支持类型.它的解释器被称为JavaScript引擎,为浏览器的一部分,广泛用于客户端的脚本语言,最早是在html(标 ...
- mmap内存映射
http://blog.csdn.net/kongdefei5000/article/details/70183119 内存映射是个很有用,也很有意思的思想.我们都知道操作系统分为用户态和内核态,用户 ...
- 【洛谷2605】[ZJOI2010] 基站选址(线段树维护DP)
点此看题面 大致题意: 有\(n\)个村庄,每个村庄有\(4\)个属性:\(D_i\)表示与村庄\(1\)的距离,\(C_i\)表示建立基站的费用,\(S_i\)表示能将其覆盖的建基站范围,\(W_i ...
- Rich feature hierarchies for accurate object detection and semantic segmentation(RCNN)
https://zhuanlan.zhihu.com/p/23006190?refer=xiaoleimlnote http://blog.csdn.net/bea_tree/article/deta ...
- linux flushing file system caches
We may drop the file system caches on Linux to free up memory for applications. Kernels 2.6.16 and n ...
- B4260 Codechef REBXOR
真是6块钱cpu(笑 爆炸入口 踹树练习(汗 对于二进制异或和弹性,我们可以贪心的来做. 瓶颈在于快速贪心. 我们可以维护一个trie树,储存异或前缀和.每次在trie树上贪心的跑. 正向and反向跑 ...
- java读取pfx或P12格式的个人交换库公私钥
使用的是CFCA签发的用于银行间交换数据的证书,下载后直接添加到浏览器中 1.导出 从浏览器导出p12文件(包含私钥) 2.验证 两种方式: openssl 代码(请注意alias别名是如何获取的): ...