Nginx基础(06)

发布于:2024-04-17 ⋅ 阅读:(25) ⋅ 点赞:(0)

Nginx基础(05)

uWSGI

  • 介绍

    • uWSGI 是一个 Web服务器

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

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

1. 用 uWSGI 部署 Python 网站项目

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

  • 安装 python 工具及依赖

  • 安装 uWSGI 并编写配置文件

安装 python 工具及依赖
1) 安装python 依赖软件

gcc make python3 python3-devel

2) 安装项目依赖

[root@proxy python]#

pip3 install  pytz-2022.6-py2.py3-none-any.whl
pip3 install  Django-1.11.8-py2.py3-none-any.whl
pip3 install  django-bootstrap3-11.0.0.tar.gz
3) 测试项目

[root@proxy python]#

tar -xf python-project-demo.tar.gz
cd python-project-demo/
python3 manage.py runserver 0.0.0.0:8000

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

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

    <!--    {% bootstrap_css %}
        {% bootstrap_javascript %}
    -->
安装 uWSGI 并修改配置文件
1) 安装 uWSGI
    [root@proxy python-project-demo]# cd ..
    [root@proxy python]# pip3 install uWSGI-2.0.21.tar.gz
    [root@proxy python]# vim   myproject.ini
    [uwsgi]
    socket=127.0.0.1:8000                  #与web服务(nginx)通信的接口
    chdir=/root/python/python-project-demo          #项目的工作目录
    wsgi-file=learning_log/wsgi.py         #指定项目中的wsgi.py配置文件
    daemonize=/var/log/uwsgi.log           #指定日志文件位置
    #processes=4    #指定启动进程的数目
    #master=true     #开启主进程管理模式
2) 运行 uWSGI

uwsgi --ini myproject.ini #读取myproject.ini运行uWSGI

3) 修改nginx配置文件 , 添加 uWSGI 转发

此乃动态访问

    [root@proxy python]# vim /usr/local/nginx/conf/nginx.conf
    ...
            location / {
                uwsgi_pass 127.0.0.1:8000;        #动态页面交给uWSGI
                include uwsgi_params;            #调用uWSGI配置文件
                root   html;
                index  index.html index.htm;
            }
    ...
    [root@proxy python]# /usr/local/nginx/sbin/nginx

测试

使用浏览器访问192.168.99.5

想访问静态页面实现动静分离 , 则添加新的location组

location /static {
    root   html;
}
           
mkdir /usr/local/nginx/html/static
echo "静态" > /usr/local/nginx/html/static/a.html

测试

使用浏览器访问192.168.99.5/static/a.html

灰度发布 / 金丝雀发布

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

  • 主要作用

    • 及时发现项目问题

    • 尽早获取用户反馈信息 , 以改进产品

    • 如果项目有问题 , 可以将问题影响控制到最小范围

1.配置Nginx实现用IP测试灰度发布

  • 不同IP的客户访问相同代理时 , 可以看到不同集群主机的内容
  • 创建不同集群 , 准备多台集群主机 , 通过 $remote_addr 变量识别不同客户机
1) 使用 proxy 主机在 nginx 配置中创建集群
    [root@proxy nginx]# vim /usr/local/nginx/conf/nginx.conf
    http {    
    ...
        upstream s8001 {                    #测试集群1
            server 192.168.99.100:8001;
        }
        upstream s8002 {                    #测试集群2
            server 192.168.99.200:8002;
        }
        upstream default {                    #正常业务集群
            server 192.168.99.100:80;
            server 192.168.99.200:80;
        }
        server {
            listen       80;
            server_name  localhost;
    ...
        set $group "default";             #定义变量$group,默认值default
        if ($remote_addr ~ "192.168.99.1"){ 
        							#如果客户机ip是99.1就访问集群1
            set $group s8001;
        }
        if ($remote_addr ~ "192.168.99.2"){      
        								#如果客户机ip是99.2就访问集群1
            set $group s8002;
        }
            location / {
                proxy_pass http://$group;        #调用集群
                root   html;
                index  index.html index.htm;
            }
    ...
    }
    [root@proxy nginx]# sbin/nginx  -s reload
2) web1 新建 nginx 虚拟主机
vim /usr/local/nginx/conf/nginx.conf
http {    
...
server {
    listen 8001;
    server_name localhost;
    root html8001;
    index index.html;
}
...
}

