用户(浏览器) 请求网站资源 -> 直接定位到django后台(全部的请求压力,都直接给了后台)css
django默认对并发性 不好,而且处理网页的静态资源,效率不好html
10万个并发请求 -> 后台应用python
用户 > nginx(自然并发性很高,而且处理静态资源css,js.jpg) ,静态资源,nginx直接从磁盘上返回 >nginx
server{ listen 80; server_name localhost.com; location / { root /opt/django_crm; index index.html; } }
咱们在/opt/django_crm中 ,放入一个图片 crm2.pngweb
这是一个静态资源算法
localhost.com/crm.png ,若是找不到资源直接返回404数据库
若是我发送了一个动态请求,(对数据库进行交互,必须有编程语言的支撑)django
localhost.com/login/ -> django 后台,django这个编程语言框架,就能够对login请求处理编程
实验环境准备,2台机器vim
用户: 浏览器发起请求, 获得请求
192.168.15.71 nginx反向代理服务器(房屋中介代理)
192.168.15.73 真实资源服务器 (有房源的房东)
分别在 2 台机器上,安装nginx
先配置真实资源服务器,
192.168.15.73 打开这个机器的页面是吃鸡的网游页面
配置反向代理服务器
修改 **nginx.conf ** 以下
虚拟主机的加载,自上而下的加载,若是是访问的ip地址,永远访问第一个
#咱们这个nginx服务器,再也不是用做虚拟主机了 #而是直接转发别人的请求,是一个代理身份 server { listen 80; server_name s16chiji.com; location / { #root /opt/s16chiji; #index index.html; #当个人请求是 s16chiji.com的时候,这个nginx不作处理,直接转发请求给另外一台机器 proxy_pass http://192.168.15.73; } #经过这个参数,定义错误页面的文件 ,当状态码是 404 400 401 时,返回40x.html页面 error_page 404 401 400 403 /40x.html; }
nginx 脚本命令
nginx 直接输入是启动 nginx -s stop 中止 nginx -s reload 平滑重启,从新读取配置文件
如何配置nginx,支持负载均衡
环境准备,准备3台机器
服务器1 nginx负载均衡器 192.168.15.71
服务器2 web应用资源1 192.168.15.73
服务器3 web应用资源2 39.96.68.102
配置负载军器 ,192.168.15.71 机器
nginx.conf,修改添加以下参数
这个参数,应该写在http{}内部,写在server{}以上 upstream s16backup { server 192.168.15.75; server 192.168.15.118; }
第一个虚拟主机,修改配置以下
server { listen 80; server_name s16chiji.com; location / { #root /opt/s16chiji; #index index.html; #当个人请求是 s16chiji.com的时候,这个nginx不作处理,直接转发请求给另外一台机器 #proxy_pass http://192.168.15.73; #这个参数,转发给地址池 proxy_pass http://s16backup; } #经过这个参数,定义错误页面的文件 ,当状态码是 404 400 401 时,返回40x.html页面 error_page 404 401 400 403 /40x.html; }
分别启动负载均衡器的nginx服务,以及两个资源服务器
nginx负载均衡算法
默认是轮训方式,你一次我一次
权重算法: upstream django { server 192.168.15.73 weight=2; server 192.168.15.118 weight=8; } ip 哈希算法: ip哈希和权重不得公用 upstream django { server 192.168.15.73 ; server 192.168.15.118 ; ip_hash; }
在进行项目部署的时候,若是报错
解决办法: no application not found 就是由于你的uwsgi没找到django的wsgi.py应用文件
为何要用nginx uwsgi
由于用户只想访问 域名,不带有任何端口 经过nginx反向代理,用户直接访问 s16chiji.com ,可是nginx直接转发给了django,咱们其实看到的页面是django uwsgi是支持并发的 python web服务器,让你的django,并发性更高,可是uwsgi不支持静态文件的处理,静态文件会丢失
用nginx处理静态文件,uwsgi处理动态请求
项目部署实验步骤
建立新的虚拟环境,且解决crm的环境依赖
在虚拟环境下安装uwsgi
pip3 install uwsgi
学习uwsgi命令,如何启动python应用
启动python web文件 建立一个test.py写入以下代码
def application(env, start_response): start_response('200 OK', [('Content-Type','text/html')]) return [b"Hello World"] # python3
用uwsgi启动一个python web文件
指定8000端口启动 http服务
指定wsgi文件
uwsgi --http :8000 --wsgi-file test.py
用uwsgi启动django项目
uwsgi --http :9000 --module Alibab_crm.wsgi
uwsgi加上热加载命令
uwsgi --http :8000 --module Alibab_crm.wsgi --py-autoreload=1
使用uwsgi配置文件去启动项目
(alicrm) [root@s16ds Alibab_crm]# cat uwsgi.ini mysite_uwsgi.ini file
# mysite_uwsgi.ini file [uwsgi] # Django-related settings # the base directory (full path) #指定django的项目目录,第一层 chdir = /opt/django_crm # Django's wsgi file #找到django的wsgi文件 #这里须要写项目的第二层目录Alibab_crm module = django_crm.wsgi # the virtualenv (full path) #填写虚拟环境的绝对路径 home = /root/Envs/DjangoCRM # process-related settings # master master = true # maximum number of worker processes processes = 5 # the socket (use the full path to be safe #指定socket协议,运行django,只能与nginx结合时使用 #指定socket协议,运行django,只能与nginx结合时使用 #指定socket协议,运行django,只能与nginx结合时使用 socket = 0.0.0.0:8080 #若是你没用nginx,只想本身启动一个http界面,用这个 # http = 0.0.0.0:8000 # ... with appropriate permissions - may be needed # chmod-socket = 664 # clear environment on exit vacuum = true
经过配置文件启动uwsgi
uwsgi --ini uwsgi.ini
收集django crm的静态文件
编辑crm的settings.py配置文件 写入以下代码
定义django的静态资源根目录,便于用命令收集资源,存放的地儿
STATIC_ROOT="/opt/crm_static" STATIC_URL = '/static/' STATICFILES_DIRS = [ os.path.join(BASE_DIR, 'static') ]
用命令收集静态文件
python3 manage.py collectstatic
配置nginx,反响代理django服务器,且解析静态文件
proxy_pass 仅仅是请求转发的参数,与uwsgi结合,还有跟高级的协议参数
修改nginx配置文件以下
server { listen 80; server_name s16chiji.com; location / { root /opt/s16chiji; index index.html; # 使用uwsgi_pass 转发基于uwsgi协议的一个请求 uwsgi_pass 192.168.15.71:8000; include /opt/nginx112/conf/uwsgi_params; } #配置一个url的入口,告诉django静态文件在哪里去找 #当请求url是 s16chiji.com/static/的时候 #就进行别名,nginx去/opt/crm_static下寻找js文件 location /static { alias /opt/crm_static/; } #经过这个参数,定义错误页面的文件 ,当状态码是 404 400 401 时,返回40x.html页面 error_page 404 401 400 403 /40x.html; error_page 500 502 503 504 /50x.html; }
此时nginx结合uwsgi 已经完成
192.168.15.71
记住这里推出虚拟环境,使用物理环境去运行