Nginx反向代理与负载均衡应用实践(二)

链接:https://pan.baidu.com/s/1xB20bnuanh0Avs4kwRpSXQ

提取码:migq

复制这段内容后打开百度网盘手机App,操作更方便哦

根据URL中的目录地址实现代理转发

1.软硬件准备

1.1硬件准备

准备4台VM虚拟机(有物理服务器更佳),一台做负载均衡,三台做RS,如下表:

HOSTNAME IP 说明 分类
lb 192.168.200.64 Nginx负载均衡器
web01 192.168.200.63 Web01服务器 上传服务器池
web02 192.168.200.60 Web02服务器 动态服务器池
web03 192.168.200.65 Web03服务器 静态服务器池

1.2软件准备

  • 系统:CentOS6.5 x86_64
  • 软件:nginx-1.10.2.tar.gz

2.根据URL中的目录地址实现代理转发的说明

案例背景:

  • 通过Nginx实现动静分离,即通过Nginx反向代理配置规则实现让动态资源和静态资源及其他业务分别由不同的服务器解析,以解决网站性能,安全,用户体验等重要问题。
  • 下图为企业常见的动静分离集群架构图,此架构图适合网站前端只使用同一个域名提供服务的场景,例如,用户访问的域名是www.yunjisuan.com,然后,当用户请求www.yunjisuan.com/upload/xx地址时候,代理会分配请求到上传服务器池处理数据;当用户请求www.yunjisuan.com/static/xx地址的时候,代理会分配请求到静态服务器池请求数据;当用户请求www.yunjisuan.com/xx地址的时候,即不包含上述指定的目录地址路径时,代理会分配请求到默认的动态服务器池请求数据(注意:上面的xx表示任意路径)。

3.项目实战准备

3.1先进行企业案例需求梳理:

  • 当用户请求www.yunjisuan.com/upload/xx地址时,实现由upload上传服务器池处理请求。
  • 当用户请求www.yunjisuan.com/static/xx地址时,实现由静态服务器池处理请求。
  • 除此以外,对于其他访问请求,全都由默认的动态服务器池处理请求.

3.2在反向均衡上进行upstream模块服务器池的配置

  1. [root@lb conf]# pwd
  2. /usr/local/nginx/conf
  3. [root@lb conf]# cat nginx.conf
  4. worker_processes 1;
  5. events {
  6. worker_connections 1024;
  7. }
  8. http {
  9. include mime.types;
  10. default_type application/octet-stream;
  11. sendfile on;
  12. keepalive_timeout 65;
  13. upstream upload_pools { #将符合upload的请求交给上传服务器池upload_pools
  14. server 192.168.200.63:80 weight=1;
  15. }
  16. upstream default_pools { #不符合上述规则的请求,默认全部交给动态服务器池default_pools
  17. server 192.168.200.60:80 weight=1;
  18. }
  19. upstream static_pools { #将符合static的请求交给静态服务器池static_pools
  20. server 192.168.200.65:80 weight=1;
  21. }
  22. server {
  23. listen 80;
  24. server_name www.yunjisuan.com;
  25. location /upload/ {
  26. proxy_pass http://upload_pools;
  27. include extar/proxy.conf;
  28. }
  29. location / {
  30. proxy_pass http://default_pools;
  31. include extar/proxy.conf;
  32. }
  33. location /static/ {
  34. proxy_pass http://static_pools;
  35. include extar/proxy.conf;
  36. }
  37. }
  38. }
  39. [root@lb conf]# nginx -t #检查配置文件是否有误
  40. nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
  41. nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
  42. [root@lb conf]# /usr/local/nginx/sbin/nginx -s reload
  43. #重启Nginx服务

3.3在web上进行文件配置(方法一)

3.3.1 web01配置(上传)

  1. [root@web01 www]# pwd
  2. /usr/local/nginx/html/www
  3. [root@web01 www]# mkdir upload
  4. [root@web01 www]# echo "upload_pools" >> upload/index.html