sbin/nginx  -s reload
mkdir html8001
echo web1-8001 > html8001/index.html
3) web2 新建 nginx 虚拟主机
vim /usr/local/nginx/conf/nginx.conf
http {
...
server {
    listen 8002;
    server_name localhost;
    root html8002;
    index index.html;
}
...
}

sbin/nginx  -s reload

mkdir html8002
echo web1-8002 > html8002/index.html
4) 测试
192.168.99.1访问192.168.99.5

192.168.99.2访问192.168.99.5

其他ip访问192.168.99.5
2. 通过不同用户ID测试灰度发布
  • 不同ID的客户访问相同代理时 , 可以看到不同集群主机的内容
  • 使用php页面 , 定义不同匹配语句
1) 使用 proxy 主机 , 要先还原 nginx , 并配置可以解析动态网页
    [root@proxy nginx]# vim html/home.php        
    		#修改php页面,将原有Welcome那行修改成以下状态
    Welcome :  <?php
    if(preg_match("/^abc/",$_SESSION['login_user'])) {        
 #preg_match匹配正则,如果登录账号是以abc开头,就连接99.100,否则连接99.200
    echo "<a href='http://192.168.99.100'>开始</a>";
    }
    else
    {
    echo "<a href='http://192.168.99.200'>开始</a>";
    }
    ?>

访问限制

  • 需求
    • 需要共享的文件数量较大较多
    • 服务器自身宽带有限
    • 频繁遭受黑客攻击
    • 业务利益最大化
  • 配置 nginx 限流限速
    • 使用 Nginx 配置全局限速 100k
    • 配置虚拟主机 www.b.com 限速200k
    • 该网站根目录下的 file_a 目录中的所有数据限速 300k
    • file_b 目录下的数据不限速
  • 使用 limit_rate 指令限制速度
    • [ limit_rate 10K ] 限制速度是 10K
    • [ limit_rate_after 10m ] 定义10m数据以上开启限速
1) 定义limit_rate限制
    [root@web1 nginx]# vim /usr/local/nginx/conf/nginx.conf
    http {
    ...    
        limit_rate 100k;        #全局限速
        server {
            limit_rate 200k;            #虚拟主机限速
            listen 80;
            server_name www.b.com;
            root html;
            index index.html;
            location /file_a {
                limit_rate 300k;        #file_a目录限速300k
            }
            location /file_b {
                limit_rate 0k;            #file_b目录不限速
            }
        }
2) 创建测试目录 */html/file_{a,b}
3) 创建测试文件并测试
dd if=/dev/zero of=html/test.img  bs=100M count=1
  • dd 命令用于 读取 , 转换并输出数据
    • dd 可从标准输入或文件中读取数据 , 根据指定的格式来转换数据 , 再输出到文件 , 设备或标准输出.
    • if=文件名a 输入文件名 , 缺省为标准输入 , 即指定源文件
    • of=文件名b 输入文件名 , 缺省为标准输出 , 即指定目的文件
    • bs=bytes read and write up to Bytes bytes at a time
    • count=N copy only N input blocks

下载测试

 wget www.a.com/test.img
4) 连接限制(非必须配置)

修改用户访问连接限制,使一个客户同时打开多个连接也无法突破限制

首先安装ngx_http_limit_conn_module模块

http {
    limit_conn_zone $binary_remote_addr zone=addr:10m;        
    server {
location /app {
limit_rate 30k;     
limit_conn addr 1 ;    
}