ngx_http_upstream_module模块:Nginx负载均衡模块

Syntax:      upstreamname { ... }

Default:     —

Context:    http

Defines a group of servers. Servers can listen ondifferent ports. In addition, servers listening on TCP and UNIX-domain socketscan be mixed.

简单示例:

upstream backend {

server backend1.example.com weight=5;

server 127.0.0.1:8080       max_fails=3 fail_timeout=30s;

server unix:/tmp/backend3;

server backup1.example.com  backup;

}

配置示例:

upstream backend {

server backend1.example.com       weight=5;

server backend2.example.com:8080;

server unix:/tmp/backend3;

server backup1.example.com:8080   backup;

server backup2.example.com:8080   backup;

}

server {

location / {

proxy_pass http://backend;

}

}

前置条件,编辑后端web 服务器测试页面

nginx作为后端web服务器:192.168.88.130:8080  test.field.com

[root@testhtml]# pwd

/usr/share/nginx/html

[root@testhtml]# cat  index.html

<h1>Welcometo Nginx on test.field.com!!</h1>

<p><em>If you see this page,test OK! </em></p>

<p>Thank you!</p>

[root@test html]#

Http作为后端web服务器:192.168.88.129:80  web2.field.com

[root@web2html]# pwd

/var/www/html

[root@web2html]# cat index.html

<h1>Welcometo Http on web2.field.com!!</h1>

<p>If yousee this page,test ok. </p>

<p><em>Thank you!</em></p>

案例1、负载均衡基础应用

编辑nginx.conf,添加如下内容:

   upstreamupservers {

        server  192.168.88.129;

        server  192.168.88.130:8080;

    }

编辑conf.d/default.conf,添加如下内容:

    location/field/ {

     proxy_pass  http://upservers/;

    }

    location ~*\.(jpg|png|gif)$ {

     proxy_pass  http://upservers;

}

[root@wwwnginx]# vi nginx.conf

user  nginx;

worker_processes  1;

error_log   /var/log/nginx/error.log warn;

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" $http_host '

'$status $body_bytes_sent"$http_referer" '

'"$http_user_agent""$http_x_forwarded_for"';

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

proxy_cache_path  /cache/nginx/ levels=1:1keys_zone=mycache:32m;

upstream  upservers {

server  192.168.88.129;

server  192.168.88.130:8080;

}

sendfile        on;

#tcp_nopush     on;

keepalive_timeout  65;

#gzip on;

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

}

[root@wwwconf.d]# vi default.conf

server {

listen      8080;

server_name localhost;

#charset koi8-r;

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

location / {

root  /usr/share/nginx/html;

# proxy_pass http://192.168.88.130/;

index index.html index.php index.htm;

}

location /field/ {

proxy_pass  http://upservers/;

proxy_set_header Host      $host;

proxy_set_header X-Real-IP $remote_addr;

}

location ~* \.(jpg|png|gif)$ {

proxy_pass   http://upservers;

proxy_set_header X-Real-IP $remote_addr;

}

error_page  500 502 503 504  /50x.html;

location = /50x.html {

root   /usr/share/nginx/html;

}

}

[root@wwwconf.d]# service nginx restart

停止 nginx:[确定]

正在启动 nginx:[确定]

[root@wwwconf.d]#

wind7访问 http://192.168.88.131:8080/field/

刷新页面,页面内容会在【Welcome to Http on web2.field.com!!!】和【Welcome to Nginx on test.field.com!!】上跳转

案例2、负载均衡定义加权轮询

定义加权轮询,默认为轮询

    upstream  upservers {

        server  192.168.88.129  weight=n;

        server  192.168.88.130  weight=m;

    }

[root@wwwnginx]# vim nginx.conf

upstream  upservers {

server  192.168.88.129  weight=2;

server  192.168.88.130:8080  weight=1;

}

[root@wwwnginx]# service nginx reload

重新载入 nginx:[确定]

