nginx-负载均衡相关配置 第五章
一、负载均衡:
通过反向代理客户端的请求到一个服务器群组,通过某种算法,将客户端的请求按照自定义的有规律的一种调度调度给后端服务器。
Nginx的负载均衡使用upstream定义服务器组,后面跟着组名,组名后面是大括号包起来的服务器列表,每个服务器使用server开头,后面跟定义的服务器名字、服务器IP:Port、参数;
1:upstream要写在Server块的外面,可以有多个,名称不同即可,如下:
upstream webserver { #配置后端服务器组
server 10.2.61.23:;
server 10.2.61.21:;
}
server {
listen ;
server_name localhost;
location / { #指定访问 / 负载到webserver 组中
proxy_pass http://webserver ;
proxy_set_header X_Forworded_For $remote_addr; #将客户端地址插入数据报头
}
location ~* ^/aab/ { #正则表达式匹配 /aab 路径 ,后端服务器必须要有这个路径
add_header webserver $upstream_addr; #将后端服务器地址插入到响应报头中,网上很多人说需要安装ngx_http_headers_module 模块。我测试默认支持这个模块,只有 add_header ,add_trailer ,expires.
proxy_pass http://webserver;
proxy_set_header X_Forworded_For $remote_addr;
}
}
访问结果:


1.2:nginx支持的三种负载方式:
round-robin:轮训调度,默认
ip_hash:会话绑定
least_conn:最少会话链接
1.3 backup 服务器
upstream webserver {
server 10.2.61.23: weight= max_fails= fail_timeout=;
server 10.2.61.21: backup;
}
server {
listen ;
server_name localhost;
location / {
proxy_pass http://webserver ;
proxy_set_header X_Forworded_For $remote_addr;
}
location ~* ^/aab/ {
add_header webserver $upstream_addr;
proxy_pass http://webserver;
proxy_set_header X_Forworded_For $remote_addr;
}
}
1.4 实现动静分离:

upstream web {
server 192.168.0.1 weight=1 max_fails=2 fail_timeout=2;
server 192.168.0.2 weight=1 max_fails=2 fail_timeout=2;
}
upstream image {
server 192.168.0.3 weight=1 max_fails=2 fail_timeout=2;
server 192.168.0.4 weight=1 max_fails=2 fail_timeout=2;
}
upstream php {
server 192.168.0.5 weight=1 max_fails=2 fail_timeout=2;
server 192.168.0.6 weight=1 max_fails=2 fail_timeout=2;
}
location /{
root html/web;
index index.php index.html;
}
location ~* \.php$ {
fastcgi_proxy http://php;
}
location ~* "\.(.jpg|png|jpeg|gif)" {
proxy_pass http://image;
}
1.5 实现数据缓存:
缓存是缓存nginx服务器接收请求过的数据,数据超时时间不能太长,因为数据可能会发生变化,但是nginx服务器内部的缓存的数据还没有更细,会导致客户端请求的数据不是最新数据的问题,数据缓存目录不能定义在server快内,要定义在http块中。
proxy_cache_path /usr/local/nginx/cache/first levels=: keys_zone=first:20m max_size=1g; #定义缓存路径与参数
upstream webserver {
server 10.2.61.23: weight= max_fails= fail_timeout=;
#server 10.2.61.21: backup;
#server 10.2.61.23: weight= max_fails= fail_timeout=; }
server {
listen ;
server_name localhost;
location / {
proxy_pass http://webserver;
proxy_set_header Host $host;
#add_header Host $upstream_addr ;
add_header X_Via $server_addr; #添加服务器地址到报文头部
add_header X-Cache $upstream_cache_status; #添加缓存状态到报文头部
proxy_cache first; #调用缓存
proxy_cache_valid 10m; #为状态码 为200 的缓存设置缓存时间为 10 分钟,不定义缓存不生效
proxy_set_header X_Forworded_For $remote_addr; }
location ~* ^/aab/ {
add_header webserver $upstream_addr;
proxy_pass http://webserver;
proxy_set_header X_Forworded_For $remote_addr;
}
}
#/usr/local/nginx/cache/first定义缓存目录参数
#evels=1:2 定义两层目录,第一层一个字符名称,第二个两个字符名称
#keys_zone=first:20m 每个缓存都是一个共享内存空间。这就是用户定义共享内存空间地址的名称
#max_size=1g 定义目录最大空间为1g,因为缓存空间越大搜索数据越慢,因此不宜太大。

1.6 另外常用的三种缓存:
open_log_cache:日志缓存,降低磁盘IO
open_file_cache:打开文件句柄缓存,将文件缓存至 Nginx管理的内存当中加速响应
fastcgi_cache:缓存后端php服务器的内容,当时如果php内容发生更改则会导致客户端访问的页面不是最新的,因此要慎用。
另外Nginx的limit限制也是基于内存共享来实现的
1.7 sticky 会话保持,基于自定义cookie 进行会话保持的方式
安装包下载地址:https://github.com/bymaximus/nginx-sticky-module-ng
./configure --prefix=/usr/local/nginx --sbin-path=/usr/local/nginx/sbin/nginx --conf-path=/usr/local/nginx/conf/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx/nginx.pid --user=nginx --group=nginx --with-http_ssl_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre=/usr/local/src/pcre-8.42 --with-zlib=/usr/local/src/zlib-1.2. --with-stream --add-module=/usr/local/src/nginx-1.14./echo-nginx-module-master --add-module=/usr/local/src/headers-more-nginx-module-master --add-module=/usr/local/src/nginx-sticky-module-ng-master
upstream webserver {
server 10.2.61.23: weight= max_fails= fail_timeout=;
#server 10.2.61.21: backup;
server 10.2.61.23: weight= max_fails= fail_timeout=;
sticky expires=1h path=/;
}

upstream webserver {
server 10.2.61.23: weight= max_fails= fail_timeout=;
#server 10.2.61.21: backup;
server 10.2.61.23: weight= max_fails= fail_timeout=;
sticky name=srv_id expires=1h path=/;
}

nginx-负载均衡相关配置 第五章的更多相关文章
- nginx负载均衡简单配置
nginx负载均衡简单配置准备三台虚拟机来做这个实验:192.168.232.132 web服务器192.168.232.133 web服务器192.168.232.134 ...
- nginx负载均衡及配置
nginx负载均衡及配置 1 负载均衡概述 负载均衡由来是因为当一台服务器单位时间内的访问量很大时,此时服务器的压力也会很大,当超过自身承受能力时,服务器就会崩溃.为避免让服务器崩溃,用户拥有更好的体 ...
- nginx 负载均衡相关知识
Nginx ("engine x") 是一个高性能的 HTTP 和 反向代理 服务器,也是一个 IMAP/POP3/SMTP 代理服务器. Nginx 是由 Igor Sysoev ...
- nginx 负载均衡简单配置
配置要求: 三台服务器 127.0.0.1 主负载(把访问请求分给主机池) 127.0.0.2 主机2 127.0.0.3 主机3 第一步: 配置127.0.0.1 ...
- nginx负载均衡常见问题配置信息
nginx为后端web服务器(apache,nginx,tomcat,weblogic)等做反向代理 几台后端web服务器需要考虑文件共享,数据库共享,session共享问题.文件共享可以使用nfs, ...
- Nginx负载均衡各种配置方式
Nginx负载均衡 - 小刚qq - 博客园http://www.cnblogs.com/xiaogangqq123/archive/2011/03/04/1971002.html Module ng ...
- Nginx负载均衡NFS配置
Nginx配置 首先在两台服务器上部署同一个项目,例如下: 测试网站节点1: http://192.168.168.61/nfstest/ 测试网站节点2: http://192.168.64.145 ...
- window下nginx负载均衡简单配置-----权重的实现
下面介绍一个在window下的nginx的负载均衡配置. 需要你在你的电脑上跑两个tomcat.一个8080,一个9080. 需要一个nginx服务器. 需要修改本机的host 注意:我们这里配置不会 ...
- php nginx 负载均衡简单配置过程
负载均衡 负载均衡是我们大流量网站要做的一个东西,下面我来给大家介绍在Nginx服务器上进行负载均衡配置方法,希望对有需要的同学有所帮助哦. 简单了解一下什么是负载均衡,单从字面上的意思来理解就可以解 ...
随机推荐
- 【问题】Can't load AMD 64-bit .dll on a IA 32-bit platform
文件下载地址:http://archive.apache.org/dist/tomcat/tomcat-connectors/native/1.2.14/binaries/ 按自己的提示找到32位或者 ...
- 这样使用 GPU 渲染 CSS 动画(转)
大多数人知道现代网络浏览器使用GPU来渲染部分网页,特别是具有动画的部分. 例如,使用transform属性的CSS动画看起来比使用left和top属性的动画更平滑. 但是如果你问,“我如何从GPU获 ...
- C# windows GDI+仿画图 绘图程序设计
C# windows GDI+仿画图 绘图程序设计 1.介绍 这里分享一个简单的画图程序 原作者:author: ping3108@163.com 2.程序主窗体设计 3.程序设计 本程序工程使用VS ...
- [原]CentOS7安装Rancher2.1并部署kubernetes (一)---部署Rancher
################## Rancher v2.1.7 + Kubernetes 1.13.4 ################ ##################### ...
- Python Solve UnicodeEncodeError 'gbk' / 'ascii' / 'utf8' codec can't encode character '\x??' in position ? 解决有关Python编码的错误
在Python中,处理中文字符一直是很令人头痛的问题,一言不合就乱码,而且引起乱码的原因也不尽相同,有时候是python本身默认的编码器设置的不对,有时候是使用的IDE的解码器不对,还有的时候是终端t ...
- HDU - 2043密码 水题
密码 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submissi ...
- Ubuntu下安装LNMP
1.安装mysql sudo apt-get install mysql-server mysql-client 2.安装nginx sudo apt-get install nginx 安装完后重启 ...
- Spring-Boot数据库密码加密配置
springboot集成mysql/oracle时需要在yml/properties中配置数据库信息,用户名密码是肯定有的,所以就涉及到密码的加密,当然不加密也是可以的,正如某位大佬所说的,不加密就像 ...
- 网络爬虫基础知识(Python实现)
浏览器的请求 url=请求协议(http/https)+网站域名+资源路径+参数 http:超文本传输协议(以明文的形式进行传输),传输效率高,但不安全. https:由http+ssl(安全套接子层 ...
- leetcode-Evaluate the value of an arithmetic expression in Reverse Polish Notation
leetcode 逆波兰式求解 Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid ope ...