• Nginx 学习(八)Nginx实现用IP测试灰度发布


    一  uWSGI

    1  基本概念

    uWSGI是一个Web服务器

    主要用途是将 Web应用程序部署到生产环境中

    可以用来连接Nginx服务与Python动态网站

    2  项目部署 

    (1)问题

    配置Nginx使其可以将动态访问转交给uWSGI:

    (2)方案

    安装Python工具及依赖

    安装uWSGI并编写配置文件

    (3)步骤

    实现此案例需要按照如下步骤进行。

    步骤一:

    1. 1)安装python依赖软件
    2. [root@proxy python]# yum -y install gcc make python3 python3-devel
    3. 2)安装项目依赖
    4. [root@proxy python]# pip3 install pytz-2022.6-py2.py3-none-any.whl
    5. [root@proxy python]# pip3 install Django-1.11.8-py2.py3-none-any.whl
    6. [root@proxy python]# pip3 install django-bootstrap3-11.0.0.tar.gz
    7. 3)测试项目
    8. [root@proxy python]# tar -xf python-project-demo.tar.gz
    9. [root@proxy python]# cd python-project-demo/
    10. [root@proxy python-project-demo]# python3 manage.py runserver 0.0.0.0:8000

    之后可在浏览器访问192.168.99.5:8000,测试完毕后按ctrl + c

    注意:

    1. 测试时如果无法连接外网,可能需要将python-project-demo/learning_logs/templates/base.html文件中的特效注释

    步骤二:

    1. 1)安装uWSGI
    2. [root@proxy python-project-demo]# cd ..
    3. [root@proxy python]# pip3 install uWSGI-2.0.21.tar.gz
    4. [root@proxy python]# vim myproject.ini
    5. [uwsgi]
    6. socket=127.0.0.1:8000 # 与web服务(nginx)通信的接口
    7. chdir=/root/python/python-project-demo # 项目的工作目录
    8. wsgi-file=learning_log/wsgi.py # 指定项目中的wsgi.py配置文件
    9. daemonize=/var/log/uwsgi.log # 指定日志文件位置
    10. #processes=4 # 指定启动进程的数目
    11. #master=true # 开启主进程管理模式
    12. 2)运行uWSGI
    13. [root@proxy python]# uwsgi --ini myproject.ini # 读取myproject.ini运行uWSGI
    14. 3)修改nginx配置文件,添加uWSGI转发
    15. [root@proxy python]# vim /usr/local/nginx/conf/nginx.conf
    16. ...
    17. location / {
    18. uwsgi_pass 127.0.0.1:8000; # 动态页面交给uWSGI
    19. include uwsgi_params; # 调用uWSGI配置文件
    20. root html;
    21. index index.html index.htm;
    22. }
    23. ...
    24. [root@proxy python]# /usr/local/nginx/sbin/nginx

    测试

    使用浏览器访问192.168.99.5

    二  灰度发布

    1  基本概念

    灰度发布是使用比较平稳的过渡方式升级或者替换产品项目的方法统称

    2  主要作用

    • 及时发现项目问题
    • 尽早获取用户反馈信息,以改进产品
    • 如果项目产生问题,可以将问题影响控制到最小范围

    3  应用案列(一)配置Nginx实现用IP测试灰度发布

    -  问题

    本案例要求不同IP的客户访问相同代理时,可以看到不同集群主机的内容

    -  方案

    创建不同集群,准备多台集群主机,通过$remote_addr变量识别不同客户机

    -  步骤

    实现此案例需要按照如下步骤进行。

    步骤一:使用proxy主机在nginx配置中创建集群

    1. 1)使用proxy主机在nginx配置中创建集群
    2. [root@proxy nginx]# vim /usr/local/nginx/conf/nginx.conf
    3. http {
    4. ...
    5. upstream s8001 { #测试集群1
    6. server 192.168.99.100:8001;
    7. }
    8. upstream s8002 { #测试集群2
    9. server 192.168.99.200:8002;
    10. }
    11. upstream default { #正常业务集群
    12. server 192.168.99.100:80;
    13. server 192.168.99.200:80;
    14. }
    15. server {
    16. listen 80;
    17. server_name localhost;
    18. ...
    19. set $group "default"; #定义变量$group,默认值default
    20. if ($remote_addr ~ "192.168.99.1"){ #如果客户机ip是99.1就访问集群1
    21. set $group s8001;
    22. }
    23. if ($remote_addr ~ "192.168.99.2"){ #如果客户机ip是99.2就访问集群1
    24. set $group s8002;
    25. }
    26. location / {
    27. proxy_pass http://$group; #调用集群
    28. root html;
    29. index index.html index.htm;
    30. }
    31. ...
    32. }

    步骤二  为web1新建nginx虚拟主机

    1. [root@web1 nginx]# vim /usr/local/nginx/conf/nginx.conf
    2. http {
    3. ...
    4. server {
    5. listen 8001;
    6. server_name localhost;
    7. root html8001;
    8. index index.html;
    9. }
    10. ...
    11. }
    12. [root@web1 nginx]# mkdir html8001
    13. [root@web1 nginx]# echo web1-8001 > html8001/index.html

    步骤三  为web2新建nginx虚拟主机

    1. [root@web2 nginx]# vim /usr/local/nginx/conf/nginx.conf
    2. http {
    3. ...
    4. server {
    5. listen 8002;
    6. server_name localhost;
    7. root html8002;
    8. index index.html;
    9. }
    10. ...
    11. }
    12. [root@web2 nginx]# mkdir html8002
    13. [root@web2 nginx]# echo web1-8002 > html8002/index.html

    步骤三 测试

    1. 192.168.99.1访问192.168.99.5
    2. 192.168.99.2访问192.168.99.5
    3. 其他ip访问192.168.99.5

    4  应用案例(二)通过不同用户ID测试灰度发布

    -  问题

    不同ID的客户访问相同代理时,可以看到不同集群主机的内容

    -  方案

    使用php页面,定义不同匹配语句

    -  步骤

    实现此案例需要按照如下步骤进行。

    1)使用proxy主机,要先还原nginx,并配置可以解析动态网页

    1. [root@web2 nginx]# vim html/home.php #修改php页面,将原有Welcome那行修改成以下状态
    2. Welcome :
    3. if(preg_match("/^abc/",$_SESSION['login_user'])) { #preg_match匹配正则,如果登录账号是以abc开头,就连接99.100,否则连接99.200
    4. echo "开始";
    5. }
    6. else
    7. {
    8. echo "开始";
    9. }
    10. ?>

    2)测试

    浏览器访问192.168.99.5/index.php分别输入不同名称的账户,可以看到"开始"连接的是不同的地址,效果如图-1所示。

    三  访问限制

    1  限流限速

    (1)使用场景

    需要共享的文件数量较大较多

    服务器自身带宽有限

    频繁遭受黑客攻击

    业务利益最大化

    (2)全局限速

    使用limit_rate,在http中定义限速

    limit_rate默认值为0,表示不限速

    使用limit_rate_after指令可以定义多少数据量以内不限速,超过该量即开启限速

    (3)针对虚拟主机与路径限速

    修改配置文件,在server中定义限速

    location中定义的限速会更优先

    (4)应用案例4:配置网站限流限速

    -  问题

    本案例要求配置nginx限流限速,效果如下:

    • 使用Nginx配置全局限速100k
    • 配置虚拟主机www.b.com限速200k,该网站根目录下的file_a目录中的所有数据限速300k,file_b目录中的所有数据不限速
    -  方案

    使用limit_rate指令限制速度

    -  步骤

    实现此案例需要按照如下步骤进行。

    1. 1)定义limit_rate限制
    2. [root@web1 nginx]# vim /usr/local/nginx/conf/nginx.conf
    3. http {
    4. ...
    5. limit_rate 100k; # 全局限速
    6. server {
    7. limit_rate 200k; # 虚拟主机限速
    8. listen 80;
    9. server_name www.b.com;
    10. root html;
    11. index index.html;
    12. location /file_a {
    13. limit_rate 300k; # file_a目录限速300k
    14. }
    15. location /file_b {
    16. limit_rate 0k; # file_b目录不限速
    17. }
    18. }
    19. 2)创建测试目录
    20. [root@web1 nginx]# mkdir html/file_a
    21. [root@web1 nginx]# mkdir html/file_b
    22. 3)创建测试文件
    23. [root@web1 nginx]# dd if=/dev/zero of=html/test.img bs=100M count=1
    24. [root@web1 nginx]# dd if=/dev/zero of=html/file_a/test.img bs=100M count=1
    25. [root@web1 nginx]# dd if=/dev/zero of=html/file_b/test.img bs=100M count=1
    26. 下载测试
    27. wget www.a.com/test.img
    28. wget www.b.com/test.img
    29. wget www.b.com/file_a/test.img
    30. wget www.b.com/file_b/test.img
    31. 连接限制(非必须配置)
    32. 修改用户访问连接限制,使一个客户同时打开多个连接也无法突破限制
    33. 首先安装ngx_http_limit_conn_module模块
    34. http {
    35. limit_conn_zone $binary_remote_addr zone=addr:10m;
    36. server {
    37. location /app {
    38. limit_rate 30k;
    39. limit_conn addr 1 ;
    40. }

    2  防盗链

    (1)使用场景

    资源被其他网站盗用

    服务器压力无端增加

    (2)实现方式

    • valid referers指令可以检测被访问资源从哪个地址来
    • 通过referer头字段判断
    • 如果为空则报403错误

    valid_referers指令可以检测被访问资源从哪个地址来

    1. 1)修改配置,添加防盗链测试语句
    2. [root@web1 nginx]# vim /usr/local/nginx/conf/nginx.conf
    3. server {
    4. ...
    5. valid_referers none 192.168.99.100; # 如果请求中的referer 头字段包含者地址是99.100或者没有referer 头字段则有效,
    6. if ($invalid_referer){ # 如果上述测试无效则条件成立
    7. return 403; # 返回错误提示
    8. }
    9. ...
    10. }
    11. 2)web1编写测试页面
    12. [root@web1 nginx]# cat html/index.html
    13. web1
    14. 测试页面 --
    15. <a href="http://192.168.99.100/nr.html">内容</a>
    16. [root@web1 nginx]# cat html/nr.html
    17. web1内容页面
    18. 3)web2编写测试页面
    19. [root@web2 nginx]# cat html/index.html
    20. web2
    21. 测试页面 --
    22. <a href="http://192.168.99.100/nr.html">内容</a>
    23. 4)测试,从192.168.99.100主页点内容可以访问,但从99.200点不可以

  • 相关阅读:
    【java核心技术】Java知识总结 -- 接口
    springboot毕设项目大学生体质测试管理系统415ef(java+VUE+Mybatis+Maven+Mysql)
    java spring cloud 企业电子招标采购系统源码:营造全面规范安全的电子招投标环境,促进招投标市场健康可持续发展
    相机光学(三十七)——自动对焦原理
    探秘电大搜题:山东开放大学学子的必备利器
    注册表单mvc 含源代码
    Web ui自动化测试框架总结
    盈科视控荣获创响中国大赛第四名
    电动吸吮式过滤器 自清洗过滤器
    猿创征文|为了学习英语,我开发了一个单词对战系统
  • 原文地址:https://blog.csdn.net/2301_79227925/article/details/132630885