Win7访问http://www.field.com/field/

依次刷新页面,页面内容会在【Welcome to Http on web2.field.com!!!】和【Welcome to Nginx on test.field.com!!】上以2:1跳转

案例3、负载均衡定义根据客户端IP进行调度

ip_hash:根据客户端IP进行调度:

每一个客户端访问时都生成一个hash码,来自同一客户端的定向到同一服务器

    upstream  upservers {

        ip_hash;

    }

[root@wwwnginx]# vim nginx.conf

upstream  upservers {

ip_hash;

server  192.168.88.129  weight=2;

server  192.168.88.130:8080  weight=1;

}

[root@wwwnginx]# service nginx configtest

nginx: theconfiguration file /etc/nginx/nginx.conf syntax is ok

nginx:configuration file /etc/nginx/nginx.conf test is successful

[root@wwwnginx]# service nginx reload

重新载入 nginx:[确定]

win7访问http://192.168.88.131:8080/field/

始终定位到【Welcome to Http on web2.field.com!!!】页面

130主机用curl访问http://192.168.88.131:8080/field/

始终定位到【Welcome to Nginx on test.field.com!!】页面

[root@testconf.d]# curl  http://192.168.88.131:8080/field/

<h1>Welcometo Nginx on test.field.com!!</h1>

<p><em>If you see this page,test OK! </em></p>

<p>Thankyou!</p>

[root@testconf.d]#

案例4、后端服务器健康状态检测:

max_fails=number 检查number次失败时定义为真实失败;

fail_timeout=time 每次检查失败的超时时间;

[root@wwwnginx]# vim  nginx.conf

upstream  upservers {

server  192.168.88.129  weight=2  max_fails=2  fail_timeout=1;

server  192.168.88.130:8080  weight=1  max_fails=2  fail_timeout=1;

}

[root@wwwnginx]# service  nginx  reload

重新载入 nginx:[确定]

wind7访问 http://www.field.com:8080/field/

刷新页面,页面内容会在【Welcome to Http on web2.field.com!!!】和【Welcome to Nginx on test.field.com!!】上以2:1跳转

测试:

1).关闭后端httpd服务

[root@web2html]# service  httpd  stop

停止 httpd:[确定]

[root@web2html]#

wind7访问 http://www.field.com:8080/field/

页面只显示【Welcome to Nginx on test.field.com!!】

2).关闭后端Nginx服务

[root@testconf.d]# service  nginx  stop

停止 nginx:[确定]

[root@testconf.d]#

wind7访问 http://www.field.com:8080/field/,

无法访问,页面显示【An error occurred.】

3).依次开启后端httpd、Nginx服务,访问恢复。

案例5、手动标记状态

backup:备用

down:下线

[root@wwwnginx]# vim nginx.conf

upstream upservers {

server  192.168.88.129  max_fails=2  fail_timeout=1;

server  192.168.88.130:8080  max_fails=2  fail_timeout=1 backup;

}

[root@wwwnginx]# service  nginx configtest

nginx: theconfiguration file /etc/nginx/nginx.conf syntax is ok

nginx:configuration file /etc/nginx/nginx.conf test is successful

[root@wwwnginx]# service nginx reload

重新载入 nginx:[确定]

测试:

1).服务器正常,只上线129主机的http服务

win7访问http://192.168.88.131:8080/field/,刷新页面,始终定位到【Welcome to Http onweb2.field.com!!!】页面

130主机用curl访问http://192.168.88.131:8080/field/,始终定位到【Welcome to Http on web2.field.com!!!】页面

[root@testconf.d]# curl  http://192.168.88.131:8080/field/

<h1>Welcometo Http on web2.field.com!!</h1>

<p>If yousee this page,test ok. </p>

<p><em>Thankyou!</em></p>

[root@testconf.d]#

2).关闭后端httpd服务,备用的130主机会上线。

[root@web2html]# service  httpd  stop