3.3.2 web02测试

3.3.3 web02配置(默认-->动态)

  1. [root@web02 www]# pwd
  2. /usr/local/nginx/html/www
  3. [root@web02 www]# echo "default_pools" > index.html

3.3.4 web01测试

3.3.5 web03配置(静态)

  1. [root@web03 www]# pwd
  2. /usr/local/nginx/html/www
  3. [root@web03 www]# mkdir static
  4. [root@web03 www]# echo "static_pools" >> static/index.html

3.3.6 web01测试

3.3.7其地址与实际访问的内容输出请对照下表:

节点 IP及端口 测试地址 字符串为代表业务
web01 192.168.200.63:80 http://www.yunjisuan.com/upload/index.html static_pools
web02 192.168.200.60:80 http://www.yunjisuan.com/ upload_pools
web03 192.168.200.65:80 http://www.yunjisuan.com/static/index.html default_pools

3.4以if语句实现(方法二)

  1. if ($request_uri ~* "^/static/(.*)$")
  2. {
  3. proxy_pass http://static_pools/$1;
  4. }
  5. if ($request_uri ~* "^/upload/(.*)$")
  6. {
  7. proxy_pass http://upload_pools/$1;
  8. }
  9. location / {
  10. proxy_pass http://default_pools;
  11. include proxy.conf;
  12. }

3.5根据URL目录地址转发的应用场景

  • 根据HTTP的URL进行转发的应用情况,被称为第7层(应用层)的负载均衡,而LVS的负载均衡一般用于TCP等的转发,因此被称为第4层(传输层)的负载均衡。
  • 在企业中,有时希望只用一个域名对外提供服务,不希望使用多个域名对应同一个产品业务,此时就需要在代理服务器上通过配置规则,使得匹配不同规则的请求会交给不同的服务器池处理。这类业务有:
  • 业务的域名没有拆封或者不希望拆分,但希望实现动静分离,多业务分离,这在前面已经讲解过案例了。
  • 不同的客户端设备(例如:手机和PC端)使用同一个域名访问同一个业务网站,就需要根据规则将不同设备的用户请求交给后端不同的服务器处理,以便得到最佳用户体验。这也是非常重要的.

4.根据客户端的设备(user_agent)转发实践需求

为了让不同的客户端设备用户访问有更好的体验,需要在后端架设不同服务器来满足不同的客户端访问,例如:移动客户端访问网站,就需要部署单独的移动服务器及程序,体验才能更好,而且移动端还分苹果,安卓,Ipad等,在传统的情况下,一般用下面的办法解决这个问题

4.1常规4层负载均衡解决方案架构

在常规4层负载均衡架构下,可以使用不同的域名来实现这个需求,例如,人为分配好让移动端用户访问wap.yunjisuan.com,PC客户端用户访问www.yunjisuan.com,通过不同域名来引导用户到指定的后端服务器,该解决方案的架构图如下:

此解决方案的最大问题就是不同客户端的用户要记住对应的域名!而绝大多数用户只会记住www.yunjisuan.com,不会记住wap.yunjisuan.com,这样一来就会导致用户体验不是很好。有没有办法让所有客户端用户只访问一个统一的www.yunjisuan.com这个地址,还能让不同客户端设备都能有更好的访问体验呢?当然有!那就是下面的第7层负载均衡解决方案。

4.2第7层负载均衡解决方案

在第7层负载均衡架构下,就可以不需要人为拆分域名了,对外只需要用一个域名,例如www.yunjisuan.com,通过获取用户请求中的设备信息(利用$http_user_agent获取),根据这些信息转给后端合适的服务器处理,这个方案最大好处就是不需要让用户记忆多个域名了,用户只需要记住主网站地址www.yunjisuan.com,剩下的由网站服务器处理,这样的思路大大地提升了用户访问体验,这是当前企业网站非常常用的解决方案。下图描述了上述解决方案相应的架构逻辑图:

4.3根据客户端设备(user_agent)转发请求实践

这里还是使用static_pools,upload_pools作为本次实验的后端服务器池。下面先根据计算机客户端浏览器的不同设置对应的匹配规则。(由于没有合适的实验验证环境,这里仅作需求实现的细节讲解)

  1. location / {
  2. if ($http_user_agent ~* "MSIE")
  3. #如果请求的浏览器为微软IE浏览器(MSIE),则让请求由static_pools池处理
  4. {
  5. proxy_pass http://static_pools;
  6. }
  7. if ($http_user_agent ~* "Chrome")
  8. #如果请求的浏览器为谷歌浏览器(Chrome),则让请求由upload_pools池处理
  9. {
  10. proxy_pass http//upload_pools;
  11. }
  12. proxy_pass http://default_pools;
  13. #其他客户端,由default_pools处理
  14. include proxy.conf;
  15. }

除了针对浏览器外,上述“$http_user_agent”变量也可针对移动端,比如安卓,苹果,Ipad设备进行匹配,去请求指定的服务器,具体细节配置如下:

  1. location / {
  2. if ($http_user_agent ~* "android")
  3. {
  4. proxy_pass http://android_pools; #这里是android服务器池
  5. }
  6. if ($http_user_agent ~* "iphone")
  7. {
  8. proxy_pass http://iphone_pools; #这里是iphone服务器池
  9. }
  10. proxy_pass http://pc_pools; #这里是默认的pc服务器池
  11. include extra/proxy.conf;
  12. }

4.4根据文件扩展名实现代理转发

除了根据URI路径及user_agent转发外,还可以实现根据文件扩展名进行转发(这里仅以细节配置作为讲解内容,如需测试请同学们自行实验)

4.4.1相关server配置

  1. #先看看location方法的匹配规则,如下:
  2. location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|css|js)$ {
  3. proxy_pass http://static_pools;
  4. include proxy.conf;
  5. }
  6. #下面是if语句方法的匹配规则:
  7. if ($request_uri ~* ".*\.(php|php5)$")
  8. {
  9. proxy_pass http://php_server_pools;
  10. }
  11. if ($request_uri ~* ".*\.(jsp|jsp*|do|do*)$")
  12. {
  13. proxy_pass http://java_server_pools;
  14. }

4.4.2 根据扩展名转发的应用场景

可根据扩展名实现资源的动静分离访问,如图片,视频等请求静态服务器池,PHP,JSP等请求动态服务器池。

  1. location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|css|js)$ {
  2. proxy_pass http://static_pools;
  3. include proxy.conf;
  4. }
  5. location ~ .*\.(php|php3|php5)$ {
  6. proxy_pass http://dynamic_pools;
  7. include proxy.conf
  8. }

在开发无法通过程序实现动静分离的时候,运维可以根据资源实体进行动静分离,而不依赖于开发,具体实现策略是先把后端的服务器分成不同的组。注意,每组服务器的程序都是相同的,因为开发没有把程序拆开,分组后,在前端代理服务器上通过讲解过的路径,扩展名进行规则匹配,从而实现请求的动静分离。

5.Nginx负载均衡检测节点状态

5.1简介

  • 淘宝技术团队开发了一个Tengine(Nginx的分支)模块Nginx_upstream_check_module,用于提供主动式后端服务器健康检查。通过它可以检测后端realserver的健康状态,如果后端realserver不可用,则所有的请求就不会转发到该节点上。
  • Tengine原生支持这个模块,而Nginx则需要通过打补丁的方式将该模块添加到Nginx中。补丁下载地址:https://github.com/yaoweibin/nginx_upstream_check_module下面介绍如何使用这个模块。

5.2安装nginx_upstream_check_module模块

5.2.1检查环境

  1. #系统已经安装了nginx-1.10.2软件
  2. [root@lb yang]# /usr/local/nginx/sbin/nginx -V
  3. nginx version: nginx/1.10.2
  4. built by gcc 4.4.7 20120313 (Red Hat 4.4.7-4) (GCC)
  5. built with OpenSSL 1.0.1e-fips 11 Feb 2013
  6. TLS SNI support enabled
  7. configure arguments: --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module

