uWSGI是一个Web服务器
主要用途是将 Web应用程序部署到生产环境中
可以用来连接Nginx服务与Python动态网站

配置Nginx使其可以将动态访问转交给uWSGI:
安装Python工具及依赖
安装uWSGI并编写配置文件
实现此案例需要按照如下步骤进行。
步骤一:
- 1)安装python依赖软件
-
- [root@proxy python]# yum -y install gcc make python3 python3-devel
- 2)安装项目依赖
-
- [root@proxy python]# pip3 install pytz-2022.6-py2.py3-none-any.whl
- [root@proxy python]# pip3 install Django-1.11.8-py2.py3-none-any.whl
- [root@proxy python]# pip3 install django-bootstrap3-11.0.0.tar.gz
- 3)测试项目
-
- [root@proxy python]# tar -xf python-project-demo.tar.gz
- [root@proxy python]# cd python-project-demo/
- [root@proxy 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文件中的特效注释
-
步骤二:
- 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
-
- [root@proxy python]# 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
灰度发布是使用比较平稳的过渡方式升级或者替换产品项目的方法统称
本案例要求不同IP的客户访问相同代理时,可以看到不同集群主机的内容
创建不同集群,准备多台集群主机,通过$remote_addr变量识别不同客户机
实现此案例需要按照如下步骤进行。
步骤一:使用proxy主机在nginx配置中创建集群
- 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;
- }
- ...
- }
步骤二 为web1新建nginx虚拟主机
- [root@web1 nginx]# vim /usr/local/nginx/conf/nginx.conf
- http {
- ...
- server {
- listen 8001;
- server_name localhost;
- root html8001;
- index index.html;
- }
- ...
- }
- [root@web1 nginx]# mkdir html8001
- [root@web1 nginx]# echo web1-8001 > html8001/index.html
步骤三 为web2新建nginx虚拟主机
- [root@web2 nginx]# vim /usr/local/nginx/conf/nginx.conf
- http {
- ...
- server {
- listen 8002;
- server_name localhost;
- root html8002;
- index index.html;
- }
- ...
- }
- [root@web2 nginx]# mkdir html8002
- [root@web2 nginx]# echo web1-8002 > html8002/index.html
步骤三 测试
- 192.168.99.1访问192.168.99.5
-
- 192.168.99.2访问192.168.99.5
-
- 其他ip访问192.168.99.5
不同ID的客户访问相同代理时,可以看到不同集群主机的内容
使用php页面,定义不同匹配语句
实现此案例需要按照如下步骤进行。
1)使用proxy主机,要先还原nginx,并配置可以解析动态网页
2)测试
浏览器访问192.168.99.5/index.php分别输入不同名称的账户,可以看到"开始"连接的是不同的地址,效果如图-1所示。

需要共享的文件数量较大较多
服务器自身带宽有限
频繁遭受黑客攻击
业务利益最大化
使用limit_rate,在http中定义限速
limit_rate默认值为0,表示不限速
使用limit_rate_after指令可以定义多少数据量以内不限速,超过该量即开启限速
修改配置文件,在server中定义限速
location中定义的限速会更优先
本案例要求配置nginx限流限速,效果如下:
使用limit_rate指令限制速度
实现此案例需要按照如下步骤进行。
- 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)创建测试目录
-
- [root@web1 nginx]# mkdir html/file_a
- [root@web1 nginx]# mkdir html/file_b
- 3)创建测试文件
-
- [root@web1 nginx]# dd if=/dev/zero of=html/test.img bs=100M count=1
- [root@web1 nginx]# dd if=/dev/zero of=html/file_a/test.img bs=100M count=1
- [root@web1 nginx]# dd if=/dev/zero of=html/file_b/test.img bs=100M count=1
- 下载测试
-
- wget www.a.com/test.img
- wget www.b.com/test.img
- wget www.b.com/file_a/test.img
- wget www.b.com/file_b/test.img
- 连接限制(非必须配置)
-
- 修改用户访问连接限制,使一个客户同时打开多个连接也无法突破限制
-
- 首先安装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 ;
- }
资源被其他网站盗用
服务器压力无端增加
valid_referers指令可以检测被访问资源从哪个地址来
- 1)修改配置,添加防盗链测试语句
-
- [root@web1 nginx]# vim /usr/local/nginx/conf/nginx.conf
- server {
- ...
- valid_referers none 192.168.99.100; # 如果请求中的referer 头字段包含者地址是99.100或者没有referer 头字段则有效,
- if ($invalid_referer){ # 如果上述测试无效则条件成立
- return 403; # 返回错误提示
- }
- ...
- }
- 2)web1编写测试页面
-
- [root@web1 nginx]# cat html/index.html
- web1
- 测试页面 --
- <a href="http://192.168.99.100/nr.html">内容</a>
- [root@web1 nginx]# cat html/nr.html
- web1内容页面
- 3)web2编写测试页面
-
- [root@web2 nginx]# cat html/index.html
- web2
- 测试页面 --
- <a href="http://192.168.99.100/nr.html">内容</a>
- 4)测试,从192.168.99.100主页点内容可以访问,但从99.200点不可以