停止 httpd:[确定]

win7访问http://192.168.88.131:8080/field/,刷新页面,始终定位到【Welcome to Nginx ontest.field.com!!】页面

129主机用curl访问http://192.168.88.131:8080/field/,始终定位到【Welcome to Nginx on test.field.com!!】页面

[root@web2html]# curl  http://192.168.88.131:8080/field/

<h1>Welcometo Nginx on test.field.com!!</h1>

<p><em>If you see this page,test OK! </em></p>

<p>Thankyou!</p>

3).开启129主主机后端httpd服务,备用的130主机会下线,129主机上线。

[root@web2html]# service httpd start

正在启动 httpd:[确定]

win7访问http://192.168.88.131:8080/field/,刷新页面,始终定位到【Welcome to Http on web2.field.com!!!】页面

案例6、自定义相应报文首部: 

编辑default.conf,添加如下内容:

add_header X-Via $server_addr;

add_header X-Cache $upstream_cache_status;

[root@wwwconf.d]# vi  default.conf

server {

listen      8080;

server_name localhost;

add_headerX-Via $server_addr;

add_headerX-Cache $upstream_cache_status;

#charset koi8-r;

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

location / {

root  /usr/share/nginx/html;

# proxy_pass  http://192.168.88.130/;

index  index.html  index.php  index.htm;

}

location  /field/ {

proxy_pass  http://upservers/;

proxy_set_header  Host      $host;

proxy_set_header  X-Real-IP  $remote_addr;

proxy_set_header        X-Forwarded-For  $proxy_add_x_forwarded_for;

}

location  ~* \.(jpg|png|gif)$  {

proxy_pass  http://upservers;

proxy_set_header  X-Real-IP  $remote_addr;

}

}

[root@wwwconf.d]# service nginx reload

重新载入 nginx:[确定]

[root@wwwconf.d]#

win7访问http://192.168.88.131:8080/field/,F12打开控制台

可以看到报文首部:

X-Via:192.168.88.131

案例7、负载均衡加入缓存机制

 Syntax:    proxy_cache_path path [levels=levels][use_temp_path=on|off] keys_zone=name:size [inactive=time] [max_size=size][manager_files=number] [manager_sleep=time] [manager_threshold=time][loader_files=number] [loader_sleep=time] [loader_threshold=time][purger=on|off] [purger_files=number] [purger_sleep=time][purger_threshold=time];

Default:     —

Context:    http

缓存文件路径定义levels=1:2,一级子目录一个字符表示,2级子目录两个字符表示

总共有62*62*62个文件

   proxy_cache_path /data/nginx/cache levels=1:2 keys_zone=one:10m;

file names in a cache will look like this:

   /data/nginx/cache/c/29/b7f54b2df7773722d382f4809d65029c  

$upstream_cache_status 包含以下几种状态:

MISS:未命中,请求被传送到后端

HIT:缓存命中

EXPIRED:缓存已经过期请求被传送到后端

UPDATING:正在更新缓存,将使用旧的应答

STALE:后端将得到过期的应答

简单配置如下: 

配置缓存:nginx.conf

proxy_cache_path   /cache/nginx/ levels=1:1keys_zone=mycache:32m;

加入头信息:default.conf

add_header  X-Via  $server_addr;

add_header  X-Cache  $upstream_cache_status;

启用缓存:

proxy_cache  mycache;

proxy_cache_valid  200  1d;

#200 1d 表示这个zone中返回200的缓存文件如果在1天内没有被访问,那么文件会被cache manager进程删除掉

proxy_cache_valid  301  302  10m;

proxy_cache_valid  any  1m;

proxy_cache_use_stale  error  http_500  http_502  http_503  http_504

[root@www nginx]#vi  nginx.conf

proxy_cache_path  /cache/nginx/levels=1:2 keys_zone=mycache:32m;