5.2.2#安装必备安装包

  1. [root@lb ~]# yum -y install wget unzip patch
  2. [root@yangwenbo ~]# rpm -qa wget unzip patch
  3. wget-1.12-1.8.el6.x86_64
  4. patch-2.6-6.el6.x86_64
  5. unzip-6.0-1.el6.x86_64

5.2.3下载补丁包

  1. [root@lb ~]# cd /yang
  2. [root@lb yang]# wget https://codeload.github.com/yaoweibin/nginx_upstream_check_module/zip/master
  3. [root@lb yang]# ls
  4. master
  5. [root@lb yang]# unzip master
  6. [root@lb yang]# mv nginx_upstream_check_module-master /usr/src/

5.2.4因为是对源程序打补丁,所以还需要Nginx源程序

  1. [root@lb yang]# cd /usr/src/nginx-1.10.2/
  2. [root@lb nginx-1.10.2]# patch -p0 < /usr/src/nginx_upstream_check_module-master/check_1.9.2+.patch
  3. patching file src/http/modules/ngx_http_upstream_hash_module.c
  4. patching file src/http/modules/ngx_http_upstream_ip_hash_module.c
  5. patching file src/http/modules/ngx_http_upstream_least_conn_module.c
  6. patching file src/http/ngx_http_upstream_round_robin.c
  7. patching file src/http/ngx_http_upstream_round_robin.h

5.2.5备份源安装程序

  1. [root@lb nginx-1.10.2]# cd /usr/local/
  2. [root@lb local]# ls
  3. bin etc games include lib lib64 libexec nginx sbin share src
  4. [root@lb local]# mv nginx{,.ori}
  5. [root@lb local]# ls
  6. bin etc games include lib lib64 libexec nginx.ori sbin share src

5.2.6#重新进行编译,编译的参数要和以前一致,最后加上 --add-module=/usr/src/nginx_upstream_check_module-master/

  1. [root@lb local]# cd /usr/src/nginx-1.10.2/
  2. [root@lb nginx-1.10.2]# ./configure --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --add-module=/usr/src/nginx_upstream_check_module-master/
  3. #以下省略。。。
  4. [root@lb nginx-1.10.2]# make && make install
  5. #以下省略。。。