upstream upservers {

server  192.168.88.129  max_fails=2  fail_timeout=1;

server  192.168.88.130:8080  max_fails=2  fail_timeout=1  backup;

}

[root@wwwconf.d]# vi default.conf

server {

listen      8080;

server_name  localhost;

add_headerX-Via  $server_addr;

add_headerX-Cache  $upstream_cache_status;

#charset koi8-r;

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

location / {

root   /usr/share/nginx/html;

# proxy_pass http://192.168.88.130/;

index  index.html  index.php  index.htm;

}

location /field/ {

proxy_cache  mycache;

proxy_cache_valid  200  1d;

proxy_cache_valid  301  302  10m;

proxy_cache_valid  any  1m;

proxy_cache_use_stale  error  http_500 http_502  http_503  http_504;

proxy_pass  http://upservers/;

proxy_set_header  Host      $host;

proxy_set_header  X-Real-IP  $remote_addr;

proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;

}

location  ~* \.(jpg|png|gif)$ {

proxy_cache  mycache;

proxy_cache_valid  200  1d;

proxy_cache_valid  301  302  10m;

proxy_cache_valid  any  1m;

proxy_cache_use_stale  error  http_500  http_502  http_503  http_504;

proxy_pass  http://upservers;

proxy_set_header  X-Real-IP  $remote_addr;

}

error_page   500  502  503  504  /50x.html;

location = /50x.html {

root   /usr/share/nginx/html;

}

}

访问http://www.field.com:8080/field/,F12打开控制台

可以看到

X-Cache:MISS

X-Via:192.168.88.131

#MISS:缓存未命中,请求被传送到后端

此时已经在Nginx代理服务器形成缓存文件

[root@www ba]#pwd

/cache/nginx/5/ba

[root@www ba]#ll

总用量 4

-rw-------. 1nginx nginx 546 4月  20 16:43141b735b7bb97cf9c8914596bd253ba5

[root@www ba]#cat 141b735b7bb97cf9c8914596bd253ba5

KEY:http://upservers/

HTTP/1.1 200 OK

Date: Fri, 20Apr 2018 08:43:20 GMT

Server:Apache/2.2.15 (CentOS)

Last-Modified:Fri, 20 Apr 2018 07:28:38 GMT

ETag:"dbec8-6e-56a42a2f67580"

Accept-Ranges:bytes

Content-Length:110

Connection:close

Content-Type:text/html; charset=UTF-8

<h1>Welcometo Http on web2.field.com!!</h1>

<p>If yousee this page,test ok. </p>

<p><em>Thankyou!</em></p>

再刷新一次页面,可以看到:

X-Cache:HIT

X-Via:192.168.88.131

缓存命中,此时请求不会被传送到后端。

Nginx配置使用upstream负载均衡和proxy_cache缓存相关推荐

  1. Nginx-06:Nginx配置实例之负载均衡

    Nginx配置实例之负载均衡 目的:访问http://192.168.17.129/edu/a.html时平均的分配到8080和8081端口,即实现负载均衡的效果. 1.准备工作 (1)准备两台tom ...

  2. Nginx 反向代理、负载均衡、页面缓存、URL重写及读写分离详解【转载】

    本文只为备份,以防失效.原文请看https://blog.51cto.com/freeloda/1288553 补充说明:部分图片为测试图片,未完全复制,参考文字描述即可. Nginx 反向代理.负载 ...

  3. Nginx 反向代理、负载均衡、页面缓存、URL重写及读写分离详解(1)

    大纲 一.前言 二.环境准备 三.安装与配置Nginx 四.Nginx之反向代理 五.Nginx之负载均衡 六.Nginx之页面缓存 七.Nginx之URL重写 八.Nginx之读写分离 注,操作系统 ...

  4. Nginx 反向代理、负载均衡、页面缓存、URL重写、读写分离及简单双机热备详解...

    大纲 一.前言 二.环境准备 三.安装与配置Nginx  (windows下nginx安装.配置与使用) 四.Nginx之反向代理 五.Nginx之负载均衡  (负载均衡算法:nginx负载算法 up ...

  5. Nginx 反向代理、负载均衡、页面缓存、URL重写及读写分离详解

    标签:读写分离 页面缓存 URL重写 Nginx 反向代理 原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://freeloda.bl ...

  6. Nginx反向代理、负载均衡、页面缓存、URL重写及读写分离详解

    大纲 一.前言 二.环境准备 三.安装与配置Nginx 四.Nginx之反向代理 五.Nginx之负载均衡 六.Nginx之页面缓存 七.Nginx之URL重写 八.Nginx之读写分离 注,操作系统 ...

  7. Nginx 配置实战:负载均衡的实现

    负载均衡的必要性 那些星星就是服务器,不过这个例子并不是实际生产中的采用的模型,因为这种星型构架,如果中间的服务器塌了,周围四个也无法联网,但是这个例子就是说明,每个服务器集群会有一个或者几个中心服务 ...

  8. django部署 nginx 配置简单的负载均衡

    这里使用的负载均衡就是将过来的请求分发给不同的django服务去处理,不同的django服务使用的相同的数据库,那么来看nginx配置 upstream test01 {# 这里配置的是简单的负载均衡 ...

  9. nginx学习九 upstream 负载均衡

    2019独角兽企业重金招聘Python工程师标准>>> 语法 Syntax: upstream name {...} Default:-- Context:http 后端服务器在负载 ...

  10. Nginx反向代理以及负载均衡配置

    一 .nginx 的优缺点: nginx 相对 apache 的优点: 轻量级,同样起web 服务,比apache 占用更少的内存及资源 抗并发,nginx 处理请求是异步非阻塞的,而apache 则 ...

最新文章

  1. centos pptp+l2tp+radius+mysql+tc限速,安装配置
  2. 不想当全栈的设计师不是_但我不想成为产品设计师
  3. html框架有什么作用,使用HTML5+CSS+JS框架有那些好处
  4. jeecmsv9导入mysql详细步骤_jeecms v9.3数据库导入
  5. 容器安全 - 非特权/非root用户运行容器,提升容器的运行安全
  6. java实现线性顺序表
  7. Ubuntu下Android Studio连接手机无法识别
  8. 数据--第20课-递归的应用实战二
  9. poj 1087.A Plug for UNIX (最大流)
  10. pip下载速度慢的解决方法
  11. python携程酒店评论_Selenium爬携程酒店评论+jieba数据分析实战
  12. Monash call:概述生物特征识别
  13. GPS 入门 1 —— 基础知识
  14. Apollo第五讲——Apollo定位模块
  15. FDC2214——电容传感器芯片的使用与配置(STM32控制)
  16. 快手年终奖25个月,背后故事你们知道吗?
  17. 计算机多用户如何共享软件,电脑端有没有可以多人共享日程表的软件?
  18. 蜂窝空间两点最短路径的Python实现
  19. 智慧农贸系统-助力农贸产业升级,优化市民菜篮子
  20. 4种SaaS商业模式+12种商业模型(赠阅)

热门文章

  1. 移动开发技术的进化历程(原生开发与跨平台技术)
  2. PRCV2018|美图短视频实时分类挑战赛冠军解决方案介绍
  3. 为什么那么多城市房价开始跌了,还是有人相信房价会一直涨?
  4. 微信网页版(在电脑上聊微信)
  5. c语言 x%2 什么意思,《X》歌词 printf((x%2)?**%d:##%d\n,x);是什么意思?
  6. 每个人都在经历淘宝的“大数据杀熟”,这5个办法巧妙避开
  7. chrome 自动操纵谷歌小恐龙
  8. 四大组件之Activity(下)
  9. android 获取堆栈地址,Android查看activity的任务堆栈
  10. 苹果屏蔽更新描述文件_屏蔽iPhone更新的iOS描述文件安装办法