5.2.7拷贝源配置文件到当前Nginx的安装目录下

  1. [root@lb local]# pwd
  2. /usr/local
  3. [root@lb local]# cp nginx.ori/conf/nginx.conf nginx/conf/
  4. cp: overwrite `nginx/conf/nginx.conf'? y
  5. [root@lb local]# cp nginx.ori/conf/extar/proxy.conf nginx/conf/extar/
  6. [root@lb local]# /usr/local/nginx/sbin/nginx -t
  7. nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
  8. nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

5.3配置Nginx健康检查,如下:

  1. [root@lb conf]# pwd
  2. /usr/local/nginx/conf
  3. [root@lb conf]# cat nginx.conf
  4. worker_processes 1;
  5. events {
  6. worker_connections 1024;
  7. }
  8. http {
  9. include mime.types;
  10. default_type application/octet-stream;
  11. sendfile on;
  12. keepalive_timeout 65;
  13. upstream static_pools {
  14. server 192.168.200.63:80 weight=1;
  15. server 192.168.200.65:80 weight=1;
  16. check interval=3000 rise=2 fall=5 timeout=1000 type=http;
  17. #对static服务器池开启健康监测
  18. }
  19. upstream default_pools {
  20. server 192.168.200.60:80 weight=1;
  21. }
  22. server {
  23. listen 80;
  24. server_name www.yunjisuan.com;
  25. location / {
  26. proxy_pass http://default_pools;
  27. include extar/proxy.conf;
  28. }
  29. location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|css|js)$ {
  30. proxy_pass http://static_pools;
  31. include extar/proxy.conf;
  32. }
  33. location /status {
  34. check_status; #启动健康检查模块
  35. access_log off; #关闭此location的访问日志记录
  36. }
  37. }
  38. }

5.4重启lb1的nginx服务

  1. [root@lb conf]# /usr/local/nginx/sbin/nginx -t
  2. nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
  3. nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
  4. [root@lb conf]# /usr/local/nginx/sbin/nginx
  5. [root@lb conf]# netstat -antup | grep nginx
  6. tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 3817/nginx
  7. #注意此处必须重启Nginx,不能重新加载

check interval=3000 rise=2 fall=5 timeout=1000 type=http;

上面配置的意思时,对static_pools这个负载均衡条目中的所有节点,每隔3秒检测一次,请求2次正常则标记realserver状态为up,如果检测5次都失败,则标记realserver的状态为down,超时时间为1秒,检查的协议是HTTP。

详细用法见官网:http://tengine.taobao.org/document_cn/http_upstream_check_cn.html

5.5访问页面时,显示如下图所示:

5.6在lb1配置文件的upstream default_pools{}里也加入健康监测命令

  1. [root@lb conf]# cat nginx.conf
  2. worker_processes 1;
  3. events {
  4. worker_connections 1024;
  5. }
  6. http {
  7. include mime.types;
  8. default_type application/octet-stream;
  9. sendfile on;
  10. keepalive_timeout 65;
  11. upstream static_pools {
  12. server 192.168.200.63:80 weight=1;
  13. server 192.168.200.65:80 weight=1;
  14. check interval=3000 rise=2 fall=5 timeout=1000 type=http;
  15. }
  16. upstream default_pools {
  17. server 192.168.200.60:80 weight=1;
  18. check interval=3000 rise=2 fall=5 timeout=1000 type=http;
  19. default服务器池开启健康监测
  20. }
  21. server {
  22. listen 80;
  23. server_name www.yunjisuan.com;
  24. location / {
  25. proxy_pass http://default_pools;
  26. include extar/proxy.conf;
  27. }
  28. location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|css|js)$ {
  29. proxy_pass http://static_pools;
  30. include extar/proxy.conf;
  31. }
  32. location /status {
  33. check_status;
  34. access_log off;
  35. }
  36. }
  37. }
  38. [root@lb conf]# /usr/local/nginx/sbin/nginx -s reload
  39. #重启服务

5.7再次访问健康监测页面时,显示如下图所示:

5.8宕掉任意一个RS节点后(3个Web服务器任选一个关闭nginx服务)

  1. [root@web01 ~]# /usr/local/nginx/sbin/nginx -s stop

5.9再次访问健康监测页面时,显示如下图所示:

6. proxy_next_upstream 参数补充

当Nginx接收后端服务器返回proxy_next_upstream参数定义的状态码时,会将这个请求转发给正常工作的后端服务器,例如500,502,503,504,此参数可以提升用户的访问体验,具体配置如下:

  1. server {
  2. listen 80;
  3. server_name www.yunjisuan.com;
  4. location / {
  5. proxy_pass http://static_pools;
  6. proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
  7. include proxy.conf;
  8. }
  9. }

Nginx反向代理与负载均衡应用实践(二)的更多相关文章

  1. Nginx反向代理与负载均衡应用实践(一)

    Nginx反向代理与负载均衡应用实践(一) 链接:https://pan.baidu.com/s/1xB20bnuanh0Avs4kwRpSXQ 提取码:migq 复制这段内容后打开百度网盘手机App ...

  2. nginx 反向代理与负载均衡应用实践

    集群介绍 集群就是指一组(若干个)相互独立的计算机,利用高速通信网络组成的一个较大的计算机服务系统,每个集群节点(即集群中的每台计算机)都是运行各自服务的独立服务器.这些服务器之间可以彼此通信,协同向 ...

  3. Linux实战教学笔记30:Nginx反向代理与负载均衡应用实践

    1.1 集群简介 简单地说,集群就是指一组(若干个)相互独立的计算机,利用高速通信网络组成的一个较大的计算机服务系统,每个集群节点(即集群中的每台计算机)都是运行各自服务的独立服务器.这些服务器之间可 ...

  4. Nginx反向代理,负载均衡,redis session共享,keepalived高可用

    相关知识自行搜索,直接上干货... 使用的资源: nginx主服务器一台,nginx备服务器一台,使用keepalived进行宕机切换. tomcat服务器两台,由nginx进行反向代理和负载均衡,此 ...

  5. nginx反向代理与负载均衡

    一:nginx反向代理与负载均衡配置思路与用法 1.nginx反向代理:就是你去相亲时,媒婆就是这里的代理,让媒婆带你去见姑娘 2.nginx负载均衡:就是有很多的媒婆经过商量给你选出最适合你的姑娘, ...

  6. 【转】Nginx反向代理和负载均衡

    原文链接:http://www.cnblogs.com/shuoer/p/7820899.html Nginx反向代理和负载均衡 环境说明 由于我使用的是windows系统,所以我用虚拟机虚拟出来了3 ...

  7. 谁说前端不需要懂-Nginx反向代理与负载均衡

    转:https://juejin.im/post/5b01336af265da0b8a67e5c9 学到老活到老 前端圈一直很新,一直要不停的学习,而且在进入大厂的路上,还要求熟悉一门后台语言等等.用 ...

  8. [转]Nginx反向代理和负载均衡部署指南

    Nginx反向代理和负载均衡部署指南   1.        安装 1)         从Nginx官网下载页面(http://nginx.org/en/download.html)下载Nginx最 ...

  9. 基于Nginx反向代理及负载均衡

    基于Nginx反向代理及负载均衡 参考:http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_pass 只要没有被启用,默认就是 ...

随机推荐

  1. LeetCode Array Easy 268. Missing Number

    Description Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one th ...

  2. 大哥带我走渗透ii--时间盲注,布尔盲注

    5/27 基于布尔的盲注 我连题目都看不懂555,先去补充一点知识.https://blog.csdn.net/weixin_40709439/article/details/81355856 返回的 ...

  3. 五、bootstrap-fileinput

    一.bootstrap-fileinput <!DOCTYPE html> <html lang="en"> <head> <meta c ...

  4. c++网络库之 poco

    java 不好吗?java面向对象很好啊. poco 做的像 java 用起来更面向对象,这是优势.开发速度提升很多.boost 那种是给大牛看的.我觉得 poco 用起来方便,不清楚的地方随时看源码 ...

  5. @InitBinder 前端传递date时间类型属性时,转换错误问题

    在Controller里加上这段代码 @InitBinder public void initBinder(WebDataBinder binder) { binder.registerCustomE ...

  6. windows 安装yaml支持和pytest支持等

    打开cmd 输入pip install pyyaml    #yaml文件支持 输入pip install pytest     #pytest框架支持 输入pip install requests  ...

  7. mysql查询表的创建时间

    mysql查询表的创建时间 查询语句: SELECT table_name,create_time FROM information_schema.TABLES;

  8. js正则去掉所有html标签/某一特定字符

    java后台 String str=hello你好吗,我很好 thank you????噼安胖胖    "; String reg = "[\ud83c\udc00-\ud ...

  9. python3 实现简单ftp服务功能(客户端)

    转载请注明出处! 可执行的命令: lspwdcd put rm get mkdir 上传下载,显示进度百分比以及平均上传下载速度 客户端 main代码: #Author by Andy #_*_ co ...

  10. PHP基础知识------页面静态化

    1.在开发项目时,有时会遇到一些页面数据量特别大,但是又不经常改变的情况,如商城首页等,这时候就需要进行页面静态化,减轻服务器和数据库的压力. 这里我们先用原生的PHP写一个简单的demo,用来理解页 ...